0

Hello i am having dificulties populate the custom row in a listView json data. The data obtained from Json it's ok if i put log.V or a textView with the proprety to .setText(JsonData) it shows the corect data. The problem is it does not populate the listView row. It looks like this

enter image description here

And i wat it to look like this enter image description here

Here is my code:

public class MainActivity extends AppCompatActivity {

private String currencyURL = "https://openexchangerates.org/api/latest.json?app_id=389cadf865504852b08a1759063621f5";
private String countryURL = "https://gist.githubusercontent.com/keeguon/2310008/raw/bdc2ce1c1e3f28f9cab5b4393c7549f38361be4e/countries.json";

private TextView currency, TXT;
private String TAG = "MainActivity::";

private ArrayList<Model> bucketCurrency = new ArrayList<>();
private ListView listView;
private ListViewAdapter listViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.activity_main_listView);
    currency = (TextView) findViewById(R.id.activity_main_country_currency_text);
    TXT = (TextView) findViewById(R.id.editText);


    getJson(currencyURL, countryURL);

    listViewAdapter = new ListViewAdapter(MainActivity.this, R.layout.list_view_custom_row, bucketCurrency);
    listViewAdapter.notifyDataSetChanged();
    listView.setAdapter(listViewAdapter);

}
 private void getJson(String URL_1_CURRENCY, String URL_2_COUNTRY) {

    final Model model = new Model();

    JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, URL_1_CURRENCY, (JSONObject) null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {

                        JSONObject ratesJsonObj = response.getJSONObject("rates");
                        String cursRon = ratesJsonObj.getString("RON");
                        String cursEuro = ratesJsonObj.getString("EUR");
                        String cursDolar = ratesJsonObj.getString("USD");

                        model.setCurrency(cursRon);
                        TXT.setText(cursRon);
                        Toast.makeText(MainActivity.this, "RON: " + cursRon, Toast.LENGTH_SHORT).show();

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

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.getMessage();
        }
    });


    AppController.getInstance().addToRequestQueue(jsonObjectRequest1);

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL_2_COUNTRY, (JSONObject) null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            try {

                for (int i = 179; i < 185 ; i++) {

                    JSONObject jsonObjectCountry = response.getJSONObject(i);
                    String countryROM = jsonObjectCountry.getString("name");
                    String countryCODE = jsonObjectCountry.getString("code");

                    model.setCountry(countryROM);
                    model.setCountryCode(countryCODE);

                    Log.v(TAG, countryROM + " " + countryCODE);
                }

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    AppController.getInstance().addToRequestQueue(jsonArrayRequest);

    bucketCurrency.add(model);
}

