gpt4 book ai didi

android - 适配器的内容已更改但 ListView 未收到通知 - 使用 AsyncTask

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:24 24 4
gpt4 key购买 nike

我正在使用 AsyncTask 来设置我的项目和东西,在 postExecute 中我调用一个同步方法来执行此操作

mAllResultsAdapter.setItems(mAllResultsItem);
mAllResultsAdapter.notifyDataSetChanged();
mListView.invalidate();
mListView.requestLayout();

大多数时候,如果我下载新数据并更新我的 ListView /适配器,它就可以正常工作,而且非常好。但有时它会以某种方式崩溃,任何人都可以帮助我解决这个问题。

这是我的崩溃日志。

  03-19 14:15:02.170: E/AndroidRuntime(23242): FATAL EXCEPTION: main
03-19 14:15:02.170: E/AndroidRuntime(23242): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131034153, class android.widget.ListView) with Adapter(class com....tracebuzz.allresults.AllResultsAdapter)]
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.ListView.layoutChildren(ListView.java:1556)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.AbsListView.onLayout(AbsListView.java:1993)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewRoot.performTraversals(ViewRoot.java:1288)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewRoot.handleMessage(ViewRoot.java:2066)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.os.Looper.loop(Looper.java:132)
03-19 14:15:02.170: E/AndroidRuntime(23242): at android.app.ActivityThread.main(ActivityThread.java:4126)
03-19 14:15:02.170: E/AndroidRuntime(23242): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 14:15:02.170: E/AndroidRuntime(23242): at java.lang.reflect.Method.invoke(Method.java:491)
03-19 14:15:02.170: E/AndroidRuntime(23242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-19 14:15:02.170: E/AndroidRuntime(23242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-19 14:15:02.170: E/AndroidRuntime(23242): at dalvik.system.NativeStart.main(Native Method)

我应该用我的代码更新我的问题,所以在这里。

public class AllResultsActivity extends TBActivity{

public String identifier,
feedIdentifier,
apiKey,
tagID,
titleName;

private ProcesReuest procesReuest;

public ArrayList<Integer> tempIDHolder = new ArrayList<Integer>();
public ArrayList<AllResultsItem> mAllResultsItem;
public AllResultsAdapter mAllResultsAdapter;
public ListView mListView;
private Context mContext;
private ViewFlipper mViewFlipper;
private TextView mTitleText;
private int itemsForLoad = 50;
private boolean firstRun = true;

private ProgressBar mLoadItemsIndicator;
private int visibleThreshold = 5;
private int mPage = 1;
private int previousTotal = 0;
private boolean loading = true;
private SharedPreferences mSettings;
private Editor mEdit;
private View footerView;
private int itemHolder;


@Override
public void onBackPressed() {
super.onBackPressed();
procesReuest.cancel(true);
AllResultsActivity.this.finish();
}
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.all_results_view);
super.onCreate(savedInstanceState);
getExtras();
mContext = this;
itemsForLoad = 50;

loadItems(apiKey);
initListview();
}

private void initListview() {
mAllResultsItem = new ArrayList<AllResultsItem>();
mAllResultsAdapter = new AllResultsAdapter(this, mAllResultsItem);
mListView.setAdapter(mAllResultsAdapter);
mAllResultsAdapter.notifyDataSetChanged();
}

@Override
public void bindResources() {
mLoadItemsIndicator = (ProgressBar)findViewById(R.id.all_results_view_more_items_indicator);
mListView = (ListView)findViewById(R.id.all_results_listview);
mViewFlipper = (ViewFlipper)findViewById(R.id.all_results_view_viewflipper);
mTitleText = (TextView) findViewById(R.id.all_results_view_title_text);
mSettings = getSharedPreferences(WBAConstants.TRACEBUZZ, MODE_PRIVATE);
mEdit = mSettings.edit();

}

@Override
public void bindListeners() {
mListView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view, int id, long arg3) {

String url = (String) view.getTag(R.string.item_url);
Intent intnt = new Intent(AllResultsActivity.this, WebviewWithControllsActivity.class);
if(url.contains("twitter")) {
int UQID = (Integer)view.getTag(R.id.twitter_result_identifier);
String table = (String)view.getTag(R.string.twitter_table);

intnt.putExtra("UQID", UQID);
intnt.putExtra("apiKey", apiKey);
intnt.putExtra("table", table);
}
View bar = (View)findViewById(R.id.all_results_title_bar);
int barHeight = bar.getHeight();
intnt.putExtra("barHeight", barHeight);
intnt.putExtra("URL", url);
AllResultsActivity.this.startActivity(intnt);
}
});

