我得到的異常爲 「* java.lang.IllegalStateException:適配器的內容已更改,但ListView沒有收到通知。續 *適配器的耳鼻喉科是不是從後臺線程修改,但只能從UI線程。「Listview和適應拋出「java.lang.IllegalStateException:」
我的代碼有兩個線程一個是SAX解析,另一種是線程擴展的AsyncTask類
這每次ArrayList內容被改變時都檢查內容。然後調用ada pter.notifyDatasetChange()方法在onProgressUpdate()..
但我希望輸出作爲解析器進行解析,尊重的元素應該是 添加到列表視圖....請幫助我guyzzz .... 。 我有代碼...
公共類SearchProperty擴展活動 {
boolean isDone;
List<Property> properties = new ArrayList<Property>();
ListView list;
ArrayAdapter<Property> adapter;
InputStream in;
ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlay);
list = (ListView) findViewById(R.id.mylistview);
list.setPadding(0, 20, 0, 20);
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Please Wait...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
adapter = new ArrayAdapter<Property>(this,
android.R.layout.simple_list_item_1, properties);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(SearchProperty.this , "Please Wait... Application is under progress..." , Toast.LENGTH_SHORT).show();
}
});
FileParserTask task = new FileParserTask();
Thread thread = new Thread(task);
thread.start();
SetTimeoutTask task11 = new SetTimeoutTask();
task11.execute();
System.out.println("I m done!!");
}
**// This thread is checking arraylist time to time after 50 ms and call on progressUpdate**
private class SetTimeoutTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
dialog.show();
};
protected String doInBackground(String... urls)
{
// Log.e("...","in do InBackground.....");
try {
for (;;) {
if (isDone)
break;
publishProgress();
Thread.sleep(50);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
@Override
protected void onProgressUpdate(Void... values)
{
//Log.e("...","in onprogressupdate.....");
super.onProgressUpdate(values);
if(list.getAdapter()!=null)
adapter.notifyDataSetChanged();
}
}
**// this thread is doing parsing of xml file in background....**
class FileParserTask implements Runnable {
@Override
public void run() {
try {
in = getAssets().open("property.xml");
PropertyHandler myhandler = new PropertyHandler(properties);
XMLReader xmlReader = SAXParserFactory.newInstance()
.newSAXParser().getXMLReader();
xmlReader.setContentHandler(myhandler);
xmlReader.parse(new InputSource(in));
isDone = true;
System.out.println("Done!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// EstateParser.parse(in,properties,adapter);
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我不認爲這將工作,因爲doInBackgroundThread仍然在後臺,所以解析那裏仍然會修改另一個線程中的適配器。他只需要在OnProgressUpdate方法中觸摸適配器。 – Kaediil
右先生....但是,如果我將代碼移動到AsyncTask,那麼我得到的內容添加到ListView的結尾......我想要的內容應該添加到列表視圖解析進步..但不是在結束。 – NullPointerException
我同意@ Kaedil..you hv一些設計更改,因爲我在我更新的答案中提到 – havexz