質問

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?

役に立ちましたか?

解決

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);
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top