简体   繁体   English

Android如何在SherlockFragment中转换sherlockActivity

[英]Android How to convert sherlockActivity in SherlockFragment

Before I used SherlockActivity distinct from my SherlockFragmentActivity in which I included a DrawerMenu for all my treatments and was walking correctly. 在使用与SherlockActivity不同的SherlockFragmentActivity之前,我在其中包括了用于所有治疗的DrawerMenu并可以正确行走。 But I decided to change SherlockActivity in SherlockFragment in order to benefit from DrawerMenu on all views. 但是我决定更改SherlockActivity中的SherlockFragment ,以便从所有视图上的DrawerMenu受益。 I tried for my first Activity in Fragment and it appear but the list containing the data from the database does not appear. 我尝试了Fragment第一个Activity ,但它没有出现,但是包含数据库中数据的列表没有出现。 Help me please. 请帮帮我。


My Adapter Class 我的适配器类

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();
    }


}

My New Fragment 我的新片段

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);
    }

}

A part of My DatabaseHelper 我的数据库助手的一部分

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 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 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>

Everything was right in my code. 在我的代码中一切都正确。 Except the color. 颜色除外。 In fact, @drawable/background1 witch is the background of my List had the same color than @color/font_color witch is the color of my list Items. 实际上, @drawable/background1巫婆是我列表的背景颜色与@color/font_color巫婆是我的列表项的颜色相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM