在android中添加更多按钮到listview

时间:2013-06-28 01:51:04

标签: android

我正在研究android项目。在我的应用程序中,我从php webservice获取数据。我将我的数据添加到不同的arraylists并将它们设置为listadapter。但我的问题是在列表中显示数据需要很长时间。因此,现在我想显示前10个项目,并希望在屏幕底部保留一个loadmore按钮。单击加载更多按钮后,需要显示下10个项目。请允许任何人在这方面帮助我。我真的很感激这个帮助。 提前谢谢。

代码:

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;
        }
    }

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我实现这个函数不是基于ListView可以做到这一点;

  • 自定义加载视图,覆盖onDraw menthod

  • 在LinearLayout视图的末尾添加加载视图。当显示加载视图时,它应调用一些方法通过HTTP从服务器获取数据。

答案 2 :(得分:0)

你只需要在

中充气xml
  lstView.addFooterView(R.layout.footer);

并处理驻留在页脚中的按钮