我开发一款application..In我的应用程序,我使用列表视图来显示json..all数据显示perfectly..but我要的是数据,以显示前10个对象...然后装载物品应展会然后剩下10将display..my JSON响应如下面给出的。
{
interestsent:
[
{
interestsent_user_id:369,
名:ABC,
PROFILE_ID:686317,
图像:,
},
{
interestsent_user_id:369,
名:DEF
PROFILE_ID:686318,
图像:,
},
{
interestsent_user_id:369,
名:GHI
PROFILE_ID:686319,
图像:,
},
{
interestsent_user_id:369,
名:JKL,
PROFILE_ID:686320,
图像:,
},
{
interestsent_user_id:369,
名:MNO
PROFILE_ID:686321,
图像:,
},
{
interestsent_user_id:369,
名:PQR,
PROFILE_ID:686322,
图像:,
},
.................
.................
]
}
Interestsent.java
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
INTEREST_SENT_URL =http://fshdfh.com/webservice/interestsent?version=apps&user_login_id=00;
新的GetData()执行();
列表视图=(ListView控件)findViewById(R.id.listview);
Log.i(DATA,页面:在OnCreate的onCreate的onCreate);
mHandler =新的处理程序();
listview.setOnScrollListener(新EndlessScrollListener(){
@覆盖
公共无效onLoadMore(INT页,诠释totalItemsCount){
Log.i(DATA,页::+网页+totalItemsCount ::
+ totalItemsCount);
mProgressbar =新ProgressDialog(MainActivity.this);
mProgressbar.setMessage(载入中...);
mProgressbar.show();
算上++;
如果(计数< maindata.size()){
如果(适配器!= NULL){
如果(!isLoadingMore){
isLoadingMore = TRUE;
mHandler.postDelayed(loadMoreRunnable,1000);
}
}
}
}
});
}
可运行loadMoreRunnable =新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
如果(计数< maindata.size()){
如果(适配器!= NULL){
adapter.addAll(maindata.get(计数));
mHandler.removeCallbacks(loadMoreRunnable);
isLoadingMore = FALSE;
如果(mProgressbar.isShowing())
mProgressbar.dismiss();
}
}
}
};
静态< T> ArrayList的< ArrayList的< T>> chunkList(名单< T>列表中,最终诠释L){
ArrayList的< ArrayList的< T>>部分=新的ArrayList< ArrayList的< T>>();
最终诠释N =则为list.size();
对(INT I = 0; I&所述N; I + = L){
parts.add(新的ArrayList< T>(list.subList(我,Math.min(N,I + L))));
}
返回的部分;
}
私人ProgressDialog mProgressbar;
公共类的GetData扩展的AsyncTask<字符串,字符串,字符串> {
ArrayList的<整数GT;数据=新的ArrayList<整数GT;();
@覆盖
在preExecute保护无效(){
// TODO自动生成方法存根
mProgressbar =新ProgressDialog(MainActivity.this);
mProgressbar.setMessage(载入中...);
mProgressbar.show();
super.on preExecute();
}
@覆盖
保护字符串doInBackground(字符串... PARAMS){
// TODO自动生成方法存根
ArrayList的< ArrayList的<整数GT;>数据=新的ArrayList< ArrayList的<整数GT;>();
ServiceHandler SH =新ServiceHandler();
/*尝试 {
视频下载(5000);
}赶上(InterruptedException异常E){
// TODO自动生成的catch块
e.printStackTrace();
} * /
/ *的for(int i = 0; I< 500;我++){
data.add(ⅰ);
}
返回null; * /
字符串jsonStr = sh.makeServiceCall(INTEREST_SENT_URL,ServiceHandler.GET);
Log.d(回应:,>中+ jsonStr);
如果(jsonStr!= NULL){
尝试 {
JSONObject的jsonObj =新的JSONObject(jsonStr);
//获取JSON数组节点
interestsent = jsonObj.getJSONArray(INTEREST_SENT);
//遍历所有联系人
的for(int i = 0; I< interestsent.length();我++){
JSONObject的C = interestsent.getJSONObject(我);
//创建新的HashMap
// HashMap的< ArrayList的<整数GT;图=新的HashMap<整数GT;();
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
//添加每个子节点HashMap中的key =>值
map.put(INTEREST_USER_ID,c.getString(INTEREST_USER_ID));
map.put(INTEREST_SENT_NAME,c.getString(INTEREST_SENT_NAME));
map.put(INTEREST_SENT_PROFILE,c.getString(INTEREST_SENT_PROFILE));
map.put(INTEREST_SENT_IMAGE,c.getString(INTEREST_SENT_IMAGE));
map.put(INTEREST_SENT_CAST,c.getString(INTEREST_SENT_CAST));
map.put(INTEREST_SENT_AGE,c.getString(INTEREST_SENT_AGE)+年);
map.put(INTEREST_SENT_LOCATION,c.getString(INTEREST_SENT_LOCATION));
//添加HashList到ArrayList中
data.addAll(图)
}
}赶上(JSONException E){
e.printStackTrace();
}
} 其他 {
Log.e(ServiceHandler,无法从URL中得到任何数据);
}
返回的数据;
}
@覆盖
保护无效onPostExecute(字符串结果){
// TODO自动生成方法存根
super.onPostExecute(结果);
maindata = chunkList(数据,50);
Log.i(DATA,maindata ::+ maindata.size());
适配器=新CustomAdapterSent(maindata.get(计数),
MainActivity.this);
listview.setAdapter(适配器);
如果(mProgressbar.isShowing())
mProgressbar.dismiss();
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
EndlessScroolListener.java
公共抽象类EndlessScrollListener实现OnScrollListener {
//项目的最低金额低于你的当前滚动位置
//加载更多了。
私人诠释visibleThreshold = 5;
//数据的当前偏移指数已加载
私人诠释当前页= 0;
//在数据集中的项目的最后的负载后的总数
私人诠释previousTotalItemCount = 0;
//真,如果我们仍然在等待最后一组数据加载。
私人布尔负荷=真;
//设置起始页指数
私人诠释startingPageIndex = 0;
公共EndlessScrollListener(){
}
公共EndlessScrollListener(INT visibleThreshold){
this.visibleThreshold = visibleThreshold;
}
公共EndlessScrollListener(INT visibleThreshold,诠释起始页){
this.visibleThreshold = visibleThreshold;
this.startingPageIndex =起始页;
this.currentPage =起始页;
}
//这种情况发生很多次滚动在第二,所以要谨慎的code你放在这里。
//我们给出了一些有用的参数来帮助我们工作,如果我们需要加载更多的数据,
//但首先我们检查,如果我们正在等待previous负载来完成。
@覆盖
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,INT totalItemCount)
{
//如果总项目数是零,并且previous不是,假设
//名单无效,应恢复为初始状态
如果(totalItemCount< previousTotalItemCount){
this.currentPage = this.startingPageIndex;
这previousTotalItemCount = totalItemCount。
如果(totalItemCount == 0){this.loading = TRUE; }
}
//如果它还是加载,我们检查如果数据集数有
//改变,如果是这样我们断定它已经完成加载和更新当前页面
//数和总项目数。
如果(装载和安培;及(totalItemCount> previousTotalItemCount)){
装载= FALSE;
previousTotalItemCount = totalItemCount;
当前是++;
}
//如果当前没有加载,我们检查,如果我们违反
//在visibleThreshold,并需要重新加载更多的数据。
//如果我们需要重新加载更多的数据,我们执行onLoadMore获取数据。
如果(装载和放大器;&安培;!(totalItemCount - visibleItemCount)< =(firstVisibleItem + visibleThreshold)){
onLoadMore(当前是+ 1,totalItemCount);
加载=真正的;
}
}
//定义过程根据页面上实际加载更多的数据
公共抽象无效onLoadMore(INT页,INT totalItemsCount);
@覆盖
公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){
//不要采取任何改变措施
}
}
CustomAdapterSent.java
公共类CustomAdapterSent扩展了BaseAdapter {
私人的ArrayList<整数GT; MDATA =新的ArrayList<整数GT;();
私人语境mContext;
私人LayoutInflater充气= NULL;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_PROFILE =PROFILE_ID;
私有静态最后弦乐TAG_IMAGE =形象;
私有静态最后弦乐TAG_CAST =投;
私有静态最后弦乐TAG_AGE =时代;
私有静态最后弦乐TAG_LOCATION =地利;
公共CustomAdapterSent(ArrayList中<整数GT; MDATA,上下文mContext){
this.mContext = mContext;
this.mData = MDATA;
充气=(LayoutInflater)mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
公共无效的addAll(ArrayList中<整数GT; mmData){
this.mData.addAll(mmData);
this.notifyDataSetChanged();
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回mData.size();
}
@覆盖
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回mData.get(为arg0);
}
@覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
}
类ViewHolder {
公众的TextView txtView;
TextView的txtproname;
}
@覆盖
公共查看getView(INT现在的位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
查看查看= convertView;
ViewHolder持有人;
如果(查看== NULL){
鉴于= inflater.inflate(R.layout.row_layout,NULL);
持有人=新ViewHolder();
///holder.propic =(ImageView的)convertView.findViewById(R.id.propicsent);
// holder.txtproname =(TextView中)convertView.findViewById(R.id.txtpronameintsent);
//holder.txtproid =(TextView中)convertView.findViewById(R.id.txtproidsent);
//holder.txtprofilecast =(TextView中)convertView.findViewById(R.id.txtprofilecastintsent);
//holder.txtprofileage =(TextView中)convertView.findViewById(R.id.txtprofileageintsent);
// holder.txtprofileplace =(TextView中)convertView.findViewById(R.id.txtprofileplaceintsent);
holder.txtView =(TextView中)view.findViewById(R.id.no_intsent);
view.setTag(保持器);
} 其他 {
支架=(ViewHolder)view.getTag();
}
Log.i(DATA,mData.get(位置)::+ mData.get(位置));
mData.get(位置);
// holder.txtproname.setText(listData.get(位置)获得(TAG_NAME));
holder.txtView.setText(「指数::+ Integer.getInteger(TAG_NAME));
返回查看;
}
}
XML文件:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / relativemainss
机器人:背景=@可绘制/ backg
>
< ListView的机器人:ID =@机器人:ID /列表
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:dividerHeight =1DP>
< / ListView控件>
<的TextView
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:ID =@ + ID / no_intsent
机器人:文字颜色=@机器人:彩色/黑白
机器人:背景=@可绘制/ nomessaegborder/>
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=加载更多
机器人:ID =@ + ID / buttoloadmoreintsent
机器人:layout_below =@机器人:ID /列表/>
< / RelativeLayout的>
解决方案
要只显示10项首次在的ListView
,并显示在滚动多个项目遵循以下步骤:
步骤1:创建 chunkList
方法,它分为的ArrayList
的大小的部分10:
静态< T>名单< ArrayList的< T>> chunkList(名单< T>列表中,最终诠释L){
名单< ArrayList的< T>>部分=新的ArrayList< ArrayList的< T>>();
最终诠释N =则为list.size();
对(INT I = 0; I&所述N; I + = L){
parts.add(新的ArrayList< T>(
list.subList(ⅰ,Math.min(N,I + L)))
);
}
返回的部分;
}
第2步:创建的ArrayList
并显示当前部分计:
私人布尔isLoadingMore = FALSE;
处理器mHandler =新的处理程序();
名单< ArrayList的< HashMap的<字符串,字符串>>> mainArrayList;
私人诠释计数= 0;
第三步:在 onPostExecute
破发结果
和ListView的数据显示:
保护无效onPostExecute(ArrayList中< HashMap的<字符串,字符串>>的结果){
super.onPostExecute(结果);
//你的code在这里...
mainArrayList = chunkList(结果,10);
isLoadingMore = FALSE;
计数= 0;
适配器=新CustomAdapterSent(Interestsent.this,
mainArrayList.get(计数));
setListAdapter(适配器);
}
}
第四步:创建的addAll方法适配器追加在当前数据源的数据:
公共无效的addAll(ArrayList中< HashMap的<字符串,字符串>> MOREDATA){
this.listData.addAll(MOREDATA);
this.notifyDataSetChanged();
}
第五步:在 onLoadMore
在ListView控件加载更多的数据:
mHandler =新的处理程序();
listview.setOnScrollListener(新EndlessScrollListener(){
@覆盖
公共无效onLoadMore(INT页,诠释totalItemsCount){
如果(计数< mainArrayList.size() - 1)
{
如果(适配器!= NULL){
算上++;
如果(!isLoadingMore){
isLoadingMore = TRUE;
mHandler.postDelayed(loadMoreRunnable,1000);
}
}
}
}
});
}
可运行loadMoreRunnable =新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
如果(计数< mainArrayList.size())
{
如果(适配器!= NULL){
算上++;
adapter.addAll(mainArrayList.get(计数));
mHandler.removeCallbacks(loadMoreRunnable);
isLoadingMore = FALSE;
}
}
}
};
I am developing one application..In my app I am using listview to display data from json..all the data display perfectly..but what I want is, to display first 10 objects...and then loading items should show and then remaining 10 will display..my json response is as given below..
{
"interestsent":
[
{
"interestsent_user_id":369,
"name":"abc",
"profile_id":"686317",
"image":"",
},
{
"interestsent_user_id":369,
"name":"def",
"profile_id":"686318",
"image":"",
},
{
"interestsent_user_id":369,
"name":"ghi",
"profile_id":"686319",
"image":"",
},
{
"interestsent_user_id":369,
"name":"jkl",
"profile_id":"686320",
"image":"",
},
{
"interestsent_user_id":369,
"name":"mno",
"profile_id":"686321",
"image":"",
},
{
"interestsent_user_id":369,
"name":"pqr",
"profile_id":"686322",
"image":"",
},
.................
.................
]
}
Interestsent.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
INTEREST_SENT_URL = "http://fshdfh.com/webservice/interestsent?version=apps&user_login_id=00";
new GetData().execute("");
listview = (ListView) findViewById(R.id.listview);
Log.i("DATA", "page ::onCreate onCreate onCreate ");
mHandler = new Handler();
listview.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
Log.i("DATA", "page :: " + page + " totalItemsCount :: "
+ totalItemsCount);
mProgressbar = new ProgressDialog(MainActivity.this);
mProgressbar.setMessage("Loading...");
mProgressbar.show();
count++;
if (count < maindata.size()) {
if (adapter != null) {
if (!isLoadingMore) {
isLoadingMore = true;
mHandler.postDelayed(loadMoreRunnable, 1000);
}
}
}
}
});
}
Runnable loadMoreRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (count < maindata.size()) {
if (adapter != null) {
adapter.addAll(maindata.get(count));
mHandler.removeCallbacks(loadMoreRunnable);
isLoadingMore = false;
if (mProgressbar.isShowing())
mProgressbar.dismiss();
}
}
}
};
static <T> ArrayList<ArrayList<T>> chunkList(List<T> list, final int L) {
ArrayList<ArrayList<T>> parts = new ArrayList<ArrayList<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(list.subList(i, Math.min(N, i + L))));
}
return parts;
}
private ProgressDialog mProgressbar;
public class GetData extends AsyncTask<String, String, String> {
ArrayList<Integer> data = new ArrayList<Integer>();
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
mProgressbar = new ProgressDialog(MainActivity.this);
mProgressbar.setMessage("Loading...");
mProgressbar.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>();
ServiceHandler sh = new ServiceHandler();
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/*for (int i = 0; i < 500; i++) {
data.add(i);
}
return null;*/
String jsonStr = sh.makeServiceCall(INTEREST_SENT_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
interestsent = jsonObj.getJSONArray(INTEREST_SENT);
// looping through All Contacts
for (int i = 0; i < interestsent.length(); i++) {
JSONObject c = interestsent.getJSONObject(i);
// creating new HashMap
// HashMap<ArrayList<Integer> map = new HashMap<Integer>();
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(INTEREST_USER_ID, c.getString(INTEREST_USER_ID));
map.put(INTEREST_SENT_NAME,c.getString(INTEREST_SENT_NAME));
map.put(INTEREST_SENT_PROFILE, c.getString(INTEREST_SENT_PROFILE));
map.put(INTEREST_SENT_IMAGE, c.getString(INTEREST_SENT_IMAGE));
map.put(INTEREST_SENT_CAST, c.getString(INTEREST_SENT_CAST));
map.put(INTEREST_SENT_AGE, c.getString(INTEREST_SENT_AGE)+" years");
map.put(INTEREST_SENT_LOCATION, c.getString(INTEREST_SENT_LOCATION));
// adding HashList to ArrayList
data.addAll(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
maindata = chunkList(data, 50);
Log.i("DATA", "maindata :: " + maindata.size());
adapter = new CustomAdapterSent(maindata.get(count),
MainActivity.this);
listview.setAdapter(adapter);
if (mProgressbar.isShowing())
mProgressbar.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
EndlessScroolListener.java
public abstract class EndlessScrollListener implements OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
onLoadMore(currentPage + 1, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
CustomAdapterSent.java
public class CustomAdapterSent extends BaseAdapter{
private ArrayList<Integer> mData = new ArrayList<Integer>();
private Context mContext;
private LayoutInflater inflater = null;
private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";
public CustomAdapterSent(ArrayList<Integer> mData, Context mContext) {
this.mContext = mContext;
this.mData = mData;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addAll(ArrayList<Integer> mmData) {
this.mData.addAll(mmData);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mData.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
public TextView txtView;
TextView txtproname;
}
@Override
public View getView(int postion, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
ViewHolder holder;
if (view == null) {
view = inflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
///holder.propic = (ImageView) convertView.findViewById(R.id.propicsent);
// holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronameintsent);
//holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidsent);
//holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastintsent);
//holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageintsent);
// holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceintsent);
holder.txtView = (TextView) view.findViewById(R.id.no_intsent);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Log.i("DATA", "mData.get(postion) :: " + mData.get(postion));
mData.get(postion);
// holder.txtproname.setText(listData.get(position).get(TAG_NAME));
holder.txtView.setText("Index :: " + Integer.getInteger(TAG_NAME));
return view;
}
}
XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativemainss"
android:background="@drawable/backg"
>
<ListView android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:dividerHeight="1dp">
</ListView>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/no_intsent"
android:textColor="@android:color/black"
android:background="@drawable/nomessaegborder" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Load More"
android:id="@+id/buttoloadmoreintsent"
android:layout_below="@android:id/list" />
</RelativeLayout>
解决方案
To Show only 10 item first time in ListView
and show more items on scroll follow following steps:
STEP 1: Create a chunkList
method which divide ArrayList
in parts of size 10:
static <T> List<ArrayList<T>> chunkList(List<T> list, final int L) {
List<ArrayList<T>> parts = new ArrayList<ArrayList<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}
STEP 2: Create ArrayList
and a counter which show current part :
private boolean isLoadingMore=false;
Handler mHandler = new Handler();
List<ArrayList<HashMap<String,String>>> mainArrayList;
private int count=0;
STEP 3: In onPostExecute
break result
and show data in ListView:
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
// your code here...
mainArrayList=chunkList(result,10);
isLoadingMore=false;
count=0;
adapter = new CustomAdapterSent(Interestsent.this,
mainArrayList.get(count));
setListAdapter(adapter);
}
}
STEP 4: Create addAll method in Adapter to append data in current data source :
public void addAll(ArrayList<HashMap<String,String>> moreData){
this.listData.addAll(moreData);
this.notifyDataSetChanged();
}
STEP 5: In onLoadMore
load more data in ListView:
mHandler = new Handler();
listview.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
if(count<mainArrayList.size()-1)
{
if(adapter !=null){
count++;
if(!isLoadingMore){
isLoadingMore=true;
mHandler.postDelayed(loadMoreRunnable,1000);
}
}
}
}
});
}
Runnable loadMoreRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(count<mainArrayList.size())
{
if(adapter !=null){
count++;
adapter.addAll(mainArrayList.get(count));
mHandler.removeCallbacks(loadMoreRunnable);
isLoadingMore=false;
}
}
}
};
相关推荐
最新文章