0

hey everyone i hope you can help and thanks for taking the time too look

im trying to use multilpe listviews in the one activity with a simple adapter and a hashmap

here is some of my code....

final ArrayList<HashMap<String,String>> listGeneral = new ArrayList<HashMap<String,String>>();

before the onCreate then is in the onCreate

setContentView(R.layout.custom_list_view);

        SimpleAdapter adapter1 = new SimpleAdapter(
                this,
                listGeneral,
                R.layout.custom_row_view,
                new String[] {"catGeneral","score1"},
                new int[] {R.id.text1,R.id.text2}
        );

setList1();
 setListAdapter(adapter1);

setList1

public void setList1(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catGeneral","General Knowledge Lv1");
        temp.put("score1", level3);
        listGeneral.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catGeneral","General Knowledge Lv2");
        temp1.put("score1", level4);
        listGeneral.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catGeneral","General Knowledge Lv3");
        temp2.put("score1", level2);
        listGeneral.add(temp2);
}

and itemListener

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);


        if (position == 0){
        Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
        }
        else if (position == 1){
            Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
            }
        else if (position == 2){
            Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
            }
        else if (position == 3){
            Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
            }
        else if (position == 4){
            Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
            }
       }

and xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000fff"
        android:layout_weight="2"
        android:drawSelectorOnTop="false">
    </ListView>

    <TextView android:id="@id/android:empty"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="#FFff00"
        android:text="No data"
    />

</LinearLayout>

all of this works for one listview. But when i add a second list

final ArrayList<HashMap<String,String>> listPopCulture = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter2 = new SimpleAdapter(
                this,
                listPopCulture,
                R.layout.custom_row_view,
                new String[] {"catPopCulture","score2"},
                new int[] {R.id.text1,R.id.text2}
        );


setList2();
setListAdapter(adapter2);

public void setList2(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catPopCulture","The 60's");
        temp.put("score2", level3);
        listPopCulture.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catPopCulture","The 70's");
        temp1.put("score2", level4);
        listPopCulture.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catPopCulture","The 80's");
        temp2.put("score2", level2);
        listPopCulture.add(temp2);
}

im not sure how to do the second itemListener for the second list, aswell as how to call the second list, as i thought the first list is called by android:id="@id/android:list" everything i have tried hasnt worked....

is it because simple adapter isnt able to be set on more then one list per activity because of its id ? and how do i declare the second or more listviews in the one xml file. In the end i want a layout with a scrollview that holds a textview then listview, then another textview and another listview....any help would be awsome

here is edited answer almost their. works but i need onItemListener for list2 and list3

setContentView(R.layout.custom_list_view);

        ListView list2 = (ListView)findViewById(R.id.list2);
        ListView list3 = (ListView)findViewById(R.id.list3);



        SimpleAdapter adapter1 = new SimpleAdapter(
                this,
                listGeneral,
                R.layout.custom_row_view,
                new String[] {"catGeneral","score1"},
                new int[] {R.id.text1,R.id.text2}
        );
        //list1.setAdapter(adapter1);


        SimpleAdapter adapter2 = new SimpleAdapter(
                this,
                listPopCulture,
                R.layout.custom_row_view,
                new String[] {"catPopCulture","score2"},
                new int[] {R.id.text1,R.id.text2}
        );

        SimpleAdapter adapter3 = new SimpleAdapter(
                this,
                listKids,
                R.layout.custom_row_view,
                new String[] {"catKids","score3"},
                new int[] {R.id.text1,R.id.text2}
        );

        setList1();
        setList2();
        setList3();

                setListAdapter(adapter1);
        list2.setAdapter(adapter2);
        list3.setAdapter(adapter3);

        //end oncreate  
    }

    public void setList1(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catGeneral","General Knowledge Lv1");
        temp.put("score1", level3);
        listGeneral.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catGeneral","General Knowledge Lv2");
        temp1.put("score1", level4);
        listGeneral.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catGeneral","General Knowledge Lv3");
        temp2.put("score1", level2);
        listGeneral.add(temp2);

        HashMap<String,String> temp3 = new HashMap<String,String>();
        temp3.put("catGeneral","General Knowledge Lv4");
        temp3.put("score1", level3);
        listGeneral.add(temp3);

        HashMap<String,String> temp4 = new HashMap<String,String>();
        temp4.put("catGeneral", "General Knowledge Lv5");
        temp4.put("score1", level1);
        listGeneral.add(temp4);

        HashMap<String,String> temp5 = new HashMap<String,String>();
        temp5.put("catGeneral", "General Knowledge Lv6");
        temp5.put("score1", level1);
        listGeneral.add(temp5);
    }

my working listener for list 1

 //list 1 listener
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            if (position == 0){
            Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
            }
            else if (position == 1){
                Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
                }
            else if (position == 2){
                Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
                }
            else if (position == 3){
                Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
                }
            else if (position == 4){
                Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
                }
           }

ive tried this for list2 onItemClick

list2.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                if (position == 0){
                    Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
                    }
                    else if (position == 1){
                        Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 2){
                        Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 3){
                        Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 4){
                        Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
                        }
            }
        }); 

any ideas on the list2 and list3 onItemClick?

1 Answer 1

1

Put two list in your xml :-

<?xml version="1.0" encoding="utf-8"?>

<ListView android:id="@+id/list1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000fff"
    android:layout_weight="2"
    android:drawSelectorOnTop="false">
</ListView>

<ListView android:id="@+id/list2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000fff"
    android:layout_weight="2"
    android:drawSelectorOnTop="false">
</ListView>

<TextView android:id="@id/android:empty"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFff00"
    android:text="No data"
/>

Now in your main activity, create your adapter like you do above. Get the list references like this :-

ListView list1 = (ListView)findViewById(R.id.list1);
ListView list2 = (ListView)findViewById(R.id.list2);
list1.setAdapter(adapter);
list2.setAdapter(secondadapter);
Sign up to request clarification or add additional context in comments.

3 Comments

when i left android:id="@id/android:list" for the first listview your code worked
but how do i implament the second onItemListener ill repost an edit
You can write hundreds itemclick listener for different listview's just like you have written above for list2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.