2015-10-13 35 views
0

我的MainActivity有一个listview与某些类别,如果我点击我的listview中的特定类别,它需要重定向到另一个活动,需要有该特定类别的详细信息。如何在列表视图中获取特定类别的数据?

例如:如果我选择FOOD in 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); 
    } 

} 

这是怎么了,我取数据从一个database.Here listview我从数据库中获取的量。

我怎样才能改变fetchdata方法来获取特定类别的BudgetAmount(我使用束从MainActivity列表视图得到类别的名称)

+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); 
    } 
相关问题