Monday, June 11, 2012

Android:To set an item as selected when the ListView opens

Android:To set an previous selected item as selected when the ListView opens
Problem:
 To set previous selected item as selected when the List View Opens.
Solution:
Make your listview as single choice mode or multible choice mode.
XML Code:
<ListView
              android:id="@+id/_list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:choiceMode="singleChoice"
              >
  </ListView>
Activty Code :

listView.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_single_choice, list_array));

jobList.setItemChecked( here your previous position, true);

1 comment:

  1. It won't work for me.

    final Dialog dia = new Dialog(this);
    dia.setContentView(R.layout.sample);
    dia.setTitle("Select File to import");
    dia.setCancelable(true);

    final ListView list_alert = (ListView) dia
    .findViewById(R.id.listView1TY);

    list_alert.setAdapter(new ArrayAdapter(getApplicationContext(),
    android.R.layout.simple_list_item_multiple_choice,
    Party_list_new));

    list_alert.setItemsCanFocus(true);
    list_alert.setItemChecked(1, true);

    // list_alert.setSaveEnabled(true);

    list_alert.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    // list_alert.onSaveInstanceState();
    list_alert.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView arg0, View arg1, int pos,
    long arg3) {

    }
    });

    please check this.

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...