我正在使用自定义光标适配器从数据库读取到列表视图。列表视图显示了数据库中每个条目的一些详细信息。当我单击一个列表项时,我想打开一个详细信息页面,该页面显示该记录的其他详细信息,这些详细信息未显示在列表视图中。我想不通。
我附上了 MainActivity 中的光标创建和适配器创建的代码片段。我还有一个单独的 customAdapter 类和一个用于详细信息页面的类。
主要活动:
public class MainActivity extends ListActivity {
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
}
private void fillList() {
ListView lv = getListView();
Cursor c = db.getData();
startManagingCursor(c);
CustomCursorAdapter adapter = new CustomCursorAdapter(getApplicationContext(), c);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent k = new Intent(MainActivity.this, ProductDetails.class);
}
});
}
}
自定义光标适配器:
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_entry, parent, false);
bindView(v, context, cursor);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtvw1 = (TextView)view.findViewById(R.id.prodcode_entry);
txtvw1.setText(cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODCODE)));
TextView txtvw2 = (TextView)view.findViewById(R.id.prodtype_entry);
String ptype = (cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODTYPE)));
txtvw2.setText(ptype.substring(0, 1));
TextView txtvw3 = (TextView)view.findViewById(R.id.prodcat_entry);
String cat = (cursor.getString(cursor.getColumnIndex(ProdDB.PRODCAT)));
txtvw3.setText(cat.substring(0, 2));
TextView txtvw4 = (TextView)view.findViewById(R.id.prodcost_entry);
float price = (cursor.getFloat(cursor.getColumnIndex(ProdDB.PRODPRICE)));
txtvw4.setText(Float.toString(price));
TextView txtvw5 = (TextView)view.findViewById(R.id.prodSup_entry);
txtvw5.setText(cursor.getString(cursor.getColumnIndex(ProdDB.SUPPLIER)));
}
}
产品详情
public class ProductDetails extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_details);
}
}
db.getData() - 来自 ProdDB
public Cursor getData() {
String[] columns = new String[] {KEY_ROWID,
KEY_PRODCODE,
KEY_PRODNAME,
KEY_PRODTYPE,
KEY_PRODCAT,
KEY_PRODPRICE,
KEY_PRODRRP,
KEY_PRODSUPPLIER,
KEY_NOTES};
return db.query(DB_TABLE, columns, null, null, null, null, null);
}