quky
quky

Reputation: 13

listview checked

hey guys i am trying to set the first item on the listview checked but the code is not working.

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListView1 extends ListActivity {
@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       String[] names = new String[] { "Android", "Windows7", "Symbian", "iPhone",
              "Android", "Windows7", "Symbian", "iPhone",
              "Android", "Windows7", "Symbian", "iPhone" };
         setListAdapter(new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_single_choice,
                           android.R.id.text1, names));
         ListView listView = getListView();
         listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
         listView.setSelection(0);
         listView.setSelected(true);

   }}

picture of the activity i want the first radio button been checked any help would be appreciated Best regards HP

Upvotes: 0

Views: 2564

Answers (4)

user1439139
user1439139

Reputation:

In case you will stick with setItemChecked(int, boolean)

By referring source code I found following:

 public void setItemChecked(int position, boolean value) {
       if (mChoiceMode == CHOICE_MODE_NONE) {
           return;
       }

        if (mChoiceMode == CHOICE_MODE_MULTIPLE) {
            mCheckStates.put(position, value);
       } else {
            boolean oldValue = mCheckStates.get(position, false);
            mCheckStates.clear();
            if (!oldValue) {
                mCheckStates.put(position, true);
            }
        }

        // Do not generate a data change while we are in the layout phase
        if (!mInLayout && !mBlockLayoutRequests) {
            mDataChanged = true;
            rememberSyncState();
            requestLayout();
        }
    }

The most important line here is

 // Do not generate a data change while we are in the layout phase

This means that even though data behind checking items is changed, drawable will not change

Mind it

Peace...

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setSelection(0);
listView.setSelected(true);

replace the above lines with below:::use

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemChecked(0,true);

Upvotes: 5

Simon
Simon

Reputation: 14472

setSelection does exactly that - it selects the item in the listview scroll - i.e. gives it focus.

http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)

try this:

listview.setItemChecked(0,true)

Upvotes: 1

FUBUs
FUBUs

Reputation: 617

You need to do your own adapter (extends of BaseAdapter) for that. and in the getView you can do that easily :

if(position == 0)layout.findViewById("mycheckbox").setchecked(true)

Upvotes: 0

Related Questions