2

I'm trying to do Autogrowin/Endless ListView but I have a problem with ArrayAdapter = update after downloading the data.

public class ListShowActivity  extends ListActivity {

    private ShowsAdapter m_adapter;
    private ArrayList<ShowsList> showList = new ArrayList<ShowsList>();

    // Adapter
    private class ShowsAdapter extends ArrayAdapter<ShowsList>
    {

        private ArrayList<ShowsList> items = new ArrayList<ShowsList>();

        public ShowsAdapter(Context context, int textViewResourceId, ArrayList<ShowsList> items)
        {
            super(context, textViewResourceId, items);  
            this.items = items;
        }

        public void appendList(ArrayList<ShowsList> results)
        {
            if((results != null) && (results.size() != 0))
            {
                m_adapter.add(results.get(0));
                m_adapter.notifyDataSetChanged();
                Log.i("test", "appendList: "+results.get(0).originalTitle);
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View v = convertView;
            TextView tt = null;
            TextView bt = null;
            ImageView iv = null;
            if (v == null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_show_item, null);

                tt = (TextView) v.findViewById(R.id.showOriginalTitle);
                bt = (TextView) v.findViewById(R.id.showTitle);
                iv = (ImageView) v.findViewById(R.id.showPoster);
            }

            ShowsList item = this.items.get(position);
            if (item != null) {
                if(tt != null){
                    tt.setText(item.originalTitle);
                }

                if(bt != null){
                    bt.setText(item.title);
                }

                if(iv != null)
                {
                    new DownloadImage().downloadImage(item.poster, iv);
                } 
            }

            return v;
        }
    }

    // Implement new OnScrollListener
    public class onScrollLoad implements OnScrollListener {

        private int lastOffset = 10;
        private int limit = 10;
        private int mPrevTotalItemCount = 0;

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            if (((firstVisibleItem + visibleItemCount) >= totalItemCount) && totalItemCount != mPrevTotalItemCount) {
                lastOffset = totalItemCount;
                mPrevTotalItemCount = lastOffset;
                limit += 1;
                ArrayList<ShowsList> results = new Api().getShows(lastOffset, limit);
                m_adapter.appendList(results);
                //new PagingRequest(m_adapter).execute(lastOffset, limit);
                Toast.makeText(getApplicationContext(), lastOffset+" / "+limit, Toast.LENGTH_SHORT).show();
            }
        }            

        public void onScrollStateChanged(AbsListView view, int scrollState) {
            Log.i("test", "onScrollStateChange()");
        }
    }

    // Implement AsyncTask for download items?
    /*
    private static class PagingRequest extends AsyncTask<Integer, Void, ArrayList<ShowsList>> {

        private ShowsAdapter mAdapter;

        public PagingRequest(ShowsAdapter adapter) {
           this.mAdapter = adapter;
        }

         protected ArrayList<ShowsList> doInBackground(Integer... params) {
             return new Api().getShows(params[0], params[1]);
         }

         protected void onPostExecute(ArrayList<ShowsList> result) {
             this.mAdapter.appendList(result);
         }
      }
      */

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_shows);

        TextView tv = (TextView) findViewById(R.id.headerbarText);
        tv.setText("List Shows");    

        // Get Items
        Api a = new Api();
        // First 10 items
        showList = a.getShows(0, 0);

        if(showList.isEmpty() == false)
        {   
            //showList =  new Api().getShows(0,0);
            m_adapter = new ShowsAdapter(this, R.layout.list_show_item, showList);
            setListAdapter(m_adapter);
            ListView lv = getListView();
            lv.setOnScrollListener(new onScrollLoad());            

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Intent intent = new Intent(ListShowActivity.this, ShowActivity.class);
                    //intent.putExtra("showID", getShowsList.get(position).ID);
                    startActivity(intent);
                }
            });
        }else{
           Toast.makeText(getApplicationContext(), "NoData", Toast.LENGTH_SHORT).show();
        }
    }
}

I pasted code without working Asynctask. In "theory"^^ everything works (appendList Log return correct value), but i as say i have problem with update ShowsAdapter (Download new image + and insert content to ListView)

darkus
  • 21
  • 1
  • The problem is in inserting a content or in downloading an image? Does it work if to comment the code which uses images? And why do you use the `m_adapter` variable instead of `this`? – vortexwolf Dec 04 '11 at 14:01
  • problem with inserting content and downloading an image is fix for now. Temporarily I improved my code, so now i can download image and everythinks works almost good. Now I have problem with too many request. For example if i have one request (downloading one item form server), i have two unnecessery items (from list). So i have downloaded item + two unnecessery items :) – darkus Dec 04 '11 at 14:37
  • Why the other items are unnecessary? If the service returns all of them then it is reasonable. But I don't know all details, be it as it is. To reduce the number of requests you can create a local array of already downloaded items and don't load them once again. – vortexwolf Dec 04 '11 at 18:24

0 Answers0