ListView Choices: What the devil is happening?

by kingkung » Thu, 23 Oct 2008 10:31:21 GMT


Sponsored Links
 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?
--~--~---------~--~----~------------~-------~--~----~



ListView Choices: What the devil is happening?

by Romain Guy » Thu, 23 Oct 2008 10:40:53 GMT


 You haven't explained what the crazy behavior is :) My bet is that you
call isChecked() too early. Try to postpone everything you do in the
OnItemClickListener by posting a Runnable into a Handler (or with
getListView().post()).






-- 
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


ListView Choices: What the devil is happening?

by kingkung » Thu, 23 Oct 2008 10:49:25 GMT


  kind of wanted people to try it and find out :)  I can try that, but
it doesn't look like it's a race condition (like something gets called
too early or something) of any kind... it's a very distinct pattern of
behavior that's happening, though it's not what I would consider the
expected behavior...

On Oct 22, 7:40pm, "Romain Guy" <[EMAIL PROTECTED]> wrote:
--~--~---------~--~----~------------~-------~--~----~



ListView Choices: What the devil is happening?

by Romain Guy » Thu, 23 Oct 2008 10:51:56 GMT


 ould you please explain what you are witnessing? (I work on ListView,
I'd like to help... :)

There's another issue in your code. In the for loop, you are comparing
a position (arg2) with an index (i). These are different and mixing
them will cause tons of "weird" issues.

On Wed, Oct 22, 2008 at 7:49 PM, kingkung <[EMAIL PROTECTED]> wrote:



--
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



ListView Choices: What the devil is happening?

by kingkung » Thu, 23 Oct 2008 11:30:26 GMT


  just sent you an email explaining the problem to your google
account. :)

On Oct 22, 7:51pm, "Romain Guy" <[EMAIL PROTECTED]> wrote:
--~--~---------~--~----~------------~-------~--~----~



ListView Choices: What the devil is happening?

by Romain Guy » Thu, 23 Oct 2008 11:55:37 GMT


 k so the issue is what I noticed: you are mixing index and positions.
A ListView contains only as many items as it needs to fill the screen.
For instance, you can see 8 items on screens, there are 8 children in
the ListView, numbered from 0 to 7 (the index.) However, your adapter
can have many more items, for instance 400. The position (arg2 in your
code) indicates the index of an item *in the adapter*. This means that
the View of index 2 is not necessarily the adapter's item of position
2. To do the conversion, you can use
ListView.getFirstVisiblePosition().

Here is your code with the fix:

if (i==arg2) becomes if (getListView().getFirstVisiblePosition() + i == arg2)
and all the getChildAt(arg2) become getChildAt(arg2 -
getListView().getFirstVisiblePosition())


On Wed, Oct 22, 2008 at 8:20 PM, kingkung <[EMAIL PROTECTED]> wrote:



--
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



ListView Choices: What the devil is happening?

by kingkung » Fri, 24 Oct 2008 00:43:10 GMT


 ust made the changes, the behavior is identical... this is my updated
code, feel free to make sure I made all changes:

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
(getListView().getFirstVisiblePosition()+i==arg2)

getListView().getChildAt(i).setBackgroundColor(Color.MAGENTA);
else

getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
}
else
getListView().getChildAt(arg2-
getListView().getFirstVisiblePosition()).setBackgroundColor(Color.TRANSPARENT);

}
});
}

On Oct 22, 8:45pm, "Romain Guy" <[EMAIL PROTECTED]> wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. which sites have tutorials for Android?

Please post some here.

I am new for Android.

Thanks

--~--~---------~--~----~------------~-------~--~----~

2. Relative Layout XML attributes have no obvious programmatic equivalent.

If a RelativeLayout must be generated at run time, what are the
equivalent API calls for the attributes set in the XML Layout editor?
Take for example this very simple RelativeLayout that places the
second ImageView to the right of the first ImageView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal" android:background="@drawable/
bg_sunrise" android:layout_gravity="center" android:gravity="center">

<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"></
ImageView>

<ImageView android:id="@+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@id/
ImageView01" android:src="@drawable/icon"></ImageView>
</RelativeLayout>


Of the many, many, many, many variations I tried, I had the most hope
for this one, but it didn't work either:

       protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               this.requestWindowFeature(Window.FEATURE_NO_TITLE);
               RelativeLayout layout = new RelativeLayout(this);
               layout.setLayoutParams( new
                               ViewGroup.LayoutParams
( LayoutParams.FILL_PARENT,
                               LayoutParams.FILL_PARENT ) );

           ImageView imageView1 = new ImageView(this);
           imageView1.setImageResource(R.drawable.icon);
           imageView1.setAdjustViewBounds(true); // set the ImageView
bounds
to match the Drawable's dimensions

               RelativeLayout.LayoutParams params1 = new
RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
               params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
           layout.addView(imageView1, params1);

           ImageView imageView2 = new ImageView(this);
           imageView2.setImageResource(R.drawable.icon);
           imageView2.setAdjustViewBounds(true); // set the ImageView
bounds
to match the Drawable's dimensions

               RelativeLayout.LayoutParams params2 = new
RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
               params2.addRule(RelativeLayout.RIGHT_OF,
imageView1.getId());
           layout.addView(imageView2, params2);
           this.setContentView(layout);
       }

Can anyone offer the Java equivalent to the above XML?  Can anyone
explain why there is no attribute, getter/setter, or method for
accessing the properties that can be set in XML?  What is the most
elegant solution for dynamically creating Layouts, Views, and other
Resources on the Android platform when there is no Java programmatic
equivalent?

--~--~---------~--~----~------------~-------~--~----~

3. Make a call without dial in background

4. Do we get any event in the Activity when the activity gets re-sized?

5. tanya flash ROM magicke hero lag

6. Lupakan BlackBerry, Kini Era Android

7. Remote Process Question