-1

I am fetching data from server and don't know the number of records being fetched. I am saving the data in sqlite and displaying it using cursor. I want to display first 10 records initially in listview. On next button click next 10 records should be displayed in the same listview and so on. Please suggest me a way.

Code:

runOnUiThread(new Runnable() {
     @Override
      public void run() {

    //stuff that updates ui
          /**
           * Updating parsed JSON data into ListView
            * */
            disp();
      }
    });

        public void disp() {

            try {

                Cursor c;

                String sql = "select * from CurrentAffairs LIMIT 10";
                c = sdb.rawQuery(sql, null);
                int rowCount=c.getCount();
                Constants.i=rowCount;
                if (c != null) {
                    // if (c.moveToNext()) {
                    while (c.moveToNext()) {

                        String message = c.getString(c.getColumnIndex("message"));
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key =>
                        // value
                        map.put(TAG_MESSAGE, message);
                        result.add(message);

                    }// while(c.moveToNext());
                }
            } catch (Exception e) {
                // TODO: handle exception
                Log.i("In Banking economics", "Error-" + e.toString());
                e.printStackTrace();
           }
            //ListAdapter adapter = new SimpleAdapter(Banking_Economics.this, result,R.layout.list_item, new String[] { TAG_MESSAGE },new int[] { R.id.textView1 });
            ListAdapter adapter=new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1, result);
            setListAdapter(adapter);
            // selecting single ListView item
            lv = getListView();

            // Launching new screen on Selecting Single ListItem
            lv.setOnItemClickListener(new OnItemClickListener() {

             @Override
             public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                    // getting values from selected ListItem
                    String message = ((TextView) view.findViewById(R.id.textView1))
                        .getText().toString();

                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(), News.class);
                    in.putExtra(TAG_MESSAGE, message);
                    startActivity(in);

                }

            });
        }

I have tried using separate arraylists for every 10 records but as the number of records are not known its not working. Also I don't have knowledge about pagination so can't use it. Any help appreciated

Note: The data from server gets updated daily and new records get added. I want to display these new records on first load of listview.

3 Answers3

1

Make an int variable and increment it by 10 on button click. If you want to show the most recent rows refer this answer.

Community
  • 1
  • 1
rusted brain
  • 1,052
  • 10
  • 23
0

You can manage paging by using Limit and offset in sqlite select query check this question
Edit check this tutorial

Community
  • 1
  • 1
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
  • I am using Limit but I don't want to skip any rows so I have omitted offset. Also I don't have knowledge about pagination. Can you please provide help there? I want to use the code on button click – Shruti Chaudhari Jul 09 '15 at 10:16
0

Changes in Code:

public int i=10,j=0;

    case R.id.imageButton1:
                i=i+10;
                j=j+10;
                ((ArrayAdapter<String>)lv.getAdapter()).clear();
                ((BaseAdapter)lv.getAdapter()).notifyDataSetChanged();
                disp();
                break;
            case R.id.imageButton2:
                if(i!=10){
                    i=i-10;
                    j=j-10;
                }
                else{
                    ib2.setEnabled(false);
                }
                ((ArrayAdapter<String>)lv.getAdapter()).clear();
                ((BaseAdapter)lv.getAdapter()).notifyDataSetChanged();
                disp();
                break;

String sql = "select * from CurrentAffairs LIMIT "+i+" OFFSET "+j;

This Worked for me. Hope it will help someone else too.