我正在使用自定义基本适配器从rss提要中获取标题,链接,描述,pubdate,guid。 我首先使用简单的数组适配器直接实现应用程序,它工作正常。但是在使用自定义基础适配器实现它时,我发现app停止了工作错误。
这是我的bean类 RssItem.java
package com.maulik.rss;
public class RssItem {
String _title;
String _link;
String _description;
String _pubdate;
String _guid;
// constructor
public RssItem(){
}
// constructor with parameters
public RssItem(String title, String link, String description, String pubdate, String guid){
this._title = title;
this._link = link;
this._description = description;
this._pubdate = pubdate;
this._guid = guid;
}
public void setTitle(String title){
this._title = title;
}
public void setLink(String link){
this._link = link;
}
public void setDescription(String description){
this._description = description;
}
public void setPubdate(String pubDate){
this._pubdate = pubDate;
}
public void setGuid(String guid){
this._guid = guid;
}
/**
* All GET methods
* */
public String getTitle(){
return this._title;
}
public String getLink(){
return this._link;
}
public String getDescription(){
return this._description;
}
public String getPubdate(){
return this._pubdate;
}
public String getGuid(){
return this._guid;
}
}
这是我的自定义基础适配器类 RssCustomListRssItemsAdapter
public class RssCustomListRssItemsAdapter extends BaseAdapter{
Context context;
List<RssItem> rssItems;
public RssCustomListRssItemsAdapter(Context context,List<RssItem>rssItems){
this.context=context;
this.rssItems=rssItems;
}
private class ViewHolder{
TextView url,title,date,description;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return rssItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return rssItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return rssItems.indexOf(rssItems.get(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder holder=null;
if(convertView==null){
convertView=inflater.inflate(R.layout.rss_item_list_row, null);
holder.url=(TextView) convertView.findViewById(R.id.page_url);
holder.title=(TextView) convertView.findViewById(R.id.title);
holder.date=(TextView) convertView.findViewById(R.id.pub_date);
holder.description=(TextView) convertView.findViewById(R.id.link);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
RssItem item=rssItems.get(position);
holder.url.setText(item.getLink());
holder.title.setText(item.getTitle());
holder.date.setText(item.getPubdate());
holder.description.setText(item.getDescription());
return convertView;
}
}
此活动称为 RSS_ListRSSItemsActivity
public class RSS_ListRSSItemsActivity extends ActionBarActivity implements
OnItemClickListener {
ActionBar ab;
private ProgressDialog pDialog;
RssCustomListRssItemsAdapter adapter;
List<RssItem> rssItem = new ArrayList<RssItem>();
RssParser rssParser = new RssParser();
RssFeed rssFeed;
ListView lv;
String title, description, link, pubdate, guid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_item_list);
ab = getSupportActionBar();
Intent i = getIntent();
Integer site_id = Integer.parseInt(i.getStringExtra("id"));
String actionBar_title = i.getStringExtra(title);
ab.setTitle(actionBar_title);
// Getting Single website from SQLite
RssDatabaseHandler rssDB = new RssDatabaseHandler(
getApplicationContext());
RssWebSite site = rssDB.getSite(site_id);
String rss_link = site.getRSSLink();
new loadRSSFeedItems().execute(rss_link);
// selecting single ListView item
lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(this);
}
/**
* Background Async Task to get RSS Feed Items data from URL
* */
class loadRSSFeedItems extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting all recent articles and showing them in listview
* */
@Override
protected String doInBackground(String... args) {
// rss link url
String rss_url = args[0];
// list of rss items
rssItem = rssParser.getRSSFeedItems(rss_url);
// looping through each item
for (RssItem item : rssItem) {
item.getTitle();
item.getLink();
item.getDescription();
item.getPubdate();
item.getGuid();
rssItem.add(item);
}
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
adapter = new RssCustomListRssItemsAdapter(RSS_ListRSSItemsActivity.this, rssItem);
lv.setAdapter(adapter);
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String args) {
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
RSS_DetailedRSSActivity.class);
// getting page url
String page_url = ((TextView) view.findViewById(R.id.page_url))
.getText().toString();
String title = ((TextView) view.findViewById(R.id.title)).getText()
.toString();
String date = ((TextView) view.findViewById(R.id.pub_date)).getText()
.toString();
String description = ((TextView) view.findViewById(R.id.link))
.getText().toString();
in.putExtra("page_url", page_url);
in.putExtra("title_url", title);
in.putExtra("date_url", date);
in.putExtra("description_url", description);
startActivity(in);
}
}
这是我的 logcat
I / Choreographer(18848):跳过137帧!应用程序可能在其主线程上做了太多工作。 W / dalvikvm(18848):threadid = 12:线程退出,未捕获异常(组= 0x40b89378) 致命异常:AsyncTask#2 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:307) 在java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:856) 引起:java.util.ConcurrentModificationException at java.util.ArrayList $ ArrayListIterator.next(ArrayList.java:569) at com.maulik.rss.RSS_ListRSSItemsActivity $ loadRSSFeedItems.doInBackground(RSS_ListRSSItemsActivity.java:88) at com.maulik.rss.RSS_ListRSSItemsActivity $ loadRSSFeedItems.doInBackground(RSS_ListRSSItemsActivity.java:1) 在android.os.AsyncTask $ 2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) ......还有5个
第88行表示(RssItem项目:rssItem){ 同样在预执行中我使用了进度对话框,我得到了泄漏的窗口错误。我确实花了一整天的时间来弄清楚这一点并在网上使用各种答案。请帮忙
答案 0 :(得分:1)
这可能是问题的一部分。在doInBackground()
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
AyncTask
有处理此类内容的方法,例如onProgressUpdate()
,可以使用doInBackground()
从publishProgress()
调用。或者onPostExecute()
也是一个好地方。
此外,我不知道这些RSS方法的作用,但如果它很重,那么它们应该移到你的AsyncTask
或不同的Thread
。
答案 1 :(得分:0)
我解决了我的错误。我创建了类型
的新实例列表与LT; RssItem&gt; itemss;
在doInBackground的for循环中,我将所有文本添加到这个新创建的List对象“itemss”中。
然后才使用
rssItems.add(itemss);
这基本上解决了我在设置custombaseadapter时遇到的问题!