我有一个列表,我发送到后台asynctask对象做一些工作。我还发送了一个自定义列表适配器,以便能够在后台填充我的列表。但是列表返回零,似乎没有添加任何内容,因为它的大小保持为零。我知道,因为我贬低了它。我的自定义列表适配器工作正常,并完美地创建列表。
这是我的代码。
Fragment_Events.java
public class Fragment_Events extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener {
FragmentController controller;
ListView eventsListView;
List<Event> events;
EventsListAdapter eventsListAdapter;
public Fragment_Events() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
controller = (FragmentController) getActivity();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
eventsListView = (ListView)getActivity().findViewById(R.id.listView_events);
events = new ArrayList<Event>();//SIZE REMAINS 0 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
eventsListAdapter = new EventsListAdapter(getActivity(),R.layout.list_events,events);
eventsListView.setAdapter(eventsListAdapter);
DLEvents.init(events,eventsListAdapter);
Bundle sendingBundle = new Bundle();
ArrayList<String> eventNames = new ArrayList<String>();
sendingBundle.putStringArrayList(AppUtils.EVENT_NAMES,eventNames);
controller.sendData(sendingBundle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_events, container, false);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
DLEvents.java
public class DLEvents {
public static final String EVENTS_OBJECT = "Events";
public static final String NAME_COLUMN = "name";
public static final String DESC_COLUMN = "description";
public static final String DATE_COLUMN = "date";
public static final String FOLL_COLUMN = "followers";
public static final String TC_COLUMN = "ticketCount";
public static final String IMAGE_COLUMN = "image";
public static void init(List<Event> list,EventsListAdapter eventsListAdapter){
DownLoadData downLoadData = new DownLoadData(list,eventsListAdapter);
downLoadData.execute();
}
public static class DownLoadData extends AsyncTask<Void,Event,Void>{
public List<Event>events;
public EventsListAdapter eventsListAdapter;
public DownLoadData(List<Event>events, EventsListAdapter eventsListAdapter) {
super();
this.events = events;
this.eventsListAdapter = eventsListAdapter;
}
public Bitmap byteArrayToBitmap(byte[] bytes){
Bitmap temp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return temp;
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = ParseQuery.getQuery(EVENTS_OBJECT);
try {
for(ParseObject tempParseObject: query.find()){
String tempName = tempParseObject.getString(NAME_COLUMN);
String tempDesc = tempParseObject.getString(DESC_COLUMN);
String tempID = tempParseObject.getObjectId();
int tempTC = tempParseObject.getInt(TC_COLUMN);
ParseFile tempPF = (ParseFile)tempParseObject.get(IMAGE_COLUMN);
Bitmap tempBM = byteArrayToBitmap(tempPF.getData());
int tempFoll = tempParseObject.getInt(FOLL_COLUMN);
Event event = new Event(tempName,tempDesc,null,tempID,tempTC,tempBM,tempFoll);
publishProgress(event);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Event... values) {
super.onProgressUpdate(values);
//events.add(values[0]); //I commented it out because it causes my listview to have duplicates, if you can shed some light on this too , I'd appreciate it. Commenting it back in also doesn't affect the size of my list
eventsListAdapter.add(values[0]);
}
}
}
Asynctask似乎工作正常,因为列表没有任何问题。但该列表仍处于0大小的事件,尽管它已通过onprogressreport在UI线程中更新
适配器完美填充列表!这不是问题所在。传递给asynctask的事件列表仍为零大小。没有添加任何项目。这就是问题所在。
答案 0 :(得分:1)
您必须致电notifyDataSetChanged()
更新适配器,并让它知道有新数据。
@Override
protected void onProgressUpdate(Event... values) {
super.onProgressUpdate(values);
//events.add(values[0]); //I commented it out because it causes my listview to have duplicates, if you can shed some light on this too , I'd appreciate it. Commenting it back in also doesn't affect the size of my list
eventsListAdapter.add(values[0]);
eventsListAdapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
一旦数据可用或其数据被修改,您需要请求适配器自行刷新。
这样做
@Override
protected void onProgressUpdate(Event... values) {
super.onProgressUpdate(values);
events.add(values[0]);
eventsListAdapter.add(values[0]);
eventsListAdapter.notifyDataSetChanged();
}