2

我在设置项目时遇到问题ListView,我正在使用Async更新项目的任务。这是我到目前为止所做的。

Async Task onPostExecute()

@Override
    protected void onPostExecute(String result) {
        notifyList = new ArrayList<HashMap<String, String>>();

        try {

            JSONObject rootObj = new JSONObject(result);
            JSONObject jSearchData = rootObj.getJSONObject("notifications");

            int maxlimit = 5;

            for (int i = 0; i < maxlimit; i++) {
                JSONObject jNotification0 = jSearchData.getJSONObject(""
                        + i + "");

                String text = jNotification0.getString("text");
                String amount = jNotification0.getString("amount");
                String state = jNotification0.getString("state");

                System.out.println(text);
                System.out.println(amount);
                System.out.println(state);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put("text", text);
                map.put("amount", amount);

                notifyList.add(map);
            }

            if (notification_adapter != null) {
                notification_list.setAdapter(new CustomNotificationAdapter(
                        notifyList));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的 CustomNotification 类,它扩展BaseAdapter

public class CustomNotificationAdapter extends BaseAdapter {

    public ArrayList<HashMap<String, String>> notificationData = new ArrayList<HashMap<String, String>>();

    public CustomNotificationAdapter(
            ArrayList<HashMap<String, String>> notificationData) {
        this.notificationData = notificationData;
    }

    @Override
    public int getCount() {

        return notificationData.size();
    }

    @Override
    public Object getItem(int position) {

        return notificationData.get(position).get("text").toString();
    }

    @Override
    public long getItemId(int position) {

        return notificationData.get(position).get("text").hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        LayoutInflater inflater = getLayoutInflater();

        vi = inflater.inflate(R.layout.custom_notification_list, null);
        TextView notificationText = (TextView) findViewById(R.id.notificationText);
        TextView notificationAmount = (TextView) findViewById(R.id.notificationPoint);
        notificationText
                .setText(notificationData.get(position).get("text"));
        notificationAmount.setText(notificationData.get(position).get(
                "amount"));

        return vi;

    }

}

NotificationAdapter 类扩展SimpleAdapter

public class NotificationAdapter extends SimpleAdapter {

List<Map<String, String>> cur_list = new ArrayList<Map<String, String>>();

public NotificationAdapter(Context context,
        List<? extends Map<String, ?>> data, int resource, String[] from,
        int[] to) {
    super(context, data, resource, from, to);

}
}

我可以从中获取所有数据,JSONResponse但无法在列表中显示。我错过了什么?

任何形式的帮助将不胜感激。

4

2 回答 2

0

notification_adapter.notifyDataSetChanged()在更新适配器的数据后调用或删除,if (notification_adapter != null) {如果你想要它容易和坏。

更新您的数据:

 public void updateData(ArrayList<HashMap<String, String> notificationData){
       this.notificationData = notificationData;
       this.notifyDataSetChanged();
}

在您的适配器内部,并将其称为:notification_adapter.updateData(notifyList);

于 2013-06-14T12:02:40.270 回答
0

尝试替换这个:

if (notification_adapter != null) {
                notification_list.setAdapter(new CustomNotificationAdapter(
                        notifyList));
            }

有了这个:

notification_adapter  = new CustomNotificationAdapter(notifyList);
notification_list.setAdapter(notification_adapter);

即使notification_adapter之前为空,这也会将适配器设置为新的 JSON 数据。

于 2013-06-14T12:02:51.620 回答