How to divide and load a ListView in multiple parts?
I am wondering how could i divide my List开发者_运维问答View
in parts and display only one part of it when user starts my app and display others when user press a Button called "Load More Items".
I have a big list of more than 500 items and thinking to divide it into parts so it could load fast.
I want functionality similar to an any email app which gives an option at the bottom to load more emails.
If anyone has any sample code for my problem then please share else a little guidance would also be appreciated.
well implementing a button is easy enough:
in your onCreate, load your adapter with the 1st 50 items, then implement a button with an onClickListener that adds the next 50 etc.
HOWEVER I think what you really want to do is lazy load your listview so as they scroll, it will load more items - that why you don't need to clutter the UI with an extra button.
for this your listactivity should implement OnScrollListener
here is an example of that: Android Endless List
From your question, it seems that you want to load few items initially and then load more items in future whenever user click on the "Load More items" button.
For that there can be two cases possible:
First case: webservice can send response in parts, like first time it sends 20 items, and next time 20 items whenever user clicks on "Load more items" button.
Second case: If you get 500 items in response, then to implement "Load more items" kind of functionality, you have to create database table to store all the items. Once you are done with database value storing, fetch 20 items initially, next time load 20 items and so on.
Initially load part of data in your list view.
You have to use concept of Handler
. onclick event you have to send message to handler inside handler you have to write the logic to load your full data and call notifydataSetChanged
method
have a look on the sample code below. Initially user is able to see some part of list. If user cvlicks on any list item then list user is able to see the whole list view. It is similar to as that you are expecting.
Sample Code
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListView extends ListActivity {
ArrayList<String> pens = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
private static final byte UPDATE_LIST = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pens.add("MONT Blanc");
pens.add("Gucci");
pens.add("Parker");
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, pens);
setListAdapter(arrayAdapter);
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.out.println("..Item is clicked..");
Message msg = new Message();
msg.what = UPDATE_LIST;
updateListHandler.sendMessage(msg);
}
});
// System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234"));
// System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4"));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("...11configuration is changed...");
}
void addMoreDataToList() {
pens.add("item1");
pens.add("item2");
pens.add("item3");
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, id + "You have chosen the pen: " + " " + pen,
Toast.LENGTH_LONG).show();
}
private Handler updateListHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LIST:
addMoreDataToList();
arrayAdapter.notifyDataSetChanged();
break;
}
;
};
};
}
Thanks Deepak
精彩评论