我正在尝试创建一个应用程序,其中我使用现有数据库中的一些产品填充我的listView,到目前为止一直很好:)
现在我在每个产品行上都有一个复选框,当它被点击时,我想获得对该产品的引用。
我是新手,请帮助我。这是代码:
public class NewListPage extends MainAppPage {
protected TextView productName;
protected TextView productPrice;
protected CheckBox checkBox;
protected Database db;
public ListView listView;
public Context context;
protected boolean isChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.existing_product_listview);
db = new Database(this);
productName = (TextView) findViewById(R.id.new_list_productName);
productPrice = (TextView) findViewById(R.id.new_list_productPrice);
listView = (ListView) findViewById(R.id.existing_product_listView);
checkBox = (CheckBox) findViewById(R.id.new_list_productChecked);
populateListView();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_main_saveItem) {
/* WHEN I CLICK ON THE SAVE ITEM I WANT TO GET THE REFERENCE TO THE PRODUCT THAT IS CHECKED HERE*/
}
return super.onOptionsItemSelected(item);
}
private void populateListView(){
//get all rows from DB;
Cursor cursor = db.getAllRows();
// get the product from the product table & price from price table
String[] from = new String[] {Database.PRODUCT, Database.PRICE};
int[] to = new int[] {R.id.new_list_productName, R.id.new_list_productPrice};
SimpleCursorAdapter cursorAdapter;
cursorAdapter = new SimpleCursorAdapter(this,R.layout.new_list_page, cursor, from, to,0);
ListView listView = (ListView) findViewById(R.id.existing_product_listView);
listView.setAdapter(cursorAdapter);
cursorAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
勾选复选框,将您的项目详细信息添加到ArrayList> arr_main = new ArrayList>();
带有id作为参考的在arraylist中插入和删除产品。
将最终的arraylist传递给另一个列表视图,以填充所选项目的列表视图。