3

我已经实施了一个listview onScrollListener. 我有 20 个数据将显示在 a 中,listview但我只想先显示 10 个数据。所以,如果我滚动列表,它将显示接下来的 10 个数据。我的代码没有错误,但滚动时无法更新数据。请更正我的代码并立即需要您的帮助。谢谢。

    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 bil_awal = 10;
    int awal;

    int itemsPerPage = 10;
    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+"");

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

        awal = list.size()-bil_awal;
        for(int i=0;i<awal; 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));
                }
            }
        }

        adap = new ListAdapter(listData);

        View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
        lv = getListView();
        lv.addFooterView(footerView);
        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);
            }
        });

        lv.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");
            listData = new ArrayList<Map<String,String>>();

            try { Thread.sleep(1000);
            } catch (InterruptedException e) {}

            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));
                    }
                }
            }
            runOnUiThread(returnRes);
        }
    };

    private Runnable returnRes = new Runnable() {
        @Override
        public void run() {
            adap = new ListAdapter(listData);
            lv.setAdapter(adap);
            adap.notifyDataSetChanged();

            loadingMore = false;
        }
    };

}
4

1 回答 1

0

你可以使用 pulltorefresh,这里是它的开源项目https://github.com/chrisbanes/Android-PullToRefresh

于 2012-10-23T00:34:20.490 回答