列表视图中的Android SearchView搜索项

时间:2015-06-12 04:41:09

标签: android database listview searchview

你好朋友我是android新手。我从列表视图中搜索项目有问题请帮帮我。

//从数据库代码中获取信息

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;

}

//这是我的列表视图代码在这里的列表数据活动

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

}

0 个答案:

没有答案