narahari_arjun
narahari_arjun

Reputation: 643

Listview displaying only last item

I have written a code to display json items into listview , when i debug the code i can see the data properly , when i am setting the list to the listview i am only seeing the last item

Code i tried :

 try {
       foodintervallist.clear();
       final String result = response.body().string();
       JSONObject jsonObject = new JSONObject(result);
       String data = jsonObject.getString("data");
       JSONArray foodintervalarray = new JSONArray(data);
       HashMap<String, String> menuMap = 
       new HashMap<String, String>();
       for (int j = 0; j < foodintervalarray.length(); j++) {
            String key1 = "";
            JSONObject jsonObject1 =  
            foodintervalarray.getJSONObject(j);
            final String food_interval =  
            jsonObject1.getString(FOOD_INTERVAL);
            if (jsonObject1.isNull(ITEM_NAME)) {
                item_name = "";
            } else {
                item_name = jsonObject1.getString(ITEM_NAME);            
            }

             if (!menuMap.containsKey(food_interval)) {
                  menuMap.put(food_interval, item_name);
             } else {
                  String key = menuMap.get(food_interval);
                  key1=key+","+item_name;
                  menuMap.put(food_interval, key1);
             }

              runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                foodintervallist.add(menuMap);
                                listViewAdapter = new ListViewAdapter(Diet_Order_Activity_New.this, foodintervallist);
                                listView.setAdapter(listViewAdapter);
                                listViewAdapter.notifyDataSetChanged();
                            }
                        });
}

My BaseAdapter class

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    inflater = (LayoutInflater) 
    activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.table_row, parent, 
    false);
    resultp = fooditervallist.get(position);
    txtFoodInterval = (TextView) 
    itemView.findViewById(R.id.foodInterval);
    txtFoodItem = (TextView) itemView.findViewById(R.id.foodItems);
    for (Map.Entry<String, String> entry : resultp.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        txtFoodInterval.setText(key);
        Toast.makeText(activity, "" + key, Toast.LENGTH_LONG).show();
        txtFoodItem.setText(value);
        // do what you have to do here
        // In your case, an other loop.
    }

    return itemView;
}

I am declaring the foodintervallist globally in my main activity class and also the listviewadapter i am initializing inside oncreate method

I am getting the data inside my arraylist , but i am able to display only the last item , what to do ?

Thanx

Upvotes: 0

Views: 2817

Answers (3)

Uttam Panchasara
Uttam Panchasara

Reputation: 5865

Put the below code outside the loop // set adapter outside the loop

listViewAdapter = new ListViewAdapter(Diet_Order_Activity_New.this,foodintervallist);
listView.setAdapter(listViewAdapter);
listViewAdapter.notifyDataSetChanged();

Upvotes: 0

smory
smory

Reputation: 324

I would recommend to check your implementation of Adapter's getCount(). As it is not provided how it looks, I would looked there... it should be like:

public int getCount (){ return fooditervallist.get(0).size() }

As you provide a list with only one item. Also I see there some issues in getView() :

  1. fooditervallist.get(position); --> don't use position there, your list has always only one item therefore use 0 instead otherwise you'll get null pointer
  2. your for loop is setting the txtFoodInterval and txtFoodItem with the all values in the Map.Set which might result in all list items having the same value ... instead of for loop you should use a "position" parameter here which is not possible with HashMap as order is not predicable. Use LinkedHashMap instead to have correct order and logic needs to be adjusted

Nevertheless I would implement it differently:

  1. JSON parsing - I would create a new object model for holding the data

class FoodItem { int interval; String name; // getters and setters here }

  1. I would put these items in the list you put your map

  2. In adapter you can then use this object quite easily like without any for loop like:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    inflater = (LayoutInflater) 
    activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.table_row, parent, false);
    FoodItem item = fooditervallist.get(position);
  txtFoodInterval = (TextView) itemView.findViewById(R.id.foodInterval);  
    txtFoodItem = (TextView) itemView.findViewById(R.id.foodItems);
  txtFoodInterval.setText(item.interval) ;
  txtFoodItem.setText(item.name);
    return itemView;

}

Also I would recommend to have a look on Retrofit library. It will make your life easier...

Upvotes: 1

VNS
VNS

Reputation: 44

put your Hashmap inside the for loop. Because when you use globally the data will be overrided and you get only last item.

Upvotes: 0

Related Questions