android filter listview with edittext item come from database

180 views Asked by At

i have a listview . in which item come from database listview has also checkbox . listview work fine . now i put a edittext on listview i wana filter my listview i try a lot . i do some thing like this.i use custom adapter.

this is my list data code

    public class DataListActivity extends Activity  {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    FoodDbHelper foodDbHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    private Button button1;
    ListDataAdapter dataAdapter = null;
    Button button;
    DataProvider dataProvider;
    ArrayList<HashMap<String, String>> namessList;
    EditText inputSearch;
    String search_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.data_list_layout);


        inputSearch = (EditText)findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changes the Text
                listDataAdapter.getFilter().filter(cs);

            }

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


            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });



    button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(DataListActivity.this, button1);
                //Inflating the Popup using xml file
                popup.getMenuInflater()
                        .inflate(R.menu.popup_menu, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {

                        AlertDialog.Builder a_builder = new AlertDialog.Builder(DataListActivity.this);
                        a_builder.setMessage("Do You Want To Close This App !!!")
                                .setCancelable(false)
                                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int id) {
                                        Intent intent = new Intent(getApplicationContext(), MainMenu.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        intent.putExtra("EXIT", true);
                                        startActivity(intent);
                                        //finish();
                                    }
                                })
                                .setNegativeButton("No" , new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                });
                        AlertDialog alert = a_builder.create();
                        alert.setTitle("Alert !!!");
                        alert.show();
                        // Toast.makeText(
                        //   MainMenu.this,
                        //   "You Clicked : " + item.getTitle(),
                        //  Toast.LENGTH_SHORT
                        // ).show();
                        return true;
                    }
                });

                popup.show(); //showing popup menu
            }
        });

        listView = (ListView) findViewById(R.id.list_View);
        listDataAdapter = new ListDataAdapter(getApplicationContext(),
                R.layout.row_layout) {
            @Override
            protected void showCheckedButton(int position, boolean value) {
                // TODO Auto-generated method stub

                final DataProvider item = (DataProvider) listDataAdapter
                        .getItem(position);
                Log.i("", "");

                item.setSelected(value);

                Button myButton = (Button) findViewById(R.id.findSelected);
                myButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        StringBuffer responseText = new StringBuffer();
                        responseText
                               .append("The following dishes were selected...\n");

                        ArrayList<DataProvider> list = listDataAdapter
                                .getSelectedIndexes();

                        int sum = 0;
                        for (int i = 0; i < list.size(); i++) {
                            DataProvider dataProvider = list.get(i);

                            sum = sum + dataProvider.getCalorie();

                            responseText.append("\n" + dataProvider.getName()
                                    + " : " + dataProvider.getCalorie()
                                    + " kcal"
                                    );

                        }

                        Toast.makeText(getApplicationContext(), ""+responseText+"\n"+"................................."
                                       +"\n"+"Total Calories In Your Menu Is : " +sum,
                                Toast.LENGTH_LONG).show();

                    }
                });


            }

        };
        listView.setAdapter(listDataAdapter);
        foodDbHelper = new FoodDbHelper(getApplicationContext());
        sqLiteDatabase = foodDbHelper.getReadableDatabase();
        cursor = foodDbHelper.getInformations(sqLiteDatabase);
        if (cursor.moveToFirst()) {
            do {
                String name, quantity, fat, protein, sugar, carbohydrates;
                boolean selected = false;
                String names = null;
                Integer calorie;

                name = cursor.getString(0);
                quantity = cursor.getString(1);
                calorie = Integer.valueOf(cursor.getString(2));
                fat = cursor.getString(3);
                protein = cursor.getString(4);
                sugar = cursor.getString(5);
                carbohydrates = cursor.getString(6);

                DataProvider dataProvider = new DataProvider(name, quantity,
                        calorie, fat, protein, sugar, carbohydrates, names, selected);

                listDataAdapter.add(dataProvider);

            } while (cursor.moveToNext());
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


                String.valueOf(parent.getItemAtPosition(position));

                Toast.makeText(getApplicationContext(),
                        "dish name is : " + name, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getApplicationContext(),
                        Detail.class);

            }

        });
    }

    public void gobackk(View view) {
        Intent intent = new Intent(this, MainMenu.class);
        startActivity(intent);
    }




}

this is custom adapter code

    public abstract class ListDataAdapter extends ArrayAdapter {

    List list = new ArrayList();
    boolean index[];

    public ListDataAdapter(Context context, int resource) {

        super(context, resource);
        index = new boolean[list.size()];

    }

    static class LayoutHandler {
        TextView name, quantity, calorie, fat, protein, sugar, carbohydrates;
        CheckBox names;

    }

    @Override
    public void add(Object object) {

        super.add(object);
        list.add(object);
        index = new boolean[list.size()];

    }

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

    }

    @Override
    public Object getItem(int position) {
        return list.get(position);

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout, parent, false);
            layoutHandler = new LayoutHandler();
            layoutHandler.name = (TextView) row
                    .findViewById(R.id.text_dish_name);
            layoutHandler.quantity = (TextView) row
                    .findViewById(R.id.text_dish_quantity);
            layoutHandler.calorie = (TextView) row
                    .findViewById(R.id.text_dish_calorie);
            layoutHandler.fat = (TextView) row.findViewById(R.id.text_dish_fat);
            layoutHandler.protein = (TextView) row
                    .findViewById(R.id.text_dish_protein);
            layoutHandler.sugar = (TextView) row
                    .findViewById(R.id.text_dish_sugar);
            layoutHandler.carbohydrates = (TextView) row
                    .findViewById(R.id.text_dish_carbohydrates);
            layoutHandler.names = (CheckBox) row.findViewById(R.id.checkBox1);

            row.setTag(layoutHandler);

        } else {
            layoutHandler = (LayoutHandler) row.getTag();

        }
        DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.name.setText(dataProvider.getName());
        layoutHandler.quantity.setText(dataProvider.getQuantity());
        layoutHandler.calorie
                .setText(String.valueOf(dataProvider.getCalorie()));
        layoutHandler.fat.setText(dataProvider.getFat());
        layoutHandler.protein.setText(dataProvider.getProtein());
        layoutHandler.sugar.setText(dataProvider.getSugar());
        layoutHandler.carbohydrates.setText(dataProvider.getCarbohydrates());

        //layoutHandler.names.setChecked(dataProvider.isSelected());
        layoutHandler.names.setTag(position);
        layoutHandler.names.setChecked(index[position]);
        layoutHandler.names
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        // TODO Auto-generated method stub
                        int pos = (Integer) buttonView.getTag();
                        index[pos] = isChecked;
                        showCheckedButton(position, isChecked);

                    }
                });


        return row;
    }

    public ArrayList<DataProvider> getSelectedIndexes() {
        int size = list.size();
        ArrayList<DataProvider> selectedItems = new ArrayList<DataProvider>();
        for (int i = 0; i < size; i++) {
            DataProvider cItem = (DataProvider) list.get(i);
            if (index[i]) {
                selectedItems.add(cItem);
            }
        }

        return selectedItems;

    }

    protected abstract void showCheckedButton(int position, boolean value);

}
0

There are 0 answers