TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Android ListView Clickable Items with MySQL

Here is my scenario:

I have two MySQL tables:

Categories (columns: id, category) Items (columns: id, item, category_id)

Android part:

I have an app that opens to a ListView populated from the table called "Categories". It does this just fine. What I would like to do is make each category item in the ListView clickable to open to a new ListView populated with items from the "Items" table (where each getPosition() = category_Id).

I can figure out that last bit of it with the PHP query. I am not sure how to make the Android part work. Do I reuse the same ListView? Does each click open a new activity?

Can someone show me a very simple example (even in psudo-code) of how this can work?

Upvotes: 0

Views: 1689

Answers (1)

Matteo Cardellini
Matteo Cardellini

Reputation: 896

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {

            String category = YOUR_ArrayList.get((int) id);
            Bundle d = new Bundle();
            d.putString("category", category);
                            Intent itemIntent = new Intent(this, ItemActivity.class);
            prodottiIntent.putExtras(d);
            startActivity(itemIntent);
        }
    });

Now in your itemActivity you should only take your bundle d (category)

            Bundle d = getIntent().getExtras();
    String category = d.getString("category");

And now with your string "category" you can search in your database and display all the items that have the category that you pressed

Upvotes: 1

Related Questions