Android SearchView search item from list view

315 views Asked by At

Hello friends I am new in android . I have problem in item searching from list view please help me.

//get information from database code

public Cursor getFood(String dish_name,SQLiteDatabase sqLiteDatabase)
{
    String[] projections = { Food.NewDishInfo.DISH_QUANTITY, Food.NewDishInfo.DISH_CALORIE, Food.NewDishInfo.DISH_FAT,
            Food.NewDishInfo.DISH_PROTEIN, Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    String selection = Food.NewDishInfo.DISH_NAME+" LIKE ?";
    String[] selection_args = {dish_name};
    Cursor cursor = sqLiteDatabase.query(Food.NewDishInfo.TABLE_NAME,projections,selection,selection_args,null,null,null);

    return cursor;

}

//this is my list data activity in which list view code are here

public class DataListActivity extends Activity {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    FoodDbHelper foodDbHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;

    ListDataAdapter dataAdapter = null;
    Button button;
    DataProvider dataProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.data_list_layout);

        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

                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 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, vitamins;
                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);
                vitamins = cursor.getString(6);

                DataProvider dataProvider = new DataProvider(name, quantity,
                        calorie, fat, protein, sugar, vitamins, 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 name = (String) ((TextView) view
                        .findViewById(R.id.text_dish_name)).getText();
                String quantity = (String) ((TextView) view
                        .findViewById(R.id.text_dish_quantity)).getText();
                String calorie = (String) ((TextView) view
                        .findViewById(R.id.text_dish_calorie)).getText();
                String fat = (String) ((TextView) view
                        .findViewById(R.id.text_dish_fat)).getText();
                String protein = (String) ((TextView) view
                        .findViewById(R.id.text_dish_protein)).getText();
                String sugar = (String) ((TextView) view
                        .findViewById(R.id.text_dish_sugar)).getText();
                String vitamins = (String) ((TextView) view
                        .findViewById(R.id.text_dish_vitamins)).getText();

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

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

                Intent intent = new Intent(getApplicationContext(),
                        Detail.class);
                intent.putExtra("Dish name", name);
                intent.putExtra("Dish quantity", quantity);
                intent.putExtra("Dish calorie", calorie);
                intent.putExtra("Dish fat", fat);
                intent.putExtra("Dish protein", protein);
                intent.putExtra("Dish sugar", sugar);

                intent.putExtra("Dish vitamins", vitamins);

                startActivity(intent);

            }

        });
    }

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

}
1

There are 1 answers

0
Dalpatsinh Chauhan On

Edittext of Search:

editSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable t) {

            filter(t.toString().trim());
        }
    });

Filter Method:

void filter(String text) {
    ArrayList<DataItem> filterdNames = new ArrayList<>();
    for (DataItem test_datalist : arrayList) {
        if (test_datalist.getName().toLowerCase().contains(text.toLowerCase())) 
      {
            filterdNames.add(test_datalist);
        }
    }
    adapter.updateList(filterdNames);
}

Update Method in the Adapter:

public void updateList(ArrayList<DataItem> arrayList11) {
    this.arrayList = arrayList11;
    notifyDataSetChanged();
}