Stephen Stephen - 20 days ago 6
Android Question

Click the list view item to show an another list view in separate activity


  • I created the listview.That was shown below.

  • you can see that
    listview_array[]
    contains Test,Category1
    and Category2.



ListActivity.java:

package com.steph.listview;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListActivity extends Activity {
private ListView lv;
private String listview_array[] = { "Test", "Category1", "Category2", "Category3"};

public void onCreate(Bundle icicle)

{
super.onCreate(icicle);
setContentView(R.layout.list1);
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {


}


});
}
}


list1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


Output:

enter image description here


  • My problem is if I click the
    Test
    in ListView.Following names have to shown in the new ListView: Test1,Test2 and Test3.

  • Then If I click the
    Category1
    in ListView.Following names have to shown in the new ListView: Video1,Video2 and Video3.

  • Then If I click the
    Category2
    in ListView.Following names have to
    shown in the new ListView: Audio1,Audio2 and Audio3.

  • I doesn't know what to do.I can create an another three seperate
    Classes for Test,Category1 and Category2.And then I need to use
    intent.

  • But my problem is how can I do it in single class.Help me if you have any suggestion.Thank You.


Answer

Altough i have no idea why you want to load a new instance of this activity when you could re-use it, here's a working example of what you want:

package com.steph.listview;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by Simon on 14.7.7.
 */

public class ListActivity extends Activity {
    private ListView lv;
    private String[] groupArray = {"Category1", "Category2", "Category3"};
    private String[][] childArray = {{"Test1", "Test2", "Test3"},
            {"Video1", "Video2", "Video3"}, {"Audio1", "Audio2", "Audio3"}};

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.list1);
        lv = (ListView) findViewById(R.id.listview);

        String[] data = getIntent().getStringArrayExtra("strArray");
        AdapterView.OnItemClickListener clickListener = null;

        // If no data received means this is the first activity
        if (data == null) {
            data = groupArray;
            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(ListActivity.this, ListActivity.class);
                    intent.putExtra("strArray", childArray[position]);
                    startActivity(intent);
                }
            };
        }

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);

        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(clickListener);

    }
}