android加载更多列表

时间:2012-10-15 09:34:23

标签: android android-listview

我想在我的应用程序中创建一个永无止境的列表。第一次,列表将显示8,然后如果有更多数据,列表将根据列表的长度显示数据。

这是我的代码:

    package com.example.main;

    public class ResultRestoActivity extends ListActivity {

        Context ctx;

        List<Map<String,String>> listData =  new ArrayList<Map<String,String>>();
        List <Map<String,String>> list = new ArrayList<Map<String,String>>();

        ListAdapter adap;
        ListView lv;

        Button btnLoadMore;

        String bank,group,city,merchant,address,telpon,handphone,bb;

        TextView tv_GName,tv_city,tv_merchant,tv_address,tv_telpon,tv_handphone,tv_bb;

        ImageButton tabs_nearby, tabs_all;

        int itemsPerPage = 4;
        boolean loadingMore = false;

        @Override
        public void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            group = getIntent().getStringExtra("GROUP");
            bank = getIntent().getStringExtra("BANK");
            city = getIntent().getStringExtra("CITY");
            merchant = getIntent().getStringExtra("MERCHANT");
            address = getIntent().getStringExtra("ADDRESS");
            telpon = getIntent().getStringExtra("TELPON");
            handphone = getIntent().getStringExtra("HANDPHONE");
            bb = getIntent().getStringExtra("BB");

            ctx = this;
            setContentView(R.layout.activity_restoresult);

            tv_GName = (TextView)findViewById(R.id.tvRestoResult);
            tv_GName.setText(""+group+"");

    //      tabs_all = (ImageButton)findViewById(R.id.imgbtn_semua);
    //      tabs_all.setOnClickListener(new OnClickListener() {
    //          @Override
    //          public void onClick(View v) {
    //              // TODO Auto-generated method stub
    //              tabs_all.setImageResource(R.drawable.tabs_semua_active);
    ////                loadStart();
    //          }
    //      });

    //      tabs_nearby = (ImageButton)findViewById(R.id.imgbtn_nearby);
    //      tabs_nearby.setOnClickListener(new OnClickListener() {
    //          @Override
    //          public void onClick(View v) {
                    // TODO Auto-generated method stub
    //              Toast.makeText(ctx, "Coming Soon", Toast.LENGTH_SHORT).show();
    //              tabs_nearby.setImageResource(R.drawable.tabs_terdekat_active);
    //              Intent i = new Intent(v.getContext(), NearbyComingSoon.class);
    //              startActivity(i);
    //              comingsoon();
    //          }
    //      });

            list = (ArrayList<Map<String,String>>) getIntent().getSerializableExtra("LIST");        

            for(int i=0;i<list.size(); i++){
                final Map<String, String> map = list.get(i);
                for (Map.Entry<String, String> entry : map.entrySet()) {
    //              String key = entry.getKey();
                    final String value = entry.getValue();
                    if(value.equalsIgnoreCase(bank)){
                        listData.add(list.get(i));
                    }
                }
            }

            lv = getListView();
            adap = new ListAdapter(listData);
            lv.setAdapter(adap);
            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View v, int position,
                        long arg3) {
                    merchant = ((TextView) v.findViewById(R.id.tv_merchantname)).getText()
                                .toString();

                    Intent i = new Intent(ResultRestoActivity.this, DetailRestoActivity.class);
                        i.putExtra("GROUP", group);
                        i.putExtra("CITY", city);
                        i.putExtra("MERCHANT", merchant);
                        i.putExtra("ADDRESS", address);
                        i.putExtra("TELPON", telpon);
                        i.putExtra("HANDPHONE", handphone);
                        i.putExtra("BB", bb);
                    startActivity(i);
                }
            });

            View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
            this.getListView().addFooterView(footerView);
            this.setListAdapter(adap);
            this.getListView().setOnScrollListener(new OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub
                    int lastInScreen = firstVisibleItem + visibleItemCount;

                    if((lastInScreen == totalItemCount) && !(loadingMore)){                 
                        Thread thread =  new Thread(null, loadMoreListItems);
                        thread.start();
                    }
                }
            });

            Thread thread = new Thread(null, loadMoreListItems);
                thread.start();
        }

        private Runnable loadMoreListItems = new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                loadingMore = true;

    //          list = (ArrayList<Map<String,String>>) getIntent().getSerializableExtra("LIST");

                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                }

                for(int i=0;i<itemsPerPage; i++){
                    final Map<String, String> map = list.get(i+2);
                    for (Map.Entry<String, String> entry : map.entrySet()) {
    //                  String key = entry.getKey();
                        final String value = entry.getValue();
                        if(value.equalsIgnoreCase(bank)){
                            listData.add(list.get(i));
                        }
                    }
                }
                adap = new ListAdapter(listData);
                lv.setAdapter(adap);
            }
        };          
    }

但此代码仍然出错。这里有错误信息:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我需要您的建议,链接,评论和所有帮助...

1 个答案:

答案 0 :(得分:1)

试试这个。在你的run方法中,像这样包围setAdapter。它应该有所帮助。问题是你正在尝试更新Ui表单后台线程。你不能这样做。更新UI时,您需要处于主线程中。

 ResultRestoActivity.this.runOnUiThread(new Runnable() {

                    public void run() {
                         adap = new ListAdapter(listData);
                         lv.setAdapter(adap);

                    }
                });