mListView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("","totalItemCount = " + totalItemCount);
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
itemHolder = previousTotal;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
mLoadItemsIndicator.setVisibility(View.VISIBLE);
mListView.invalidate();

mPage++;
loadItems(apiKey);
loading = true;
} else {

}
}
});
}

private void loadItems(String passedApiKey) {

procesReuest = new ProcesReuest();
procesReuest.execute(identifier, feedIdentifier, passedApiKey);
}

public void getExtras() {
Bundle extras = getIntent().getExtras();

feedIdentifier = null;
tagID = null;
apiKey = null;
if(extras != null) {
identifier = extras.getString("identifier");
feedIdentifier = extras.getString("feedidentifier");
apiKey = extras.getString("apiKey");
titleName = extras.getString("tag_name");
tagID = extras.getString("tag_id");
if((titleName != null) && (!titleName.equals("null"))) {
mTitleText.setText(titleName);
}
}
}

public void displayDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Error");
dialog.setMessage("Failed to contact server, check your Internet connection or try again later");
dialog.setNegativeButton("back", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
AllResultsActivity.this.finish();
}
});
dialog.show();
}


private synchronized void addSyncedItems() throws InterruptedException {

if(firstRun){
mAllResultsAdapter.setItems(mAllResultsItem);
firstRun = false;
}
mAllResultsAdapter.notifyDataSetChanged();

mListView.invalidate();
mLoadItemsIndicator.setVisibility(View.GONE);

}

/** Subclass for processing the ResultReqesut for the specified type **/

