[英]Not getting json output in required layout?
I am trying to fetch country flags into my GridView
, but it's not showing anything and I am not getting any error in logcat either. 我正在尝试将国家标志获取到我的
GridView
,但是它没有显示任何内容,并且在logcat中也没有收到任何错误。 when I try debugging its showing response but not showing that in GridView
. 当我尝试调试其显示响应但未在
GridView
显示响应时。
here is my code: 这是我的代码:
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 output: 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
},
here is my gridview adapter.. 这是我的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 class: 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;
}
}
You cannot change UI elements from a non-UI thread. 您不能从非UI线程更改UI元素。 Try using runOnUiThread.
尝试使用runOnUiThread。
runOnUiThread(new Runnable(){
@Override
public void run(){
// change UI elements here
gridView.setAdapter(pmFanciersAdapter);
pmFanciersAdapter.notifyDataSetChanged();
}
});
you adapter code 您的适配器代码
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);
}
}
To achieve this you should not send Id, country, flags separately. 为此,您不应单独发送ID,国家/地区标志。 If you do so, You can't map which id is for which country and for which flag.
如果这样做,则无法映射哪个ID代表哪个国家和哪个国旗。 So you should make one
Pojo
class (say CountryDetails
). 因此,您应该
Pojo
一门Pojo
课程(例如CountryDetails
)。 Add code, name, flag, id as fields. 添加代码,名称,标志,ID作为字段。
Then Create ArrayList of type Pojo in Activity: 然后在Activity中创建Pojo类型的ArrayList:
ArrayList<CountryDetails> al = new ArrayList<>();
Then in your for loop
create object of CountryDetails
and add all values to it. 然后在
for loop
创建CountryDetails
对象,并将所有值添加到该对象。 Later add al
to your Adapter. 以后将
al
添加到您的适配器。 Change constructor of PMFanciersAdapter
having only Context
and ArrayList<CountryDetails>
as parameters. 更改仅以
Context
和ArrayList<CountryDetails>
作为参数的PMFanciersAdapter
构造函数。
First correct this because even if you are able to display with your code, in some cases images and country names will mismatch. 首先,请更正此问题,因为即使您能够显示代码,在某些情况下图像和国家/地区名称也会不匹配。 I hope you can do further work after fixing this.
希望您在解决此问题后可以做进一步的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.