Question

I'm facing problem with setting up the items in ListView, I'm using an Async task for updating the items. Here is what I have done so far.

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();
        }
    }

Here is my CustomNotification class which extends 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 class which extends 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);

}
}

I'm able to get all the data from JSONResponse but I'm not able to show it on the list. What am I missing?

Any kind of help will be appreciated.

Was it helpful?

Solution

Try replacing this:

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

with this:

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

This will set the adapter to the new JSON data even if notification_adapter was previously null.

OTHER TIPS

Call notification_adapter.notifyDataSetChanged() after updating your adapter's data or remove the if (notification_adapter != null) {if you want it easy and bad.

to update your data:

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

Inside your adapter, and call it like: notification_adapter.updateData(notifyList);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top