-1

我想在我的应用程序中列出一个永无止境的清单。第一次,列表将显示 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.

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

4

1 回答 1

1

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

 ResultRestoActivity.this.runOnUiThread(new Runnable() {

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

                    }
                });
于 2012-10-15T09:40:50.803 回答