The AppController class:

    public class AppController extends Application {

    public static final String TAG = AppController.class
            .getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {

        super.onCreate();

        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueueModified(Request<T> req_1, Request<T> req_2) {
        req_1.setTag(TAG);
        getRequestQueue().add(req_1);
        req_2.setTag(TAG);
        getRequestQueue().add(req_2);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

The model class:

public class Model {
private String country;
private String countryCode;
private String currency;
private ImageView countryFlagImage;

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getCountryCode() {
    return countryCode;
}

public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}

public ImageView getCountryFlagImage() {
    return countryFlagImage;
}

public void setCountryFlagImage(ImageView countryFlagImage) {
    this.countryFlagImage = countryFlagImage;
}

public String getCurrency() {
    return currency;
}

public void setCurrency(String currency) {
    this.currency = currency;
}

}

And the ListView adapter class:

public class ListViewAdapter extends ArrayAdapter<Model> {

Activity activity;
int layoutResource;
ArrayList<Model> newData = new ArrayList<>();

public ListViewAdapter(Activity act, int resource, ArrayList<Model> data) {
    super(act, resource, data);

    activity = act;
    layoutResource = resource;
    newData = data;
    notifyDataSetChanged();

}

@Override
public int getCount() {
    return newData.size();
}

@Override
public Model getItem(int position) {
    return newData.get(position);
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

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

    View row = convertView;
    ViewHolder holder;

    if (row == null || (row.getTag()) == null){
        LayoutInflater inflater = LayoutInflater.from(activity);
        row = inflater.inflate(layoutResource, null);

        holder = new ViewHolder();

        holder.hCountry = (TextView) row.findViewById(R.id.custom_listview_country_text);
        holder.hCountryCode = (TextView) row.findViewById(R.id.custom_listview_country_code);
        holder.hCurrency = (TextView) row.findViewById(R.id.custom_listview_currency);
        holder.hFlag = (ImageView) row.findViewById(R.id.custom_listview_country_flag_image);

        row.setTag(holder);

    } else {
        holder = (ViewHolder) row.getTag();
    }

    holder.hModel = getItem(position);
    holder.hCountry.setText(holder.hModel.getCountry());
    holder.hCountryCode.setText(holder.hModel.getCountryCode());
    holder.hCurrency.setText(holder.hModel.getCurrency());


    return row;
}

public class ViewHolder {
    Model hModel;
    TextView hCountry;
    TextView hCountryCode;
    TextView hCurrency;
    ImageView hFlag;
}

}

XML custom row

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">

<ImageView
    android:id="@+id/custom_listview_country_flag_image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@color/gray_300"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:src="@mipmap/ic_launcher"/>

<TextView
    android:id="@+id/custom_listview_country_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/country"
    android:textSize="25dp"
    android:layout_toRightOf="@id/custom_listview_country_flag_image"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="20dp"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/custom_listview_country_code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/country_code"
    android:textSize="15dp"
    android:layout_toRightOf="@id/custom_listview_country_flag_image"
    android:layout_below="@id/custom_listview_country_text"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="20dp"
    android:textStyle="italic"/>

<TextView
    android:id="@+id/custom_listview_currency"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/country_currency"
    android:textSize="25dp"
    android:layout_toRightOf="@id/custom_listview_country_text"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="40dp"
    android:textStyle="bold"/>

5
  • try to put short your question! None have time to read all this! Commented May 20, 2016 at 8:16
  • I am triing to give all the details necesarry :) @NiravRanpara Commented May 20, 2016 at 8:18
  • bucketCurrency i think is null; because of async request... i am not sure... bucketCurrency.add(model); is on wrong place Commented May 20, 2016 at 8:23
  • 1
    Try to destroy and reinit the list view on each response, or maybe update it. Commented May 20, 2016 at 8:37
  • @Cliff - i have moved the bucketCurrency.add(model) in the for loop afther model.setCountryCode(countryCODE); The view knows how many rows to inflate but it just not sets the TextView... and it's all blank . Commented May 20, 2016 at 8:38

2 Answers 2

1

Try this!!!

private String cursRon;

private void getJson(String URL_1_CURRENCY, String URL_2_COUNTRY) {

Model model = null;

JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, URL_1_CURRENCY, (JSONObject) null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {

                    JSONObject ratesJsonObj = response.getJSONObject("rates");
                    cursRon = ratesJsonObj.getString("RON");
                    String cursEuro = ratesJsonObj.getString("EUR");
                    String cursDolar = ratesJsonObj.getString("USD");


                    TXT.setText(cursRon);
                    Toast.makeText(MainActivity.this, "RON: " + cursRon, Toast.LENGTH_SHORT).show();

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

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.getMessage();
    }
});


AppController.getInstance().addToRequestQueue(jsonObjectRequest1);

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL_2_COUNTRY, (JSONObject) null, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {

        try {

            for (int i = 179; i < 185 ; i++) {
                model = new Model();
                JSONObject jsonObjectCountry = response.getJSONObject(i);
                String countryROM = jsonObjectCountry.getString("name");
                String countryCODE = jsonObjectCountry.getString("code");
                model.setCurrency(cursRon);
                model.setCountry(countryROM);
                model.setCountryCode(countryCODE);

                Log.v(TAG, countryROM + " " + countryCODE);
               bucketCurrency.add(model);
            }

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

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

AppController.getInstance().addToRequestQueue(jsonArrayRequest);


}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should nest the requests, then move bucketCurrency.add(model); inside the onResponse of the second request Like this:

JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, URL_1_CURRENCY, (JSONObject) null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {

                        JSONObject ratesJsonObj = response.getJSONObject("rates");
                        String cursRon = ratesJsonObj.getString("RON");
                        String cursEuro = ratesJsonObj.getString("EUR");
                        String cursDolar = ratesJsonObj.getString("USD");

                        model.setCurrency(cursRon);
                        TXT.setText(cursRon);
                        Toast.makeText(MainActivity.this, "RON: " + cursRon, Toast.LENGTH_SHORT).show();

                        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL_2_COUNTRY, (JSONObject) null, new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {

                                try {

                                    for (int i = 179; i < 185 ; i++) {

                                        JSONObject jsonObjectCountry = response.getJSONObject(i);
                                        String countryROM = jsonObjectCountry.getString("name");
                                        String countryCODE = jsonObjectCountry.getString("code");

                                        model.setCountry(countryROM);
                                        model.setCountryCode(countryCODE);


                                        Log.v(TAG, countryROM + " " + countryCODE);

                                        //move  bucketCurrency.add(model); to here
                                        bucketCurrency.add(model);
                                    }

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

                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                            }
                        });

                        AppController.getInstance().addToRequestQueue(jsonArrayRequest);



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

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.getMessage();
        }
    });


    AppController.getInstance().addToRequestQueue(jsonObjectRequest1);

3 Comments

I moved the bucketCurrency.add(model) know i get 5 custom rows with no text in it. The setText seems not to be working i guess
Log the text before you set it in the adapter. Also checkfor your view in xml or in code if you set its visibility to gone or invisible
paste the xml of your list Item too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.