Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to populate a list view with json data and also give a pagination feature to that screen. the listView should show 5 datas at a time and on clicking the pagination button the listview should show the next 5 datas.

here is the code that i have used
Java
try {
                    Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    JSONObject obj = new JSONObject(response);
                    JSONArray jsonArray = obj.getJSONArray("contacts");
                    int length = jsonArray.length();
                    if (length != 0)
                    {
                        for (int i = 0; i < length; i++)
                        {
                            JSONObject user = jsonArray.getJSONObject(i);
                            for (int l = 1; l <= 5; l++)
                            {
                                preference_array.add(user.getString("id").toString());
                            }
                        }
                    }
                        int rem = preference_array.size() % rowSize;
                        if (rem > 0) {
                            for (int k = 0; k < rowSize - rem; k++) {
                                preference_array.add("");
                            }

                            addItem(0);

                            int size = preference_array.size() / rowSize;

                            for (int j = 0; j < size; j++) {
                                final int k;
                                k = j;
                                final Button btnPage = new Button(MainActivity.this);
                                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(120, ViewGroup.LayoutParams.WRAP_CONTENT);
                                lp.setMargins(5, 5, 5, 5);
                                btnPage.setTextColor(Color.WHITE);
                                btnPage.setTextSize(20.0f);
                                btnPage.setId(j);
                                btnPage.setText(String.valueOf(j + 1));
                                mLinearScroll.addView(btnPage, lp);
                                btnPage.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // TODO Auto-generated method stub
                                        /**
                                         * add arraylist item into list
                                         */
                                        addItem(k);
                                    }
                                });
                            }
                        }
                       
                        display();
                 
                } 


Java
private void addItem(int i) {
        // TODO Auto-generated method stub
        preference_array_temp.clear();
        i = i * rowSize;

        /**
         * fill temp array list to set on page change
         */
        for (int j = 0; j < rowSize; j++) {
            preference_array_temp.add(j, preference_array.get(i));
            i = i + 1;
        }

        // set view
        display();
    }

    private void display() {
        adapter = new CustomBaseAdapter(MainActivity.this, preference_array);
        fav.setAdapter(adapter);
    }


i am able to populate the listview and display the pagination button. but the listview repeats the all the data 5 times and loads all 13*5 times data in the listView.

i need to be able to click the 2nd pagination button and show the next 5 datas. where could i have gone wrong. please give guidance.
Posted

1 solution

I'm not sure I follow the logic of your code, but in the first part you have:
Java
for (int i = 0; i < length; i++)
{
    JSONObject user = jsonArray.getJSONObject(i);
    for (int l = 1; l <= 5; l++)
    {
        preference_array.add(user.getString("id").toString());
    }
}

So you get the object whose index is i, and add 5 copies of its id field to the array. Is that what you wanted?

You would be better setting your page number first, and then use that to extract the next 5 entries, whenever the new page button gets clicked. So if page number is 0 you get the first five elements, if it's 1 you get elements 5 to 9 etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900