CustomListAdapter上的网络图像

时间:2015-10-07 06:12:15

标签: java android listadapter

我有一个自定义列表视图,其中包含国家/地区列表及其标记。我已设法获取列表中的国家/地区名称列表,但整个列表是在XML中设置的1个标志。

我想获得实现的标志列表,但我有点迷失,因为我可以将图像放入列表中。

请参阅下面的代码:

CountryAdapter.java

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

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.search_results_row_list, parent, false);
        holder = new CountryHolder();
        holder.countryName = (TextView) convertView.findViewById(R.id.country_text);
        holder.countryFlag = (ImageView) convertView.findViewById(R.id.country_flag);
        convertView.setTag(holder);
    } else {
        holder = (CountryHolder) convertView.getTag();
    }
    holder.countryName.setText(countryArrayList.get(position).getCountryName());
//        holder.countryFlag.setImageResource(countryArrayList.get(position).getImgId());
    return convertView;
}

网络电话:

public void getCountries(Activity activity) {

    RequestQueue requestQueue = Volley.newRequestQueue(activity.getApplicationContext());
    CustomNetwork jsObjRequest = new CustomNetwork(url, createRequestSuccessListener(), createRequestErrorListener());

    requestQueue.add(jsObjRequest);
}

//     Method called when there is an error in the network call
//    ----------------------------------------Error Listener for Request---------------------------------------
private Response.ErrorListener createRequestErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Networking", "Error is at: " + error.toString());
        }
    };
}

private Response.Listener<JSONArray> createRequestSuccessListener() {
    return new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.i("Networking", "Response is: " + response.toString());
            countriesArrayList = new ArrayList<>();
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject tempObject = response.getJSONObject(i);
                    String countryName = tempObject.getString("name");
                    String countryFlag = tempObject.getString("flag_32");  //This "flag_32" will return the name of the file on the website im hosting the file
                    countriesArrayList.add(new Countries(countryName, countryFlag));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            setCountriesArrayList(countriesArrayList);
            mCallBack.callBack();
        }
    };

}

0 个答案:

没有答案