How to update some data in a Listview without using notifyDataSetChanged()?

后端 未结 3 1376
野的像风
野的像风 2020-12-07 16:51

I\'m trying to create a ListView with a list of downloading tasks.

The downloading tasks are managed in a Service (DownloadService). Everyt

3条回答
  •  庸人自扰
    2020-12-07 17:38

    Although its not an answer to your question but one optimization that can be done in your getView() method is this, Instead of creating and setting click listener every time like this:

    holder.downloadStateBtn.setTag(position); 
    holder.downloadStateBtn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) { 
                int position = (Integer) v.getTag(); 
                 // your current normal click handling
            }
        });
    

    You can just create it once as class variable and set it while creating row's View:

    final OnClickListener btnListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) { 
            int position = (Integer) v.getTag();
            // your normal click handling code goes here
        }
    }
    

    and then in getView():

     if (v == null) {
            v = mLayoutInflater.inflate(R.layout.saved_show_list_item, parent, false);
            // your ViewHolder stuff here 
            holder.downloadStateBtn.setOnClickListener(btnClickListener);//<<<<<
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
    

    oh and don't forget to set tag on this button in getView() as you are already doing:

    holder.downloadStateBtn.setTag(position);
    

提交回复
热议问题