Android 在多个活动中使用单个阵列适配器的多个实例
我在一个新闻阅读器上工作,我有两个主要的活动,主要的和故事。Main调用asynctask并获取所有rss源,然后在完成后显示标题。故事显示特定提要中的文章。main中有一个导航栏,用于调用故事 我下载和解析的文章位于提要的ArrayList的hashmap中。我只有一个适配器类arrayAdapter。一切正常,每次创建新的listview时,我都会创建单独的arrayAdapter实例,但问题是当我从故事返回到主视图并单击列表中的任何内容时,特别是当故事中的项目少于主视图中的标题时,它会崩溃: java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保适配器的内容不是从后台线程修改的,而是仅从UI线程修改的。[在带有适配器(class package.package.ArticleListAdapter)的ListView(21301099654,class android.widget.ListView)中] 我认为我的错误来自适配器的行为不像单独的实例,尽管我认为我已经将它们作为单独的实例。我四处寻找这方面的帮助,但我看到的大部分是人们从后台线程更改数据,而不是在单独的活动中 不幸的是,这段代码被改写了,一部分是为了可读性,另一部分是因为我是为其他人写的,封闭源代码yadda yaddaAndroid 在多个活动中使用单个阵列适配器的多个实例,android,android-activity,android-adapter,Android,Android Activity,Android Adapter,我在一个新闻阅读器上工作,我有两个主要的活动,主要的和故事。Main调用asynctask并获取所有rss源,然后在完成后显示标题。故事显示特定提要中的文章。main中有一个导航栏,用于调用故事 我下载和解析的文章位于提要的ArrayList的hashmap中。我只有一个适配器类arrayAdapter。一切正常,每次创建新的listview时,我都会创建单独的arrayAdapter实例,但问题是当我从故事返回到主视图并单击列表中的任何内容时,特别是当故事中的项目少于主视图中的标题时,它会崩溃
class main{
public HashMap<Integer, ArrayList<ArticleHolder>> allArticles;
public ArticleListAdapter ArtListAdapter;
public void onCreate(Bundle savedInstanceState) {
allArticles = new HashMap<Integer, ArrayList<ArticleHolder>>();
myApp appstate = (myApp)getApplicationContext();
appState.setParsedArticles(allArticles); //Added this for universal availability of the articles
//download things
//background.execute();
//...
}
private class downloader extends Asynctask ...{
...
onPostExecute(){
ArticleListAdapter = new ArticleListAdapter(context c, list_item L, allArticles.get(1));
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(ArtListAdapter);
}
}
class stories{
public HashMap<Integer, ArrayList<ArticleHolder>> allArticles;
public ArticleListAdapter ArtListAdapter;
public void onCreate(Bundle savedInstanceState) {
myApp appstate = (myApp)getApplicationContext();
allArticles = appState.getParsedArticles();
//set layout
//get article to display from Bundle extras
ListView LV = (ListView) findViewById(R.id.listView1);
ArtListAdapter = new ArticleListAdapter(StoriesScreenActivity.this, R.layout.article_list_item, allCleanArticles.get(buttonFeedNum));
LV = (ListView)findViewById(R.id.listView1);
LV.setAdapter(ArtListAdapter);
}
}
好的,所以我接受了绝地武士的建议,尝试添加notifyDataSetChanged(),但我不知道如何让它与我的自定义适配器啮合。但这让我走上了正确的道路,因为我意识到主屏幕上的listview在返回时没有得到更新。这让我开始考虑使用onResume方法,经过一些尝试和错误之后,我最终调用了代码,将视图与onResume中的适配器关联起来。这里的诀窍是,应用程序在打开时崩溃,因为代码没有准备好,所以我阻止onResume中的代码运行,直到应用程序准备就绪。据我所知,这似乎解决了问题。谢谢你的帮助 在异步任务中使用:
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
}
因为它有用户界面的访问权限。
对于ListView,请使用ListView.notifyDataSetChanged()
希望这有帮助试试这篇文章,你的AsyncTask.doInBackground()方法会有所帮助。
protected Boolean doInBackground(String... params) {
boolean succeeded = false;
String downloadPath = null;
for(Integer num: feedArray){
downloadPath = "example.com/rss/"+Integer.toString(num);
doSax(downloadPath, num);
}
for(Integer num: feedArray){
currentDirtyFeedArticles = allDirtyArticles.get(num);
for(ArticleHolder dirtyArticle: currentDirtyFeedArticles){
dirtyArticle.setTitle(textCleaner(dirtyArticle.getTitle()));
//some text cleaning to make it look pretty
}
}
succeeded = true;
return succeeded;
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
}