-1

I want to make a never ending list in my application. For first time, the list will show 8 and then if there are more data, the list will show the data based on list's length.

here's my code :

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

but this code still get an error. and here its error message :

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

I need your advice, links, comments and all of your helps...

Renjith
  • 5,783
  • 9
  • 31
  • 42
Reza Rachman
  • 143
  • 3
  • 15
  • exception is self-explanatory: use `AsyncTask` instead `Thread` ... and do UI stuff like `adap = new ListAdapter(listData); lv.setAdapter(adap);` in `onPostExecute` ... you can also check this http://stackoverflow.com/questions/1080811/android-endless-list – Selvin Oct 15 '12 at 09:37
  • how to i get the next data in list if there are more data than 8 ? – Reza Rachman Oct 15 '12 at 09:39
  • Try to have a look at [here](http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/) – Praveenkumar Oct 15 '12 at 10:01

1 Answers1

1

Try this. In your run method surround the setAdapter like this. it should help. The problem is you are trying to update Ui form background thread. You can't do that. You need to be in main thread when you update the UI.

 ResultRestoActivity.this.runOnUiThread(new Runnable() {

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

                    }
                });
Andro Selva
  • 53,910
  • 52
  • 193
  • 240