Android:onclick后显示数据库中的listview数据

发布于 2024-12-08 02:15:55 字数 2647 浏览 0 评论 0原文

我有一个这样的数据库:表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-12-15 02:15:55

尝试直接使用 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);
        }

Try using the id directly:

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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文