0

It's quite a common question but still I'm unable to implement it. In my application, I'm fetching Inbox messages and displaying them in ListView. Code is working fine but onStartup it loads all messages from inbox to listview. But I want when application starts it loads only first 15 message after that a button having label LoadMore will show and when clicking button next 15 messages will add to listview. Here's my code snippet:

listViewSMS=(ListView)findViewById(R.id.lvSMS); 
listViewSMS.setAdapter(smsListAdapter);
getInboxSms();
smsListAdapter = new SMSListAdapter(this,populateSMSList());
    listViewSMS.setAdapter(smsListAdapter);

public void getInboxSms() {
    ContentResolver cr = getContentResolver();
    c = cr.query(Uri.parse("content://sms/inbox"), null, null, null, null);
    totalSMS = c.getCount();
    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {
            Log.d("SMSss", "Contact number : "+ c.getString(c.getColumnIndexOrThrow("address"))+ "\n"
                            + "msg : " + c.getColumnIndexOrThrow("body")
                            + "\n"+ "Person : "
                            + getContactName(c.getString(c.getColumnIndexOrThrow("address"))));
            c.moveToNext();
        }
    }
}

private List<SMSListModel> populateSMSList() 
{
    if(c.getCount()>0)
    {
        for(int i=0;i<c.getCount();i++)
        {
            if(c.moveToPosition(i))
            {

                if(getContactName(c.getString(c.getColumnIndex("address"))) == null )
                {
                    list.add(new SMSListModel(c.getString(c.getColumnIndex("address")),c.getString(c.getColumnIndex("body"))));

                }
                }
        }
    }
    return list;        
}

Any help will be appreciated. Thanks

Android beginner
  • 245
  • 2
  • 6
  • 22

3 Answers3

0

You can use a Adapter (see http://developer.android.com/reference/android/widget/ListAdapter.html). First, create a list with the 15 first messages and set use it in your adapter and set use setAdapter() on your ListView object. When a user clicks your button you just create a new list with some more elements and then a new adapter and you set it to your listview.

AlexanderNajafi
  • 1,631
  • 2
  • 25
  • 39
0

You can use LoadMoreButton simple tutorial, which describes your goal step by step.

naran z
  • 486
  • 3
  • 13
0

you can use Listview.setOnScrollListener

ListView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // checking up n down

                    }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            flag_end_show = false;
            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                flag_end_show = true;
                if(flag_loading == false)
                {
                    flag_loading = true;

                        new LoadMoreAsync().execute();
                }
            }

        }
    });
Rajesh Mikkilineni
  • 854
  • 10
  • 22
  • onStartup does it load all inbox messages and what is `new LoadMoreAsync().execute()`. Thanks – Android beginner Jul 14 '14 at 08:10
  • its nothing but calling a function for getting more data... you have to use pagination , i,e first load 20-30 messages , if user scrolls to bottom of the list if have to get next 20-30 messages and set them to adapter . – Rajesh Mikkilineni Jul 14 '14 at 08:14