如何在listview中获取特定类别的数据?

时间:2015-10-13 06:49:36

标签: android android-listview android-bundle

我的MainActivitylistview个类别,如果我点击listview中的特定类别,则需要重定向到另一个活动,该活动需要详细说明特别类别。

例如:如果我在Mainactivity中选择食物,我想重定向到活动想要有预算食物量的其他活动。

public class addbudget extends ActionBarActivity implements View.OnClickListener{

    SimpleCursorAdapter adapter;
    DBhelper helper;
    SQLiteDatabase db;
    EditText txtBudget;
    TextView txr;
    ListView rldlist,list;
    Button btn66;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addbudget);

        btn66=(Button)findViewById(R.id.btn_addBudget);
        btn66.setOnClickListener(this);
        helper=new DBhelper(addbudget.this);
        txr=(TextView)findViewById(R.id.addbud);

        txtBudget=(EditText)findViewById(R.id.etBudget);

        rldlist = (ListView) findViewById(R.id.rldlist);

        list = (ListView) findViewById(R.id.list);

        Bundle data_from_list= getIntent().getExtras();
        String value_in_tv= data_from_list.getString("passed data key");
        txr.setText(value_in_tv);

        fetchData2();

    }



    private void clearfield(){
        txtBudget.setText("");

    }


    public void onClick(View v) {
        if (btn66 == v) {
            ContentValues value = new ContentValues();
            value.put(DBhelper.Amount, txtBudget.getText().toString());
            value.put(DBhelper.Description,txr.getText().toString());

            if (txtBudget.length() == 0) {
                txtBudget.requestFocus();
                txtBudget.setError("Field Cannot Be Empty");
            } else {

                db = helper.getWritableDatabase();
                db.insert(DBhelper.TABLE2, null, value);
                db.close();
                clearfield();
                Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show();

fetchData2();
            }
        }
    }



    private void fetchData2() {
        db = helper.getReadableDatabase();
        Cursor c = db.query(DBhelper.TABLE2, null, null, null, null, null, null);
        adapter = new SimpleCursorAdapter(
                this,
                R.layout.row2,
                c,
                new String[]{DBhelper.Amount},
                new int[]{R.id.lbl});
        list.setAdapter(adapter);
    }

}

这就是我从数据库中获取listview数据的方法。这是我从数据库中获取金额。

如何更改fetchdata方法以获取特定类别的BudgetAmount?(我使用bundle从MainActivity的列表视图中获取类别的名称)

1 个答案:

答案 0 :(得分:0)

您可以使用sql语法:

String sql = "select * from DBhelper.TABLE2 where category_name_column = " + category";

Cursor c = db.rawQuery(sql.toString(), null);

或者如果你想使用params:

String whereClause = "your_category_column = ?";

String[] whereArgs = new String[] {
    "category_name"
};

Cursor c = db.query(DBhelper.TABLE2, null, whereClause, whereArgs, null, null, null);

所以你的方法可以这样:

private void fetchData2(String category) {

         db = helper.getReadableDatabase();

         String whereClause = "your_category_column = ?";

         String[] whereArgs = new String[] {"category"};

         Cursor c = db.query(DBhelper.TABLE2, null, whereClause, whereArgs, null, null, null);
         adapter = new SimpleCursorAdapter(
                this,
                R.layout.row2,
                c,
                new String[]{DBhelper.Amount},
                new int[]{R.id.lbl});
         list.setAdapter(adapter);
    }