ArrayAdapter中的getView()没有从AsyncTask调用

时间:2013-01-28 19:57:21

标签: java android android-listview android-asynctask

在我的一个Android活动中,我有一个ListView lvFeedsListlistView中的每个行元素将包含3个textViews - RSSFeedName,publishDate& feedLength

Feed的内容将从HTTPRsponse中重新审核。 我在AsyncTask

中提取此回复

所以,在doInBackground()中,我发送了HTTPRequest&收到&解析了响应&准备了包含上述3个信息的ArrayList

然后仅在doInBackground()内,我正在创建自定义ArrayAdapter,以便在行元素中形成3 TextViews。 我的目的是在ListView in onPostExecute()上设置此适配器。

但是,当我运行应用程序时,ListView不会显示任何内容。

我试图调试&似乎getView()类中的ArrayAdapter没有被调用。 (但我不确定这是不是这个原因。)

这是代码,抱歉长度......似乎有必要。

活动代码:

public class GenericFeedsActivity extends Activity{

    private ListView lvFeedsList;
    private ArrayList<FeedsClass> feedList;

    protected void onCreate(Bundle savedInstanceState) {
        ...
        lvFeedsList = (ListView) findViewById(R.id.lvFeedsList);
        lvFeedsList.setOnItemClickListener(this);
        lvFeedsList.setEnabled(false);
        ...
        new AsyncResponseHandler(this).execute();
    }

    class AsyncResponseHandler extends AsyncTask {
        Context context;
        FeedListAdapter adapter;

        public AsyncResponseHandler(Context c) {
            this.context = c;
        }

        @Override
        protected Object doInBackground(Object... params) {
            ...
            /* 
             *  Sending HTTPRequest to a URL & getting list of feeds
             *  Saving this list of feeds in a ArrayList -feedList, containing elements of type FeedsClass (declared above)
             *  Below line parses the HTTPResponse XML & stores various information in feedList.
             */
            feedList = utils.parseRssResponseXML(in);   // Working fine, geeting elements
            adapter = new FeedListAdapter(
                GenericFeedsActivity.this, feedList);
            in.close();
            return null;
        }

        protected void onPostExecute(Object e) {
            // Setting Arrayadapter
            lvFeedsList.setAdapter(adapter);
            lvFeedsList.setEnabled(true);
        }
    }
}

适配器代码:

public class FeedListAdapter extends ArrayAdapter {

    private Context context;
    private ArrayList<FeedsClass> feedList;

    public FeedListAdapter(Context c, ArrayList<FeedsClass> data) {
        super(c, R.layout.rowlayout);
        this.context = c;
        this.feedList = data;
    }

    class ViewHolder {
        TextView tvFeedName;
        TextView tvFeedPubDate;
        TextView tvFeedLength;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;
            if (row == null) {

                LayoutInflater inflator = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflator.inflate(R.layout.rowlayout, null);
                holder = new ViewHolder();

                holder.tvFeedName = (TextView) row.findViewById(R.id.tvFeedName);
                holder.tvFeedPubDate = (TextView) row.findViewById(R.id.tvFeedPubDate);
                holder.tvFeedLength = (TextView) row.findViewById(R.id.tvFeedLength);

                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }
            // Getting values of feedName, publishDate & feedLength
            String feedName = feedList.get(position).getTitle();
            String feedDate = feedList.get(position).getPublishDate();
            String feedLength = feedList.get(position).getStreamLength();

            holder.tvFeedName.setText(feedName);
            holder.tvFeedPubDate.setText(feedDate);
            holder.tvFeedLength.setText(feedLength);
        }
        return row;
    }
}

2 个答案:

答案 0 :(得分:3)

问题在于您是ArrayAdapter的子类。这不起作用,因为ArrayAdapter在内部认为您的数据中没有任何元素;它不仅神奇地知道查看lvFeedsList变量,因为它使用的数据集是内部的。

相反,在构造函数中确保调用此构造函数:

适配器代码:

public FeedListAdapter(Context c, ArrayList<FeedsClass> data) {
    super(c, R.layout.rowlayout, data); // add 'data'
    this.context = c;
    this.feedList = data;
}

这将使一切正常。

答案 1 :(得分:1)

adapter.notifyDataSetChanged()

可以在AsyncResponseHandler.onPostExecute()的最后帮助。如果不是 - 检查保存适配器数据的ArrayList是否为空。