Android:onclick后显示数据库中的listview数据
我有一个这样的数据库:表pepak,表类别,表子类别 我只想在单击其中一个 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);
}
}
单击后如何显示数据库中的列表视图数据?
I have a database like this: table pepak, table category, table subcategory
I just want to display category if I click one of the pepak.name
But now if I click one of pepak.name, my app faced an error and stopped.
Here is my code:
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);
}
}
How can I display the listview data from the database after the click?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试直接使用 id:
Try using the id directly: