Preface
This article implements the use of ViewHolder to refresh a certain data without refreshing all the data every time. Let’s not say much below, let’s take a look at the detailed introduction.
Implementation method
Inherit BaseAdapter and create a new ViewHolder class.
public class TestListAdapter extends BaseAdapter { private Context mContext; private List<String> strList; public TestListAdapter(Context context, List<String> list) { super(); = context; = list; } @Override public int getCount() { // TODO Auto-generated method stub return (); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (null == convertView) { convertView = (mContext).inflate(, null); holder = new ViewHolder(); = (TextView) (.textView_id); = (TextView) (.textView_str); (holder); } else { holder = (ViewHolder) (); } (position + ""); String str = (position); (str); return convertView; } private static class ViewHolder { private TextView iDText; private TextView strText; } public void updataView(int posi, ListView listView) { int visibleFirstPosi = (); int visibleLastPosi = (); if (posi >= visibleFirstPosi && posi <= visibleLastPosi) { View view = (posi - visibleFirstPosi); ViewHolder holder = (ViewHolder) (); String txt = ().toString(); txt = txt + "++;"; (txt); (posi, txt); } else { String txt = (posi); txt = txt + "++;"; (posi, txt); } } }
In Activity, callupdateView()
Method, refresh the data.
public class MainActivity extends Activity { private MainActivity mContext; private EditText idEdit; private TextView textView; private List<String> strList = new ArrayList<String>(); private ListView listView; private TestListAdapter ListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mContext = this; for (int i = 0; i < 100; i++) { ("test data"); } idEdit = (EditText) findViewById(.edittext_id); textView = (TextView) findViewById(.textview_modify); listView = (ListView) findViewById(); ListAdapter = new TestListAdapter(mContext, strList); (ListAdapter); //Dynamic refresh (new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String idStr = ().toString(); int idInt = (idStr); (idInt, listView);//Dynamic modification } }); } }
Give the layout file:
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <EditText android: android:layout_width="200dp" android:layout_height="wrap_content" android:hint="put modify id" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dynamic Modification" android:textColor="#123456" /> </LinearLayout> <ListView android: android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.