gpt4 book ai didi

java - 即使在添加项目后列表大小为零

转载 作者:行者123 更新时间:2023-11-30 01:56:50 24 4
gpt4 key购买 nike

我有一个列表,我将其发送到后台异步任务对象以进行一些处理。我还发送了一个自定义列表适配器,以便能够在后台填充我的列表。但是该列表返回零并且似乎没有添加任何内容,因为它的大小仍然为零。我知道,因为我调试了它。我的自定义列表适配器工作得很好,并且完美地创建了列表。

这是我的代码。

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 的事件列表保持为零大小。没有项目被添加。这就是问题所在。

最佳答案

您必须调用 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();
}

关于java - 即使在添加项目后列表大小为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com