listivew 动态刷新单个item

使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据。

继承BaseAdapter,新建ViewHolder类。

  1. public class TestListAdapter extends BaseAdapter {  
  2.     private Context mContext;  
  3.   
  4.     private List<String> strList;  
  5.   
  6.     public TestListAdapter(Context context, List<String> list) {  
  7.         super();  
  8.         this.mContext = context;  
  9.         this.strList = list;  
  10.     }  
  11.   
  12.     @Override  
  13.     public int getCount() {  
  14.         // TODO Auto-generated method stub  
  15.         return strList.size();  
  16.     }  
  17.   
  18.     @Override  
  19.     public Object getItem(int position) {  
  20.         // TODO Auto-generated method stub  
  21.         return position;  
  22.     }  
  23.   
  24.     @Override  
  25.     public long getItemId(int position) {  
  26.         // TODO Auto-generated method stub  
  27.         return position;  
  28.     }  
  29.   
  30.     @Override  
  31.     public View getView(int position, View convertView, ViewGroup parent) {  
  32.         // TODO Auto-generated method stub  
  33.         ViewHolder holder = null;  
  34.         if (null == convertView) {  
  35.             convertView = LayoutInflater.from(mContext).inflate(R.layout.line, null);  
  36.   
  37.             holder = new ViewHolder();  
  38.             holder.iDText = (TextView) convertView.findViewById(R.id.textView_id);  
  39.             holder.strText = (TextView) convertView.findViewById(R.id.textView_str);  
  40.   
  41.             convertView.setTag(holder);  
  42.         } else {  
  43.             holder = (ViewHolder) convertView.getTag();  
  44.         }  
  45.   
  46.         holder.iDText.setText(position + "");  
  47.         String str = strList.get(position);  
  48.         holder.strText.setText(str);  
  49.   
  50.         return convertView;  
  51.     }  
  52.   
  53.     private static class ViewHolder {  
  54.         private TextView iDText;  
  55.   
  56.         private TextView strText;  
  57.     }  
  58.   
  59.     public void updataView(int posi, ListView listView) {  
  60.         int visibleFirstPosi = listView.getFirstVisiblePosition();  
  61.         int visibleLastPosi = listView.getLastVisiblePosition();  
  62.         if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {  
  63.             View view = listView.getChildAt(posi - visibleFirstPosi);  
  64.             ViewHolder holder = (ViewHolder) view.getTag();  
  65.   
  66.             String txt = holder.strText.getText().toString();  
  67.             txt = txt + "++;";  
  68.             holder.strText.setText(txt);  
  69.             strList.set(posi, txt);  
  70.         } else {  
  71.             String txt = strList.get(posi);  
  72.             txt = txt + "++;";  
  73.             strList.set(posi, txt);  
  74.         }  
  75.     }  
  76. }  

在Activity中,调用updateView()方法,刷新数据。

  1. public class MainActivity extends Activity {  
  2.     private MainActivity mContext;  
  3.   
  4.     private EditText idEdit;  
  5.   
  6.     private TextView textView;  
  7.   
  8.     private List<StringstrList = new ArrayList<String>();  
  9.     private ListView listView;  
  10.     private TestListAdapter ListAdapter;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         mContext = this;  
  17.   
  18.         for (int i = 0; i 100; i++) {  
  19.             strList.add("test data");  
  20.         }  
  21.   
  22.         idEdit = (EditText) findViewById(R.id.edittext_id);  
  23.         textView = (TextView) findViewById(R.id.textview_modify);  
  24.   
  25.         listView = (ListView) findViewById(R.id.listview);  
  26.         ListAdapter = new TestListAdapter(mContext, strList);  
  27.         listView.setAdapter(ListAdapter);  
  28.   
  29.         //动态刷新  
  30.         textView.setOnClickListener(new OnClickListener() {  
  31.   
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 // TODO Auto-generated method stub  
  35.                 String idStr = idEdit.getText().toString();  
  36.                 int idInt = Integer.parseInt(idStr);  
  37.                 ListAdapter.updataView(idInt, listView);//动态修改  
  38.             }  
  39.         });  
  40.     }  
  41. }  

给出布局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#FFFFFF"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="40dp"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <EditText  
  14.             android:id="@+id/edittext_id"  
  15.             android:layout_width="200dp"  
  16.             android:layout_height="wrap_content"  
  17.             android:hint="put modify id" />  
  18.   
  19.         <TextView  
  20.             android:id="@+id/textview_modify"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="动态修改"  
  24.             android:textColor="#123456" />  
  25.     </LinearLayout>  
  26.   
  27.     <ListView  
  28.         android:id="@+id/listview"  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="wrap_content" >  
  31.     </ListView>  
  32.   
  33. </LinearLayout>  
原文地址:https://www.cnblogs.com/wangzehuaw/p/5237429.html