在我的活动中,我有一个包含25个项目的适配器,我有一个列表视图。我想在列表视图中插入5个项目,如果我在ativity中按下next按钮,页面将重新加载接下来的5个项目,依此类推。
发布于 2011-11-17 19:10:34
更改适配器中的项,然后在适配器上调用notifyDataSetChanged()。
adapter.clear();
adapter.addAll(nextFiveElements);
adapter.notifyDataSetChanged();此外,在使用ViewHolder模式时也要谨慎。在实现getView()时,使用此设计模式将节省大量内存:
http://www.screaming-penguin.com/node/7767
发布于 2011-11-17 19:19:53
您不应该将所有25项都不必要地添加到适配器中。仅添加五个值;
static int pageNo. = 0 ;
final int pageSize = 5;
btn.onClick()
{
pageNo.+=;
for(int i = pageNo. ;i<pageN0. + pageSize;i++)
{
adapter.add(*i'th value*);
//modify syntext as per need
}发布于 2011-11-17 19:45:46
您必须保持数组的位置
我已经创建了一个演示适配器,您可以看到
private static final int NO_OF_ITEMS_IN_PAGE = 5;
pivate static int currentPageNo = 0;
public class MyAdapter extends BaseAdapter {
ArrayList<String> arrNotes;
LayoutInflater inflater;
public MyAdapter(Context c, ArrayList<String> arrNotes) {
this.arrNotes = arrNotes;
inflater = ((Activity) c).getLayoutInflater();
}
@Override
public int getCount() {
return NO_OF_ITEMS_IN_PAGE;
}
@Override
public Object getItem(int position) {
int actualPosition = currentPageNo * NO_OF_ITEMS_IN_PAGE + position;
return arrTodaysMedicines.get(actualPosition);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
int actualPosition = currentPageNo * NO_OF_ITEMS_IN_PAGE + position;
String strNote = arrNotes.get(actualPosition);
if(convertView == null) {
convertView = inflater.inflate(R.layout.note_list, null);
}
TextView tvTitle =
(TextView) convertView.findViewById(R.id.tvTitle);
tvTitle.setText(strNote);
return convertView;
}
}https://stackoverflow.com/questions/8166092
复制相似问题