I wrote a small activity which illustrates some crazy behavior I found
while implementing ListView and choices.  Feel free to plug this in
and try it yourselves (and be sure to check logcat as well):

public class TestActivity extends ListActivity {

        public String[] strings = { "What","the","devil","is","happening?" };

        @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.listview);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setAdapter(new
ArrayAdapter<String>(this,R.layout.main,strings));
        getListView().setOnItemClickListener(new OnItemClickListener()
{
                        public void onItemClick(AdapterView<?> arg0, View arg1, 
int arg2,
                                        long arg3) {
                                        Log.d("test","Checked: "+
((TextView)getListView().getChildAt(arg2)).getText().toString());
                                        if (getListView().isItemChecked(arg2))
                                        {
                                                for (int i=0; 
i<getListView().getChildCount(); i++)
                                                {
                                                        if (i==arg2)
                                                                
getListView().getChildAt(i).setBackgroundColor(Color.MAGENTA);
                                                        else
        
getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                                                }
                                        }
                                        else
        
getListView().getChildAt(arg2).setBackgroundColor(Color.TRANSPARENT);

                                }
        });
    }
}

The R.layout.main is just your standard default TextView when you
create a new project in Eclipse.  The R.layout.listview is the
standard LinearLayout with embedded ListView for ListActivities shown
below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
      android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
        <ListView android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

Can anyone explain rationally what is causing this to happen?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to