private class ProcesReuest extends AsyncTask<String, Void, Exception>{
Downloader downloader = new Downloader();
JSONObject result = null;
List<NameValuePair> postParams;
boolean timeOut;
@Override
protected Exception doInBackground(String... params) {
Log.d("","doInBackground " + apiKey);

try {
postParams = new ArrayList<NameValuePair>(2);
postParams.add(new BasicNameValuePair("api_key",apiKey));
postParams.add(new BasicNameValuePair("num","100"));
postParams.add(new BasicNameValuePair("feed_id", feedIdentifier));
postParams.add(new BasicNameValuePair("page", Integer.toString(mPage)));
postParams.add(new BasicNameValuePair("order", "time DESC"));
postParams.add(new BasicNameValuePair("tag_id", tagID));
postParams.add(new BasicNameValuePair("format", "json"));
Log.d("","postParams = " + postParams);
downloadJSON(postParams);
proccesJSON();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Exception exception) {
Log.d("","onPostExecute");
try {
addSyncedItems();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(!result.getJSONObject("results").getString("result_count").equals("0")){
mViewFlipper.setDisplayedChild(2);
} else {
Log.d("","View 3");
mViewFlipper.setDisplayedChild(2);
}
} catch (Exception e) {
// TODO: handle exception
}
}

private void proccesJSON() throws JSONException, IOException {
String UIDHolderString = mSettings.getString(WBAConstants.UIDHOLDER, "empty");
JSONArray arrayHolder = null;

if(UIDHolderString.equals("empty")){
arrayHolder = new JSONArray();
} else {
arrayHolder = new JSONArray(UIDHolderString);
}

if(result != null){
JSONObject feedResults = result.getJSONObject("results").getJSONObject("result");
Log.d("","" + feedResults);
String uniqueItemID = null;
Iterator<String> feedResultIter = feedResults.keys();

while (feedResultIter.hasNext()) {
uniqueItemID = (String) feedResultIter.next();
Log.d("","" + uniqueItemID);

if(!uniqueItemID.equals("metaresult")) {
AllResultsItem item = new AllResultsItem();


if(UIDHolderString.contains(uniqueItemID)) {
item.setNewFeed(0);
} else {
item.setNewFeed(1);
arrayHolder.put(uniqueItemID);
}

JSONObject resultItem = feedResults.getJSONObject(uniqueItemID);
String typeFeed = resultItem.getString("type");
if(!typeFeed.contains("twitter") && typeFeed != null){
if(resultItem.has("tags") && resultItem.getString("tags")!= null) {
JSONObject tags = resultItem.getJSONObject("tags");

Iterator<String> tagIter = tags.keys();
ArrayList<String> tagHolder = new ArrayList<String>();
while (tagIter.hasNext()) {
String tagID = (String) tagIter.next();
String tagName = tags.getString(tagID);
tagHolder.add(tagName);
}
item.setTagNames(tagHolder.toString());
}
item.setType(typeFeed);
if((resultItem.has("title")) && (!resultItem.isNull("title")) && (!resultItem.getString("title").equals("null") )) {
item.setResultTitle(resultItem.getString("title"));
}
if(resultItem.has("unique_id")) {
item.setResultIdentifier(Integer.parseInt(resultItem.getString("unique_id")));
}
if(resultItem.has("src_link") && (resultItem.getString("src_link") != null)) {
item.setMessageURL(resultItem.getString("src_link"));
}
if(resultItem.has("date_time")) {
item.setDate( resultItem.getString("date_time"));
}
if(resultItem.has("description")) {
item.setMessage(resultItem.getString("description"));
}

} else if(resultItem.getString("type").contains("twitter")){

Icon icon = new Icon();
if((resultItem.has("title")) && (!resultItem.isNull("title")) && (!resultItem.getString("title").equals("null"))) {
item.setUserTwitter(resultItem.getString("title"));
}
if(resultItem.has("unique_id")) {
item.setIdentifier(Integer.parseInt(resultItem.getString("unique_id")));
}
if(resultItem.has("src_link") && (resultItem.getString("src_link") != null)) {
item.setMessageURL(resultItem.getString("src_link"));

}
if(resultItem.has("date_time")) {

item.setDate(resultItem.getString("date_time"));
}
if(resultItem.has("description")) {
item.setMessageText(resultItem.getString("description"));

}
if(resultItem.has("image_link")) {
if(!resultItem.has("null")){
icon.setImageURL(resultItem.getString("image_link"));
}
}
if(resultItem.has("table")) {
item.setTable(resultItem.getString("table"));
}

item.setType(typeFeed);
item.addIconsObject(icon);
if(resultItem.has("tags") && resultItem.getString("tags")!= null) {
JSONObject tags = resultItem.getJSONObject("tags");

Iterator<String> tagIter = tags.keys();
ArrayList<String> tagHolder = new ArrayList<String>();
while (tagIter.hasNext()) {
String tagID = (String) tagIter.next();
String tagName = tags.getString(tagID);

tagHolder.add(tagName);

}
item.setTagNames(tagHolder.toString());
}
}
mAllResultsItem.add(item);
}
}
mEdit.putString(WBAConstants.UIDHOLDER, arrayHolder.toString());
mEdit.commit();
}
}

public boolean downloadJSON(List<NameValuePair>postParams) {
try {
result = downloader.getJSONFromURL("http://www.example.com/webservice/get_feed_results.php", true, postParams);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
}

Syncronized 方法的原因只是为了尝试让适配器和 ListView 同时更新。

最佳答案

根据代码 fragment ,有一些标记为@Override的方法(bindResources()和bindListeners()等)既没有继承自官方API,也没有在AllResultsActivity中显式调用。假设这些已正确实现并涉及父类(super class)或其他地方。我在您的代码中看到的问题是:

@Override
public void onCreate(Bundle savedInstanceState) {
... ...

loadItems(apiKey); // <-- method start AsyncTask, and finially involve mAllResultsAdapter.notifyDataSetChanged();
initListview(); // <-- method that bind Adapter to ListView. i.e. mListView.setAdapter(mAllResultsAdapter);
}

问题是当 AysncTask 执行并调用 onPostExecute() 时(调用 mAllResultsAdapter.notifyDataSetChanged(); 时),适配器尚未绑定(bind)到 ListView。只需调用 initListview();在 loadItems(apiKey) 之前;应该使 java.lang.IllegalStateException: 消失。

更新:
我深入查看了您的代码,问题是绑定(bind)到适配器的 ArrayList 在后台线程中发生了更改:

// proccesJSON() is called in AsyncTask.doInBackground()
private void proccesJSON() throws JSONException, IOException {
... ...

mAllResultsItem.add(item); // <-- mAllResultsItem is bound to mAllResultsAdapter

... ...
}

您的适配器实际上是从后台线程修改的,与异常建议的完全一样。解决方案是将它移动到 UI 线程或简单地用 runOnUiThread() 包装它:

... ...

runOnUiThread(new Runnable() {
public void run() {
mAllResultsItem.add(item);
mAllResultsAdapter.notifyDataSetChanged();
}
}); // end of runOnUiThread

... ...

希望这对您有所帮助。

关于android - 适配器的内容已更改但 ListView 未收到通知 - 使用 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com