我有一个这样的数据库:table pepak,table category,table subcategory 我只想点击其中一个pepak.name来显示类别 但是现在如果我点击pepak.name中的一个,我的应用程序遇到错误并停止了。
这是我的代码:
Menu.java
public class Menu extends ListActivity{
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = db.rawQuery("SELECT _id, name FROM pepak", null);
adapter = new SimpleCursorAdapter(
this,
R.layout.pepaklist,
cursor,
new String[] {"name"},
new int[] {R.id.name});
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, PepakCats.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("PEPAK_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.coll_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.about:
Intent i = new Intent("com.pepakbahasajawa.ABOUT");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
PepakCats.java
public class PepakCats extends ListActivity {
protected Cursor cursor;
protected ListAdapter adapter;
protected int pepakId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
pepakId = getIntent().getIntExtra("PEPAK_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery(
"SELECT _id, pepakId, catname FROM category WHERE pepakId = ?",
new String[]{""+pepakId}
);
adapter = new SimpleCursorAdapter(
this,
R.layout.category_list,
cursor,
new String[] {"catname"},
new int[] {R.id.catname}
);
setListAdapter(adapter);
}
}
点击后如何显示数据库中的列表视图数据?
答案 0 :(得分:1)
直接尝试使用id:
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, PepakCats.class);
intent.putExtra("PEPAK_ID", id);
startActivity(intent);
}