Search code examples
androidandroid-layoutandroid-listview

Unselect the ListView Item onClick in android


What is happening i have a listview on which i putting background color change on selection.As well as by default i am putting the first listview item selected as

public class OneWayFlightResult extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;


public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
     return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row, null);
    TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
    TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
    TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
    ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);

    HashMap<String, String> flight = new HashMap<String, String>();
    flight = data.get(position);

    flightTime.setText(flight.get(TestActivity.FlightTime));
    flightCompanyName.setText(TestActivity.FlightCompanyName);
    flightNumber.setText(TestActivity.FlightNumber);

    if(position == 0){

        vi.setBackgroundResource(R.drawable.selection_effect);
        vi.setSelected(true);

    }
    return vi;
}

This is XML file i am using in this selection_effect.xml

<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="#00a7eb" />
    </shape>
</item>
</selector>

So by default this the first list view is selected.Now when the user select the another listview item the first one remains selected and the other one also got the same effect.So how could change the effect on click of the listview item dynamically .Means by default first item comes up selected when the user selects other item other one get selected the effect from the default one get removed


Solution

  • I got solution for your problem. Do as following.

    1) open your main layout file where ListView you have created.
    Add android:choiceMode="singleChoice". This will look like below.

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:choiceMode="singleChoice" >
    </ListView>
    

    2) Open your list_item.xml layout file. In which, to your root view, add android:background="?android:attr/activatedBackgroundIndicator". In my sample project, its look like below.

    <?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:background="?android:attr/activatedBackgroundIndicator"
        android:orientation="vertical" >
    
        //your views
    
    </LinearLayout>
    

    3) Open your activity file. After setting adapter to your ListView, add list.setSelector(R.drawable.selection_effect);. This will look like below.

    ListView ls = (ListView) findViewById(R.id.listView1);
    ListAdapter adapter = new ListAdapter(this, data);
    ls.setAdapter(adapter);
    ls.setSelector(R.drawable.selection_effect);
    

    Here, selection_effect is drawable file which you have created in drawable directory. I tested my code. Which is working fine.

    4) To select first view by default, remove your code in BaseAdapter and put following code after completing 3rd step.

    ls.setItemChecked(0,true);
    

    You need to put it after above code like below.

    ListAdapter adapter = new ListAdapter(data);
    ls.setAdapter(adapter);
    ls.setSelector(R.drawable.selection_effect);
    ls.setItemChecked(0, true);
    

    Explanation

    ls.setSelector(R.drawable.selection_effect);

    This will select row item based on selector you have defined in drawable directory.

    ls.setItemChecked(0, true);

    This will select first item by default at first time run. After you can select other items by clicking on them.