Shreyas Patne
Shreyas Patne

Reputation: 5

The adaptor not showing the Array list items in List view but show the string in Toast msg which used for test

I write this code to fetch data from URL and get the title from each JSON object and show this in the list view. The problem is data is stored successfully in productlist variable, which I check it by Log.d and Toast massage method. but it not show in listview.

  public void onResponse(JSONArray response) {
            try {
                    for(int i=0;i<response.length();i++) {
                        JSONObject productfromjson = response.getJSONObject(i);
                        productList.add(productfromjson.getString("title"));
                    }
                    Log.d("products", String.valueOf(productList));
                    Toast.makeText(MainActivity.this, String.valueOf(productList), Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();

                }
        }

the following is the adaptor code ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, productList); myproducts.setAdapter(arrayAdapter);

Upvotes: 0

Views: 47

Answers (3)

Nika
Nika

Reputation: 1049

You have to let the adapter know that the data in the list has changed by adding this line into your code:

arrayAdapter.notifyDataSetChanged();

So your code should look like this:

public void onResponse(JSONArray response) {
     try {
         for(int i=0;i<response.length();i++) {
             JSONObject productfromjson = response.getJSONObject(i);
             productList.add(productfromjson.getString("title"));
          }
          arrayAdapter.notifyDataSetChanged();
          Log.d("products", String.valueOf(productList));
          Toast.makeText(MainActivity.this, String.valueOf(productList), Toast.LENGTH_SHORT).show();
     } catch (JSONException e) {
          e.printStackTrace();
     }
}

Upvotes: 0

Haris Abdullah
Haris Abdullah

Reputation: 119

brother you don't have used notifyDataSetChanged(); Here is how you will do it.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, productList); 
 myproducts.setAdapter(arrayAdapter);

public void onResponse(JSONArray response) {
 try {
     for(int i=0;i<response.length();i++) {
         JSONObject productfromjson = response.getJSONObject(i);
         productList.add(productfromjson.getString("title"));
      }
      arrayAdapter.notifyDataSetChanged();
      Toast.makeText(getApplicationcontext, String.valueOf(productList), Toast.LENGTH_SHORT).show();
 } catch (JSONException e) {
      e.printStackTrace();
 }

}

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

You have to notify adapter about dataset changes using notifyDataSetChanged() or notifyItemRangeInserted() like below

public void onResponse(JSONArray response) {
     try {
         for(int i=0;i<response.length();i++) {
             JSONObject productfromjson = response.getJSONObject(i);
             productList.add(productfromjson.getString("title"));
          }
          arrayAdapter.notifyDataSetChanged();
          Log.d("products", String.valueOf(productList));
          Toast.makeText(MainActivity.this, String.valueOf(productList), Toast.LENGTH_SHORT).show();
     } catch (JSONException e) {
          e.printStackTrace();
     }
}

Upvotes: 1

Related Questions