我想将新项目附加到ListView以启用无限滚动。
private ListAdapter adapter;
ArrayList < HashMap < String, String >> listViewList;
...
class GetItems extends AsyncTask < String, String, String > {
protected String doInBackground(String...args) {
...
for (int i = 0; i < items.length(); i++) {
...
HashMap < String, String > map = new HashMap < String, String > ();
map.put("name_1", name_1);
map.put("name_2", name_3);
map.put("name_3", name_3);
listViewList.add(map);
}
}
protected void onPostExecute(String file_url) {
adapter = new CustomAdapter(Activity.this, listViewList,
R.layout.single_item, new String[] {}, new int[] {});
setListAdapter(adapter);
}
}
GetItems
课程会根据您提供的页码获取新项目。我想将新项目附加到onPostExecute
上的现有ListView项目。
知道该怎么做吗?
答案 0 :(得分:1)
实际上,您只需在adapter.notifyDataSetChanged
中拨打onPostExecute
即可。
但是你不应该多次实例化适配器。它应该只做一个,所以整体看起来像这样
private ListAdapter adapter;
...
void onCreate(){ // assuming this is an activity
adapter = new CustomAdapter(Activity.this, listViewList,
R.layout.single_item, new String[] {}, new int[] {});
setListAdapter(adapter);
}
class GetItems extends AsyncTask<String, String, String> {
...
protected void onPostExecute(String file_url) {
adapter.notifyDataSetChanged();
}
}
答案 1 :(得分:1)
在GitHub上查看这个库
根据您的评论编辑:
你需要忘记ListActivity
并且你曾经听说它存在,它对你想要做的事情毫无用处。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DemosAdapter extends BaseAdapter {
public List<DemoItem> listOfItems;
private View v;
private ViewWrapper wrapper;
private Context context;
public DemosAdapter(Context c, List<DemoItem> listOfItems) {
this.context = c;
this.listOfItems = listOfItems;
}
@Override
public int getCount() {
return listOfItems.size();
}
@Override
public DemoItem getItem(int i) {
return listOfItems.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
public void sort() {
Collections.sort(listOfItems, new Comparator<DemoItem>() {
@Override
public int compare(DemoItem item, DemoItem item2) {
return item.getName().compareTo(item2.getName());
}
});
}
public void addItems(List<DemoItem> listOfFreshItems){
listOfItems.addAll(listOfFreshItems);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DemoItem itm = listOfItems.get(position);
v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.demo_item, null, false);
wrapper = new ViewWrapper(v);
v.setTag(wrapper);
}
else {
wrapper = (ViewWrapper) v.getTag();
}
wrapper.getTvDemoName().setText(itm.getName());
return v;
}
private class ViewWrapper {
private View base;
private TextView tvDemoName;
public ViewWrapper(View v) {
this.base = v;
}
public TextView getTvDemoName() {
if (tvDemoName == null) {
tvDemoName = (TextView) base.findViewById(R.id.tvDemoName);
}
return tvDemoName;
}
}
}
首次添加数据时,请执行此操作
demosAdapter = new DemosAdapter(this, listOfDemos);
demosAdapter.sort();
lvListOfDemos.setAdapter(demosAdapter);
获得新商品时,请致电
adapter.addItems(List<DemoItem> newItems);