我有一个listview,显示两个sqlite表的值。首先,我尝试使用ArrayAdapter来查看它们,并显示所有值。但他们看起来很乱,所以我尝试使用一个简单的适配器。我的代码是。
for(int i=0; i<= promo.size();i++){
ProductModulePromo b =dataSource.getproductmodulepromo(moduleid);
productmoduledesc = b.getProductmoduledesc().toString();
producttype = b.getProducttype().toString();
productpromo = b.getproductpromotion().toString();
map1.put("three",productpromo);
map1.put("two",producttype);
map1.put("one",productmoduledesc);
Globals.promo.add(map1);}
促销是my arraylist<productmodulepromo>()
。当我尝试编译这些代码。显示的所有值都相同。所以我尝试在datasource.java上使用此代码进行更改,并在formpromo.java上使用这些arraylist。这是我的DBDatasource.java的代码:
public ArrayList<HashMap<String, String>> getproduct1() {
ArrayList<HashMap<String, String>> promo = new ArrayList<HashMap<String, String>>();
database = dbHelper.getReadableDatabase();
Cursor cursor = database.rawQuery("select productmodule.productmoduledescription, productdetail.producttype, productdetail.productpromotion from productmodule, productdetail where productmodule.productmoduleid=productdetail.productmoduleid", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
productmoduledesc=cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
producttype = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_TYPE));
productpromo = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_PROMOTION));
map1.put("one",productmoduledesc);
map1.put("two",producttype);
map1.put("three",productpromo);
promo.add(map1);
} while (cursor.moveToNext());
}
cursor.close();
return promo;
}
这是我的Formpromo.java:
dataSource = new DBDataSource(this);
dataSource.open();
promo = dataSource.getproduct1();
adapter = new SimpleAdapter(FormPromo.this, promo, R.layout.item_to,
new String[] {"one","two","three"},
new int[] {R.id.edtbrandto,R.id.edtqtyto,R.id.edtharga});
setListAdapter(adapter);
final ListView listpromo = (ListView) findViewById(android.R.id.list);
setListAdapter(adapter);
我在Java.Lang.NullpointerException
上收到map1.put("one",productmoduledesc);
错误。我不知道如何修复它们。感谢帮助。任何帮助将不胜感激。
答案 0 :(得分:0)
好像你从未设置map1,将它添加到循环的开头,因为否则你会覆盖旧值
像这样...
do {
map1 = newHashMap<String, String>(); // add
productmoduledesc=cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
producttype = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_TYPE));
productpromo = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_PROMOTION));
map1.put("one",productmoduledesc);
map1.put("two",producttype);
map1.put("three",productpromo);
promo.add(map1);
} while (cursor.moveToNext());
...