0

我在函数 in 中设置了返回ArrayList包含 ListView 数据的doInBackground函数AsyncTask,并notifyDataSetCahnged()在 onPostExecute() 中 设置了

但是尽管arraylist中有数据,但daat不会反映在listview上?为什么 ?

代码:

public class E0XMLParsing_RSSExampleActivity extends Activity {

    ListView lvFeeds;
    FeedsAdapter feedsAdapter;
    ArrayList<Feed> listOfFeeds;
    ProgressDialog progress;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listOfFeeds = new ArrayList<Feed>();
        lvFeeds = (ListView) findViewById(R.id.listFeeds);
        feedsAdapter = new FeedsAdapter(getApplicationContext(), R.layout.row, listOfFeeds);
        String url = "http://www.aljazeera.com/Services/Rss/?PostingId=200772215035764169";
        progress = ProgressDialog.show(this, "Aljazeera RSS", 
                                    "Please wait while downloading the RSS feeds ...");
        new DownloadRSS().execute(url);
    }


    private class DownloadRSS extends AsyncTask<String,Integer,ArrayList<Feed>>{

        @Override
        protected ArrayList<Feed> doInBackground(String... urls) {
            XMLParser parser = new XMLParser();

            return parser.getFeedsList(urls[0]);
        }

        protected void onPostExecute(ArrayList<Feed> listOfFeeds2){
            progress.dismiss();
            listOfFeeds = listOfFeeds2;
            feedsAdapter.notifyDataSetChanged();
        }

    }

饲料适配器

public class FeedsAdapter extends ArrayAdapter<Feed>{

    public ArrayList<Feed> listOfFeeds= null;
    Context context;
    public FeedsAdapter(Context context,int textViewResourceId,ArrayList<Feed> feeds) {
        super(context,textViewResourceId,feeds);
        this.listOfFeeds = feeds;
        this.context = context;
    }

    @Override
    public View getView(int position, View view,ViewGroup parent){
        View v= view;
        if (v!=null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.row,null);
            Feed f = listOfFeeds.get(position);

            TextView lblTitle = (TextView) v.findViewById(R.id.lblTitle);
            TextView lblDescription = (TextView) v.findViewById(R.id.lblDescription);
            TextView lblDate = (TextView) v.findViewById(R.id.lblDate);

            lblTitle.setText(f.title);
            lblDescription.setText(f.description);
            lblDate.setText(f.date);
        }

        return v;
    }
4

3 回答 3

4

当您调用listOfFeeds = listOfFeeds2;它时,它将新对象引用设置为您的对象listOfFeeds并使在适配器上调用notifyDataSetChanged无用。为了解决这个问题,您可以在onPostExecute中做两件事。

第一的:

listOfFeeds.clear();
listOfFeeds.addAll(listOfFeeds2);
feedsAdapter.notifyDataSetChanged();

*在这种方法中,您正在清除listOfFeeds并从第二个对象推送项目,从而导致不更改适配器的数据对象引用。所以你需要调用 notifyDataSetChanged 来更新列表

第二:

feedsAdapter = new FeedsAdapter(getApplicationContext(),
       R.layout.row, listOfFeeds);
listOfFeeds.setAdapter(feedsAdapter);

*在这种方法中,您只需设置新的适配器。所以你不需要在这里调用 notifyDataSetChanged

于 2012-04-27T19:49:45.867 回答
0

我不确定这是否能回答你的问题,但为了更新你的 ListView,首先你必须更新适配器,然后在 ListView 设置适配器,最后再次绘制 ListView

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, myNewStringArray);
myListView.setAdapter(myAdapter);
myListView.invalidate() //if you perform the re-draw in UI thread
// OR myListView.postInvalidate() if you perform the re-draw in non-UI thread
于 2012-04-27T19:56:40.550 回答
-1

onPostExecute()中,将所有数据放入一个runOnUiThread()调用中:

runonuithread () {
    feedsAdapter.notifyDataSetChanged();
}
于 2012-04-27T19:34:11.900 回答