我正在尝试将国旗添加到我的GridView
中,但它没有显示任何内容,我也没有在logcat中收到任何错误。
当我尝试调试其显示响应但未在GridView
中显示时。
这是我的代码:
private GridView gridView;
//ArrayList for Storing image urls and titles
private ArrayList<String> images;
private ArrayList<String> count;
private ArrayList<String> Id;
private ArrayList<String> country;
ArrayList<CountryDetails> al = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pmfanciers);
getSupportActionBar().hide();
mtoolbar = (ImageButton) findViewById(R.id.toolbar_new);
mtoolbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(PMFanciersActivity.this, PMDashboardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); //
return false;
}
});
gridView = (GridView) findViewById(R.id.gridView);
images = new ArrayList<>();
count = new ArrayList<>();
Id = new ArrayList<>();
country = new ArrayList<>();
getData();
}
private void getData() {
//Showing a progress dialog while our app fetches the data from url
final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show();
loading.dismiss();
try {
JSONObject jObj = new JSONObject(response);
JSONArray arr = jObj.getJSONArray("country_details");
/*JSONArray json = new JSONArray(response);*/
for (int i = 0; i < arr.length(); i++) {
//Creating a json object of the current index
JSONObject obj = null;
CountryDetails cd=new CountryDetails();
try {
//getting json object from current index
obj = arr.getJSONObject(i);
Id.add(String.valueOf(obj.getInt("country_code")));
count.add(obj.getString(TAG_COUNT));
images.add(obj.getString("country_flag"));
country.add(obj.getString("country_name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
//Creating GridViewAdapter Object
final PMFanciersAdapter pmFanciersAdapter = new PMFanciersAdapter(getApplicationContext(), images, count, Id, country);
//Adding adapter to gridview
runOnUiThread(new Runnable(){
@Override
public void run(){
// change UI elements here
gridView.setAdapter(pmFanciersAdapter);
pmFanciersAdapter.notifyDataSetChanged();
}
});
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
json输出:
{
"status_code": 200,
"status": "OK",
"status_message": "Success",
"country_details": [
{
"country_code": "AF",
"country_name": "America",
"country_iso": "AFG",
"country_flag": "http://........./128x128/af.png",
"calling_code": "93",
"fancier_count": 3
},
{
"country_code": "AL",
"country_name": "Africa",
"country_iso": "ALB",
"country_flag": "http://.......128x128/al.png",
"calling_code": "355",
"fancier_count": 0
},
这是我的gridview适配器..
public class PMFanciersAdapter extends BaseAdapter {
//Imageloader to load images
private ImageLoader imageLoader;
//Context
private Context context;
//Array List that would contain the urls and the titles for the images
private ArrayList<String> images;
private ArrayList<String> count;
private ArrayList<String> Id;
private ArrayList<String> country;
public PMFanciersAdapter(Context context, ArrayList CountryDetails){
//Getting all the values
this.context = context;
this.images = images;
this.count = count;
this.Id = Id;
this.country = country;
}
static class ViewHolder {
ImageView imageView;
TextView textView;
LinearLayout grid_id;
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int position) {
return images.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Creating a linear layout
View view = convertView;
final ViewHolder gridViewImageHolder;
// check to see if we have a view
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fanciers_grid_item, parent, false);
gridViewImageHolder = new ViewHolder();
gridViewImageHolder.imageView = (ImageView) view.findViewById(R.id.imageView1);
gridViewImageHolder.textView = (TextView) view.findViewById(R.id.text1);
gridViewImageHolder.grid_id = (LinearLayout) view.findViewById(R.id.grid_id);
view.setTag(gridViewImageHolder);
} else {
gridViewImageHolder = (ViewHolder) view.getTag();
}
NetworkImageView networkImageView = new NetworkImageView(context);
imageLoader = PMCustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.drawable.loader, android.R.drawable.ic_dialog_alert));
networkImageView = (NetworkImageView) gridViewImageHolder.imageView;
networkImageView.setDefaultImageResId(R.color.white);
networkImageView.setAdjustViewBounds(true);
networkImageView.setImageUrl(images.get(position), imageLoader);
gridViewImageHolder.textView.setText(count.get(position));
gridViewImageHolder.grid_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, PMMemberListingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("CountryID", Id.get(position));
intent.putExtra("countryName",country.get(position));
context.startActivity(intent);
}
});
return view;
}
}
CountryDetails类:
public class CountryDetails {
@SerializedName("country_code")
@Expose
private String countryCode;
@SerializedName("country_name")
@Expose
private String countryName;
@SerializedName("country_iso")
@Expose
private String countryIso;
@SerializedName("country_flag")
@Expose
private String countryFlag;
@SerializedName("calling_code")
@Expose
private String callingCode;
@SerializedName("fancier_count")
@Expose
private Integer fancierCount;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCountryIso() {
return countryIso;
}
public void setCountryIso(String countryIso) {
this.countryIso = countryIso;
}
public String getCountryFlag() {
return countryFlag;
}
public void setCountryFlag(String countryFlag) {
this.countryFlag = countryFlag;
}
public String getCallingCode() {
return callingCode;
}
public void setCallingCode(String callingCode) {
this.callingCode = callingCode;
}
public Integer getFancierCount() {
return fancierCount;
}
public void setFancierCount(Integer fancierCount) {
this.fancierCount = fancierCount;
}
}
答案 0 :(得分:0)
您无法从非UI线程更改UI元素。尝试使用runOnUiThread。
runOnUiThread(new Runnable(){
@Override
public void run(){
// change UI elements here
gridView.setAdapter(pmFanciersAdapter);
pmFanciersAdapter.notifyDataSetChanged();
}
});
您的适配器代码
private void getData() {
//Showing a progress dialog while our app fetches the data from url
final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show();
loading.dismiss();
try {
JSONObject jObj = new JSONObject(response);
JSONArray arr = jObj.getJSONArray("country_details");
/*JSONArray json = new JSONArray(response);*/
for (int i = 0; i < arr.length(); i++) {
//Creating a json object of the current index
JSONObject obj = null;
try {
//getting json object from current index
obj = arr.getJSONObject(i);
Id.add(String.valueOf(obj.getInt("country_code")));
count.add(obj.getString(TAG_COUNT));
images.add(obj.getString("country_flag"));
country.add(obj.getString("country_name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
//Creating GridViewAdapter Object
PMFanciersAdapter pmFanciersAdapter = new PMFanciersAdapter(getApplicationContext(), images, count, Id, country);
//Adding adapter to gridview
runOnUiThread(new Runnable(){
@Override
public void run(){
// change UI elements here
gridView.setAdapter(pmFanciersAdapter);
pmFanciersAdapter.notifyDataSetChanged();
}
});
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
答案 1 :(得分:0)
要实现此目的,您不应单独发送标识,国家/地区标志。如果您这样做,则无法确定哪个国家/地区的ID和哪个国家/地区。所以你应该创建一个Pojo
类(比如CountryDetails
)。将代码,名称,标志,ID添加为字段。
然后在Activity中创建Pojo类型的ArrayList:
ArrayList<CountryDetails> al = new ArrayList<>();
然后在for loop
的{{1}}创建对象中添加所有值。稍后将CountryDetails
添加到您的适配器。更改仅al
和PMFanciersAdapter
作为参数的Context
构造函数。
首先要更正此问题,因为即使您能够使用代码进行显示,在某些情况下,图像和国家/地区名称也会不匹配。我希望你能在解决这个问题后做进一步的工作。