Chinaunix首页 | 论坛 | 博客
  • 博客访问: 822113
  • 博文数量: 210
  • 博客积分: 10002
  • 博客等级: 上将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 09:56
文章分类

全部博文(210)

文章存档

2011年(1)

2010年(6)

2009年(65)

2008年(138)

我的朋友

分类: LINUX

2008-12-12 11:52:48

Just 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(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                                                 {
                                                        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:45 pm, "Romain Guy" ...@google.com> wrote:

> Ok 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 ...@gmail.com> wrote:

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

> > On Oct 22, 7:51 pm, "Romain Guy" ...@google.com> wrote:
> >> Could 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 ...@gmail.com> wrote:

> >> > I 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:40 pm, "Romain Guy" ...@google.com> wrote:
> >> >> 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()).

> >> >> On Wed, Oct 22, 2008 at 7:31 PM, kingkung ...@gmail.com> wrote:

> >> >> > 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(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 > >> >> >                                                {
> >> >> >                                                        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:

> >> >> >
> >> >> > > >> >> >      android:layout_width="fill_parent"
> >> >> >    android:layout_height="fill_parent"
> >> >> >    android:orientation="vertical"

> >> >> >         > >> >> >        android:layout_width="fill_parent"
> >> >> >        android:layout_height="wrap_content"
> >> >> >    />
> >> >> >

> >> >> > Can anyone explain rationally what is causing this to happen?

> >> >> --
> >> >> Romain Guywww.curious-creature.org

> >> --
> >> Romain Guywww.curious-creature.org

> --
> Romain Guywww.curious-creature.org
阅读(1715) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~