listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {
for(int i =parent.getFirstVisiblePosition(); i <parent.getLastVisiblePosition(); i ++)
{
Log.i("in", Integer.toString(i));
View v开发者_开发知识库 = parent.getChildAt(i);
((Button)v.findViewById(R.id.removefav)).setVisibility(View.INVISIBLE);
((Button)v.findViewById(R.id.gotherefav)).setVisibility(View.INVISIBLE);
}
viewItem=listView.getChildAt(position);
((Button)viewItem.findViewById(R.id.removefav)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.gotherefav)).setVisibility(View.VISIBLE);
TextView text = (TextView)viewItem.findViewById(R.id.item);
name = text.getText();
This code works if you are clicking on items that are visible on the first start of the ListView
, but if you scroll and have, lets say half visible item, throws NullpointerException
:
07-02 18:38:47.290: WARN/dalvikvm(6078): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-02 18:38:47.290: ERROR/AndroidRuntime(6078): Uncaught handler: thread main exiting due to uncaught exception
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): java.lang.NullPointerException
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at walk.me.Tabber$1.onItemClick(Tabber.java:89)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.widget.ListView.performItemClick(ListView.java:3285)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.os.Handler.handleCallback(Handler.java:587)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.os.Looper.loop(Looper.java:123)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at java.lang.reflect.Method.invoke(Method.java:521)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): at dalvik.system.NativeStart.main(Native Method)
What I'm trying to do is, I have a ListView
and two buttons. And the user touches one of the items the child views (two buttons) show up, when the user touches another item, buttons on previously selected item disappear.
What I'm doing wrong? Is that "half-visible" item (when the list is scrolled) makes the NPE? I also tried starting from
i=0
but it was the same.
You should not modify the views like that in listView. The list view in android is always backed by an Adapter and it is optimised so that views are re-used when they are scrolled out of the screen. This means that you always should have getView() implemented in adapter that should reflect how the data (model in adapter) is presented in the view. It is important to react properly when you receive a view to recycle in getView parameters (i.e. not to create it from scratch, but rather than that set proper fields, texts, visibility etc. according to the item from the adapter corresponding to the view).
Whenever you modify all the relevant data in the adapter (change a flag indicating the visibility in your case) you should call notifyDataSetChanged() on the adapter and then the listview will recreate all the visible items on its own (it will loop through them and call getView() reusing the views that were already created.
Look also at Android listActivity onListItemClick with CheckBox which covers similar problem.
精彩评论