Detailed explanation of Android notifyDataSetChanged of dynamic update ListView case
- 2021-12-13 17:14:04
- OfStack
Sometimes we need to modify the generated list and add or modify data. notifyDataSetChanged () can notify Activity to update ListView without refreshing Activity again after modifying the array bound by the adapter. Today's example is to dynamically update ListView through Handler AsyncTask.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30px"
android:textSize="18sp"
></TextView>
import java.util.ArrayList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class main extends Activity {
/** Called when the activity is first created. */
ListView lv;
ArrayAdapter<String> Adapter;
ArrayList<String> arr=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.lv);
arr.add("123");
arr.add("234");
arr.add("345");
Adapter = new ArrayAdapter<String>(this,R.layout.playlist, arr);
lv.setAdapter(Adapter);
lv.setOnItemClickListener(lvLis);
editItem edit= new editItem();
edit.execute("0"," No. 1 1 Items ");// Put the first 1 The content of item should be changed to " No. 1 1 Items "
Handler handler=new Handler();
handler.postDelayed(add,3000);// Delay 3 Second execution
}
Runnable add=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
arr.add(" Increase 1 Items ");// Increase 1 Items
Adapter.notifyDataSetChanged();
}
};
class editItem extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
arr.set(Integer.parseInt(params[0]),params[1]);
//params What I get is 1 An array of, params[0] Here it is "0",params[1] Yes " No. 1 1 Items "
//Adapter.notifyDataSetChanged();
// You cannot call after adding Adapter.notifyDataSetChanged() Update UI , because with UI Not on the same thread
// Below onPostExecute Method will be used in the doBackground After execution by UI Thread invocation
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Adapter.notifyDataSetChanged();
// Complete execution, update UI
}
}
private OnItemClickListener lvLis=new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Triggered when an entry is clicked
//arg2 That is, the position of the item in the point
setTitle(String.valueOf(arr.get(arg2)));
}
};
}