Home > Mobile >  Android development of listview notifyDataSetChanged problem, please everyone a great god help me
Android development of listview notifyDataSetChanged problem, please everyone a great god help me

Time:09-26

Is the ArrayList ListView array add finished, send a message to let adapter call notifyDataSetChanged method

But every time it will take me each update in the array to the beginning of the Position that data is ignored, please explain



 private void updateNews () {
//when news show out entirely, cancel the operation of the update news
If (currentIndex==jsonSize) {
Toast. MakeText (mView. GetContext (), "all over!" Toast), LENGTH_SHORT), show ();
return;
}

For (int I=currentIndex; i JSONObject row=data. GetJSONObject (I);

String title=row. Get String (" title ");
String link=row. Get String (" tlink ");
String imgurl=row. Get String (" imgurl ");

News News=new News (imgurl, link, the title);
NewsList. Add (news);
}

CurrentIndex +=10;

The Message MSG=new Message ();
MSG. What=UPDATE_NEWS;
Handler. SendMessage (MSG);

}



//handling of update news 
Handler=new handler () {
@ Override
Public void handleMessage (Message MSG) {
//initialize
If (MSG. What==UPDATE_NEWS) {
Adapter. NotifyDataSetChanged ();
}
}
};



CodePudding user response:

The above printing code is printed in the getView BaseAdapter

 
@ NonNull
@ Override
Public View getView (int position, @ Nullable View convertView, @ NonNull ViewGroup parent) {
ViewHolder holder;
If (convertView==null) {
ConvertView=LayoutInflater. The from (context). Inflate (resourceId, parent, false);
Holder=new ViewHolder ();
Holder. The image=convertView. The findViewById (R.i d.i mg);
Holder. The title=convertView. The findViewById (R.i which itle);

Holder. The image. SetImageURL (newsList. Get (position). GetImgurl ());
Holder. The title. The setText (newsList. Get (position). The getTitle ());

The d (" FirstFragment ", "adapter:" + newsList. Get (position). The getTitle () + "position:" + position);
ConvertView. SetTag (holder);
} else {
Holder=(ViewHolder) convertView. GetTag ();
}
Return convertView.
}

CodePudding user response:

Problem is caused by the lost the position corresponding to the location of the item shows a problem, but the data is correct

CodePudding user response:

Are you a child thread processing data and then notify the main thread to update? If this is going to have a problem, cannot be directly modified in child threads in the adapter data

CodePudding user response:

Listview multiplexing mechanism is you made a mistake,
Convertview is a back and forth to use controls, you can't return back directly, this is the reuse control, the basic idea of
New blog "habit 3" is describe the description of the principle of reuse recommended read as a whole: https://blog.csdn.net/weimingjue/article/details/87921494

Will your holder... set... And the log moved to return a row above normal,
Also the child thread to modify a list may cause collapse, suggest to return to the main thread first, then modify the list,
  • Related