Hey everyone!
I have a problem in my android app. I am trying to do a shopping list app. I tried to do 2 shopping list in my app,each of them with its items. When I what to see the items of an list I use a SimpleCursorAdapter.The problem is that if I want to see the items of second list I see its items ,and the first one also. What should I do? All the titles of lists are stored in a table and all the items are stored in an other table. Here is my class All ideas are welcomed. Thank you.My code:
开发者_JAVA百科public class Produse extends Activity{
TextView selection;
String titlelist;
String id;
private ProductDbAdapter prod;
private ListDbAdapter db;
private SimpleCursorAdapter adapter;
private ListView lv;
private static final int MENU_ADD = Menu.FIRST;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.produse);
selection=(TextView)findViewById(R.id.text);
lv=(ListView)findViewById(R.id.listprod);
Bundle bundle = getIntent().getExtras();
titlelist = bundle.getString("param1");
id=bundle.getString("param2");
selection.setText(titlelist);
db=new ListDbAdapter(this);
db.open();
prod=new ProductDbAdapter(this);
prod.open();
populate();
prod.close();
db.close();
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ADD, 0, "Add Item");
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case MENU_ADD:
additem();
return true;
}
return false;
}
public void additem() {
Intent j= new Intent(Produse.this, List2.class);
j.putExtra("param2", id);
startActivity(j);
}
private void populate(){
Cursor c=prod.fetchAll();
startManagingCursor(c);
if (c.moveToFirst()){
do{
if (id.toString().equals(c.getString(1)))
adapter=new SimpleCursorAdapter(this,R.layout.listproduct,c,
new String[] {ProductDbAdapter.KEY_ITEM,ProductDbAdapter.KEY_QUANTITY,ProductDbAdapter.KEY_UNITS},
new int[] {R.id.prod1,R.id.prod2,R.id.prod3});
}while (c.moveToNext());
lv.setAdapter(adapter);
}
}
}
Hmmm something that looks definitely wrong is that you're reassigning your adapter inside your loop. I think the POINT of cursor adapters is that you don't have to iterate over your cursor, that's the Adapter's job. So, I think you need to change your populate code to something like this:
private void populate(){
Cursor c=prod.fetchAll();
startManagingCursor(c);
adapter=new SimpleCursorAdapter(this,R.layout.listproduct,c,
new String[] {ProductDbAdapter.KEY_ITEM,ProductDbAdapter.KEY_QUANTITY,ProductDbAdapter.KEY_UNITS},
new int[] {R.id.prod1,R.id.prod2,R.id.prod3});
lv.setAdapter(adapter);
}
精彩评论