android永远不会从json结束列表

时间:2012-10-09 04:02:30

标签: android android-layout android-listview

我想从服务器列出其数据列表。但我只想显示前10个列表,如果有一些数据则加载更多。在这里我的代码:

  

mylist.java

    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;

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

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

    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");        

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

        ListView 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)ctx.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) {

                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() {
            loadingMore = true;

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

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Log.d("Gagal Load :", e.toString());
            }
            for (int i=0; i<itemsPerPage; 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() {
            if (listData != null && listData.size() > 0) {
//              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));
//                      }
//                  }
//              }
                for(int i=0; i<listData.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));
                    }}
                }
                setTitle(String.valueOf(adap.getCount())+ "items");

                adap.notifyDataSetChanged();

                loadingMore = false;
            }
        }
    };
}

我已经尝试但仍然没有为我工作。请纠正我的错。谢谢你提前。

1 个答案:

答案 0 :(得分:0)

更改,

for(int i=0;i<list.size();i++)

有了这个,

for(int i=0;i<list.size()&& i<=10;i++){
//your code for data loading
list.size()-10;
}