0

我正在编写一个应用程序,从 RSS 提要中获取信息,使用 DOM 解析器(客户端 RSS 提要的问题)对其进行解析,然后在 ListView 中显示解析的对象。

出于某种原因,getView()没有被称为...

这是活动的代码:

public class NoPicList extends Activity {
    ListView list;
    NoPicAdapterV2 adapter2;
    ProgressDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.no_pic_list);
        list = (ListView) findViewById(R.id.noPicListView);

        // get the request from the main activity
        Bundle b = getIntent().getExtras();
        String request = b.getString("REQUEST");

        mDialog = new ProgressDialog(this);
        mDialog.setCancelable(false);
        mDialog.setMessage("Lodaing Data");
        mDialog.show();

        new GetNewsAndCalendar().execute(request);
    }

    @Override
    protected void onPause() {
        mDialog.dismiss();
        super.onPause();
    }

    class GetNewsAndCalendar extends
        AsyncTask<String, Void, ArrayList<Message>> {

        @Override
        protected ArrayList<Message> doInBackground(String... params) {
            String url = params[0];
            DOMFeedParser parser = new DOMFeedParser(url);
            return parser.parse();
        }

        @Override
        protected void onPostExecute(ArrayList<Message> result) {
            adapter2 = new NoPicAdapterV2(NoPicList.this, R.layout.no_pic_list_item, result);
            list.setAdapter(adapter2);
            mDialog.dismiss();
            }

    }

}

这是适配器的代码:

public class NoPicAdapterV2 extends ArrayAdapter<NewAndCalendar> {
    private ArrayList<Message> data;
    private LayoutInflater inflator;
    private Activity mActivity;

    public NoPicAdapterV2(Context context, int textViewResourceId,
        ArrayList<Message> result) {
        super(context, textViewResourceId);
        data = (ArrayList<Message>) result;
        mActivity = (Activity) context;
        inflator = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null)
            vi = inflator.inflate(R.layout.no_pic_list_item, parent, false);

        TextView title = (TextView) vi.findViewById(R.id.noPicTitle);
        TextView subtitle = (TextView) vi.findViewById(R.id.noPicSubtitle);
        TextView id = (TextView) vi.findViewById(R.id.noPicID);

        title.setText(data.get(position).getTitle().toString());
        subtitle.setText(data.get(position).getDate().toString());
        id.setText(data.get(position).getDescription());

        return vi;
    }
}

DOMFeedParser 代码是根据IBM 开源 Java XML 解析器文章构建的。

非常感谢...

4

3 回答 3

3

您应该覆盖(在您的自定义适配器中)

getCount()

并返回大小data以让适配器工作。或者您调用 ArrayAdapter 构造函数data

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
于 2012-04-19T10:04:43.210 回答
0

您的 ArrayAdapter 不知道数组的内容,因此计数为 0。

改用这个超级构造函数:http: //developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20java.util.List%3CT%3E%29

像这样 :

public NoPicAdapterV2(Context context, int textViewResourceId,
    ArrayList<Message> result) {
    // Here
    super(context, textViewResourceId, result);
    data = (ArrayList<Message>) result;
    mActivity = (Activity) context;
    inflator = (LayoutInflater) mActivity
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
于 2012-04-19T10:36:05.163 回答
0

我希望下面的代码能够工作,只需向 super(...) 添加一个参数

public NoPicAdapterV2(Context context, int textViewResourceId,
        ArrayList<Message> result) {
        super(context, textViewResourceId,result);
        data = (ArrayList<Message>) result;
        mActivity = (Activity) context;
        inflator = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
于 2012-04-19T11:32:40.797 回答