簡體   English   中英

Android如何在SherlockFragment中轉換sherlockActivity

[英]Android How to convert sherlockActivity in SherlockFragment

在使用與SherlockActivity不同的SherlockFragmentActivity之前,我在其中包括了用於所有治療的DrawerMenu並可以正確行走。 但是我決定更改SherlockActivity中的SherlockFragment ,以便從所有視圖上的DrawerMenu受益。 我嘗試了Fragment第一個Activity ,但它沒有出現,但是包含數據庫中數據的列表沒有出現。 請幫幫我。


我的適配器類

public class ListAdapterAnSco extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<AnSco> listAnsco = null;
    private ArrayList<AnSco> arraylist;
    ListAnSco anscoList;

    public ListAdapterAnSco(Context context, List<AnSco> listAnsco) {
        mContext = context;
        this.listAnsco = listAnsco;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<AnSco>();
        this.arraylist.addAll(listAnsco);
    }

    public class ViewHolder {
        TextView an;
    }

    @Override
    public int getCount() {
        return listAnsco.size();
    }

    @Override
    public AnSco getItem(int position) {
        return listAnsco.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.annee_sco_item, null);
            // Locate the TextViews in listview_item.xml
            holder.an = (TextView) view.findViewById(R.id.an_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.an.setText(listAnsco.get(position).getAnsco());
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Send single item click data to SingleItemView Class
                Intent intent = new Intent(mContext, ListPeriode.class);
                // Pass all data rank
                intent.putExtra("ANSCO_ID",(listAnsco.get(position).getIdan()));
                // Pass all data country
                intent.putExtra("ANSCO_AN",(listAnsco.get(position).getAnsco()));
                // Start SingleItemView Class
                mContext.startActivity(intent);
            }
        });

        view.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                int idAn = (listAnsco.get(position).getIdan());
                String annee = (listAnsco.get(position).getAnsco());
                AnSco ansco=new AnSco(idAn,annee);
                AlertDialog diag= Alerts.ShowEditDialogAnSco(mContext,ansco);
                diag.setOnDismissListener(new OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
//                  Intent intent = new Intent(mContext, ListAnSco.class);
//                  mContext.startActivity(intent);
                    notifyDataSetChanged();
                }
            });
            diag.show();

                return false;
            }
        });
        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        listAnsco.clear();
        if (charText.length() == 0) {
            listAnsco.addAll(arraylist);
        } 
        else 
        {
            for (AnSco wp : arraylist) 
            {
                if (wp.getAnsco().toLowerCase(Locale.getDefault()).contains(charText)) 
                {
                    listAnsco.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }


}

我的新片段

public class ListAnSco extends SherlockFragment {

    protected EditText searchText;
    DatabaseHelper dbhelper;

    protected Cursor cursor;
    ListAdapterAnSco adapter;
    private static ListView liste;
    TextView ansco;
    String[] from;
    int to[];
    Context mcont;
    ImageButton btnSearch, btnGo, btnAdd;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.annee_sco_list, container, false);

        // setContentView(R.layout.annee_sco_list);
        // setTitle(R.string.titre_list_ansco);
        searchText = (EditText) v.findViewById(R.id.searchTextAn);
        liste = (ListView) v.findViewById(R.id.list_ansco);
        ansco = (TextView) v.findViewById(R.id.label_ansco);
        btnSearch = (ImageButton) v.findViewById(R.id.btnSearchAn);
        btnGo = (ImageButton) v.findViewById(R.id.btnGoAn);
        btnAdd = (ImageButton) v.findViewById(R.id.btnAddAn);
        mcont = getActivity();
        setInvisible();
        try {
            LoadListAnsco();
        } catch (Exception e) {
            Toast.makeText(mcont, "Impossible d'afficher \n" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }

        btnAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mcont, AddAnSco.class);
                startActivity(intent);
            }
        });

        btnGo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    LoadListAnsco();
                } catch (Exception e) {
                    Toast.makeText(mcont, "Impossible d'afficher \n" + e.toString(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        // Locate the EditText in menu.xml
        // Capture Text in EditText
        searchText.addTextChangedListener(textWatcher);
        btnSearch.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                setVisible();
            }
        });

        return v;
    }

    // EditText TextWatcher
    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            String text = searchText.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

    };

    public void LoadListAnsco() {
        dbhelper = new DatabaseHelper(mcont);
        boolean ok = true;
        try {
            adapter = new ListAdapterAnSco(mcont, dbhelper.getLabelsAnSco());
            liste.setAdapter(adapter);
            liste.setTextFilterEnabled(true);
            Log.i("ListAnsco 3", "OK");
        } catch (Exception ex) {
            ok = false;
            AlertDialog.Builder b = new AlertDialog.Builder(mcont);
            b.setMessage(ex.toString());
            b.show();
        } finally {
            if (ok) {
                dbhelper.close();
            }
        }
    }

    private void setVisible() {
        // TODO Auto-generated method stub
        searchText.setVisibility(View.VISIBLE);
        // Focus on EditText
        searchText.requestFocus();

        // Force the keyboard to show on EditText focus
        InputMethodManager imm = (InputMethodManager) mcont
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

    private void setInvisible() {
        // TODO Auto-generated method stub
        searchText.setText("");
        searchText.clearFocus();
        searchText.setVisibility(View.GONE);
    }

}

我的數據庫助手的一部分

public List < AnSco> getLabelsAnSco(){
        List < AnSco > lansco = new ArrayList < AnSco > ();
        // Select All Query
        String selectQuery = "SELECT  * FROM "+  TABLE_ANSCO+ " order by " + ANSCO;// tAnsco is your table name?

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if ( cursor.moveToFirst () ) {
            do {
                lansco.add ( new AnSco ( cursor.getInt(0) , cursor.getString(1) ) );
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return lansco;
    }

anne_sco_item.wml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="20px" >

    <TextView
        android:id="@+id/label_ansco"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ansco"
        android:textColor="@color/font_color"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/an_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/label_ansco"
        android:layout_marginLeft="35dp"
        android:textColor="@color/font_color" />

</RelativeLayout>

annee_sco_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="4px"
    android:background="@drawable/background1" >

    <include
        android:id="@+id/menuAn"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        layout="@layout/menu_an" />

    <ListView
        android:id="@+id/list_ansco"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:dividerHeight="4dip"
        android:divider="@drawable/list_divider_bl"
        android:footerDividersEnabled="false"
        android:layout_below="@id/menuAn"
        />

</RelativeLayout>

在我的代碼中一切都正確。 顏色除外。 實際上, @drawable/background1巫婆是我列表的背景顏色與@color/font_color巫婆是我的列表項的顏色相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM