问题
In my activity I have one adapter contains 25 items , and I have one listview. I want to insert 5 items in the listview and if I press the next button in my ativity ,the page will reloads with the next 5 items and so on.
回答1:
Alter the items in the adapter, and then call notifyDataSetChanged() on the adapter.
adapter.clear();
adapter.addAll(nextFiveElements);
adapter.notifyDataSetChanged();
Also, be scrupulous about using the ViewHolder pattern. When implementing getView(), utilizing this design pattern will save a LOT of memory:
http://www.screaming-penguin.com/node/7767
回答2:
you should not add all 25 items to the adapter unnecessarily. add five values only ;
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
}
回答3:
You have to maintain the position of the array
I have created a demo Adapter you can see
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/how-to-insert-the-data-from-the-adapter-to-the-listview