Android:notifyDataSetChanged();不起作用
我在服务器中有一个数据库,从平板电脑中我从数据库中的一个表中获取一些值。我将此信息正确加载到列表中,但我想知道为什么在发生更改时,即使使用Android:notifyDataSetChanged();不起作用,android,refresh,adapter,Android,Refresh,Adapter,我在服务器中有一个数据库,从平板电脑中我从数据库中的一个表中获取一些值。我将此信息正确加载到列表中,但我想知道为什么在发生更改时,即使使用notifyDataSetChanged(),也不会发生任何事情。我必须说,为了加载数据,y使用AsyncTaskClass 所以,我的问题是,我不知道是否使用notifyDataSetChanged();方法,因为如果有更改,我希望刷新图像。下面是该类代码的某些部分: @Override public void onCreate(Bundle sav
notifyDataSetChanged(),也不会发生任何事情代码>。我必须说,为了加载数据,y使用AsyncTaskClass
所以,我的问题是,我不知道是否使用notifyDataSetChanged();方法,因为如果有更改,我希望刷新图像。下面是该类代码的某些部分:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_candidatos);
candidatosList = new ArrayList<HashMap<String, String>>();
new CargarCandidatos().execute();
}
// public void timer(){
// new CountDownTimer(tiempo, 100) {
//
// public void onTick(long millisUntilFinished) {
//
// }
//
// public void onFinish() {
// // new CargarCandidatos().execute();
//
// }
// }.start();}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class CargarCandidatos extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Monitorizacion.this);
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_candidatos, "GET", params);
Log.d("Candidatos: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
candidatos = json.getJSONArray(TAG_CANDIDATOS);
for (int i = 0; i < candidatos.length(); i++) {
JSONObject c = candidatos.getJSONObject(i);
// Storing each json item in variable
String nserie = c.getString(TAG_NSERIE);
String dni = c.getString(TAG_DNI);
String nombre = c.getString(TAG_NOMBRE);
String test = c.getString(TAG_TEST);
String pregunta = c.getString(TAG_PREGUNTA);
String bateria = c.getString(TAG_BATERIA);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NSERIE, nserie);
map.put(TAG_DNI, dni);
map.put(TAG_NOMBRE, nombre);
map.put(TAG_TEST, test);
map.put(TAG_PREGUNTA, pregunta);
map.put(TAG_BATERIA, bateria);
// adding HashList to ArrayList
candidatosList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
Monitorizacion.this, candidatosList,
R.layout.list_item, new String[] { TAG_NSERIE,
TAG_DNI, TAG_NOMBRE, TAG_TEST, TAG_PREGUNTA, TAG_BATERIA},
new int[] { R.id.id, R.id.dni, R.id.nombre, R.id.test, R.id.pregunta, R.id.bateria});
setListAdapter(adapter);
adapter.notifyDataSetChanged();
// timer();
}
});
}
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局,所有候选对象);
CandidatesList=新的ArrayList();
新的CargarCandidatos().execute();
}
//公共无效计时器(){
//新的倒计时器(tiempo,100){
//
//公共void onTick(长毫秒未完成){
//
// }
//
//公共无效onFinish(){
////新建CargarCandidatos().execute();
//
// }
//}.start();}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类CargarCandidatos扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(Monitorizacion.this);
设置消息(“加载…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url_candidates,“GET”,params);
Log.d(“candidateos:,json.toString());
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
candidateos=json.getJSONArray(TAG_candidateos);
对于(int i=0;ivalue
地图放置(TAG_NSERIE,NSERIE);
地图放置(标记为DNI,DNI);
地图放置(标签名称,名称);
地图放置(标签测试,测试);
地图放置(TAG_PREGUNTA,PREGUNTA);
地图放置(塔格贝特里亚,贝特里亚);
//将哈希列表添加到ArrayList
候选名单。添加(地图);
}
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
适配器=新的SimpleAdapter(
这个,候选人名单,
R.layout.list_项,新字符串[]{TAG_NSERIE,
标签DNI,标签名称,标签测试,标签PREGUNTA,标签BATERIA},
新int[]{R.id.id,R.id.dni,R.id.nombre,R.id.test,R.id.pregunta,R.id.bateria});
setListAdapter(适配器);
adapter.notifyDataSetChanged();
//定时器();
}
});
}
}
}
适配器定义布局的组成!
->setListAdapter():定义ListView/GridView/Gallery的适配器。。。
但是您需要指定数据
我建议您在“onCreate”或构造函数中初始化“setListAdapter”。
将数据设置到适配器后(例如:adapter.setItem(yourData))
现在!您应该调用notifyDataSetChanged!
因为您已经更改了数据,但视图没有刷新,notifydatasetchanged()重新加载视图的内容(ListView/GridView/Gallery…)
为了获得良好的实践和正确的理解,我建议您使用“baseAdapter”使用“自定义适配器”
阅读并完成本教程(我通过本教程学习):
阅读文档:您需要编辑的内容是
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
Monitorizacion.this, candidatosList,
R.layout.list_item, new String[] { TAG_NSERIE,
TAG_DNI, TAG_NOMBRE, TAG_TEST, TAG_PREGUNTA, TAG_BATERIA},
new int[] { R.id.id, R.id.dni, R.id.nombre, R.id.test, R.id.pregunta, R.id.bateria});
setListAdapter(adapter);
adapter.notifyDataSetChanged();
// timer();
}
});
进入OnCreate()。并从Asynctask返回列表CandidatesList。而不是设置更新候选列表的计时器。其中一个主要原因是notifyDataSetChanged()
对您不起作用-是
适配器失去了对列表的引用
当您第一次初始化适配器时
会引用数组列表
,并将其传递给其超类。但是,如果重新初始化现有的arrayList
,它将丢失引用,因此,适配器的通信通道将丢失
在适配器中创建和添加新列表时
。艾尔
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
list = dataSource.getList();
list = dataSource.getList();
this.subscriber = myAdapter.getSubscriber(); // keep for unsubscribe in destroy
dataSource.subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribe(this.subscriber);
public class MyListAdapter extends RecyclerView.Adapter<LocationListAdapter.ViewHolder> {
private List<ListItem> mDataset = new ArrayList<>();
public Subscriber<ListItem[]> getSubscriber() {
return Subscribers.create(new Action1<ListItem[]>() {
@Override
public void call(ListItem[] listItems) {
mDataset.clear();
mDataset.addAll(Arrays.asList(listItems));
notifyDataSetChanged();
}
});
}
......
private val demoList: MutableList<AnyClass> = mutableListOf()
.add
.addAll
.remove