简体   繁体   中英

Add load more button to listview in android

I am working on android projects. In my application I am getting the data from php webservice. I added my data to different arraylists and set them to listadapter. But my problem is it is taking very long time to display the data in list. Hence now I want to display first 10 items and want to keep a loadmore button at the bottom of the screen. Once the load more button is clicked the next 10 items need to display. Please can anybody help me in this regard. I would really appreciate for this help. Thank you in advance.

Code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        Longop op = new Longop();
            op.execute("");
    }


public void loadSomeDummyData() {

        try {

            response = CustomHttpClient
                    .executeHttpPost(
                            "http://website.com/folder/testfile.php",
                            postParameters);

            for (int i = 1; i < arr1.length - 1; i++) {

                id.add(new String(arr[0]));
                name.add(new String(arr[1]));
                dateofbirth.add(new String(arr[2]));
                status.add(new String(arr[3]));

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private class Longop extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            loadSomeDummyData();
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            mdialog.dismiss();

            myListView.setAdapter(new MyArrayAdapter(Sample.this,
                        R.layout.list, id, name, dateofbirth,
                        status));
            }

        @Override
        protected void onPreExecute() {
            mdialog = ProgressDialog.show(Sample.this, "Please wait...",
                    "Retrieving data ...", true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }



    private class MyArrayAdapter extends ArrayAdapter<String> {

        // this list hold the data that we will see in listview
        private List<String> myData = new ArrayList<String>();
        private List<String> myData1 = new ArrayList<String>();
        private List<String> myData2 = new ArrayList<String>();
        private List<String> myData3 = new ArrayList<String>();


        public MyArrayAdapter(Context context, int textViewResourceId,
                List<String> objects, List<String> objects1,
                List<String> objects2, List<String> objects3) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub

            context = getContext();

            myData = objects;
            myData1 = objects1;
            myData2 = objects2;
            myData3 = objects3;
            }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View v = convertView;

            if (v == null) {

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.list, null);
                // Log.d(EKSI, "Getting the inflater from the system context");
            }

            String sid = myData.get(position);
            String sname = myData1.get(position);
            String dob = myData2.get(position);
            String sstatus = myData3.get(position);


                TextView entryTextView = (TextView) v.findViewById(R.id.id1);

                entryTextView.setText(sid);


                TextView entryTextView1 = (TextView) v
                        .findViewById(R.id.id2);
                entryTextView1.setText(sname);


                TextView entryTextView2 = (TextView) v
                        .findViewById(R.id.id3);
                entryTextView2.setText(dob);


                TextView entryTextView3 = (TextView) v
                        .findViewById(R.id.id4);
                entryTextView3.setText(sstatus);

            return v;
        }
    }

I implement this function not base ListView that can do this;

  • Custom loading view,override onDraw menthod

  • Add loading view at the end of LinearLayout view. When loading view is show, it should call some methods to get data from server by HTTP.

you just need to inflate an xml in

  lstView.addFooterView(R.layout.footer);

and handle onclick of the button residing in footer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM