Bhavin Chauhan
Bhavin Chauhan

Reputation: 2010

android Listview Seleted Event

I have created sample apps with 2 ListView. I have an issue in listview selected event. I want to bind data into second ListView when user clicked on first listview. I have extends 'Activity' in my Activity class. I also want to change my current list item with different color.

listviewCatagory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    arg1.setSelected(true);
                    TextView lblCatagory = (TextView) findViewById(R.id.lblCatagory);
                    lblCatagory.setTextColor(getResources().getColor(
                            R.color.White));
                    //Log.d("Selected Item", Integer.toString(arg2));

                    Toast.makeText(getApplicationContext(), Integer.toString(arg2), Toast.LENGTH_SHORT).show();
                }

                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }

            });

Upvotes: 0

Views: 84

Answers (2)

Chintan Rathod
Chintan Rathod

Reputation: 26034

Use drawable to provide selection color and other stuffs.

item_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_focused="true">
    <shape>
        <solid android:color="#FFFFFF" />
    </shape>
</item>
<item>
    <shape>
        <solid android:color="#666666" />
    </shape>
</item>

</selector>

and use this inside your custom layout as follow.

Suppose your custom layout has RelativeLayout then

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/item_selected">

Upvotes: 1

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

Use onItemClickListener for listview. onItemSelectedListener is used for spinners

Upvotes: 1

Related Questions