如何显示listiview不同的JSON字符串? [英] how to display different json string in listiview?
本文介绍了如何显示listiview不同的JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用JSON解析在我的应用程序,我得到如下回应,我想从我的回应listiview显示的名字,但我的应用程序崩溃了,任何一个可以告诉我什么是错误
[
{
friend_id:1,
user_spoc_dob:1993年11月11日,
状态:真正的,
spouse_name:百合,
OCC:SPOC
},
{
friend_id:1,
user_mother_dob:1974年12月12日,
状态:真正的,
mother_name:SITA,
消息:详细地址不可用,
OCC:妈妈
},
{
friend_id:1,
user_father_dob:1972年11月19日,
状态:真正的,
father_name:公羊,
消息:详细地址不可用,
OCC:爸爸
},
{
friend_id:1,
user_dob:1994年2月20日,
状态:真正的,
user_fname:苏,
OCC:杀破狼
}
]
Mainactiviyt.java
类LoadAllStates扩展
AsyncTask的<字符串,字符串的ArrayList<串GT;> {
私人ProgressDialog pDialog;
私人字符串试验;
私人的ArrayList<串GT; testlist;
私人字符串光正交码; 私人字符串ID; @覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(TestFamilyocaasion.this);
pDialog.setMessage(请稍候...);
pDialog.setIndeterminate(真);
pDialog.setCancelable(真);
pDialog.show();
} 保护的ArrayList<串GT; doInBackground(
字符串参数... args){
SH的ServiceHandler =新的ServiceHandler();
//制作到URL的请求和响应得到
statedata =新的ArrayList<串GT;();
testlist =新的ArrayList<串GT;();
字符串jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL,ServiceHandler.GET); Log.d(回应:,>中+ jsonStr); Log.d(回应:,>中+ jsonStr);
如果(jsonStr!= NULL){
尝试{
jsonObj =新JSONArray(jsonStr);
的for(int i = 0; I< jsonObj.length();我++){
JSONObject的C = jsonObj.getJSONObject(I)
串wifebday =(c.has(user_spoc_dob))? c.getString(user_spoc_dob):空;
wifename =(c.has(spouse_name))? c.getString(spouse_name):空;
串mothersbday =(c.has(user_mother_dob))? c.getString(user_mother_dob):空;
mothersname =(c.has(mother_name))? c.getString(mother_name):空;
串fatherbday =(c.has(user_father_dob))? c.getString(user_father_dob):空;
fathername =(c.has(father_name))? c.getString(father_name):空;
串ownbbday =(c.has(user_dob))? c.getString(user_dob):空;
ownname =(c.has(user_fname))? c.getString(user_fname):空;
OCCS =(c.has(OCC))? c.getString(OCC):空;
IDS =(c.has(friend_id))? c.getString(friend_id):空;
的System.out.println(BDAY+ wifebday + mothersbday + fatherbday + ownbbday);
// if测试,以避免增加空值
如果(wifename!= NULL)statedata.add(wifename);
如果(mothersbday!= NULL)statedata.add(mothersname);
如果(fatherbday!= NULL)statedata.add(fathername);
如果(ownbbday!= NULL)statedata.add(ownname);
如果(!OCCS = NULL)testlist.add(OCCS);
} }赶上(JSONException E){
e.printStackTrace();
}
}其他{
Log.e(的ServiceHandler,无法从URL得到任何数据);
}
返回statedata;
}
保护无效onPostExecute(ArrayList的<串GT;的结果){
super.onPostExecute(结果); 如果(pDialog.isShowing())
pDialog.dismiss(); ALIST =新的ArrayList<串GT;();
aList.addAll(结果);
适配器=新CustomAdapterGiftsharealert(TestFamilyocaasion.this,结果);
listviw.setAdapter(适配器);
adapter.notifyDataSetChanged(); }}
适配器
私有类CustomAdapterGiftsharealert延伸BaseAdapter {
//的String []的结果;
上下文语境;
// INT [] imageId;
私人的ArrayList<串GT;的ListData;
私人LayoutInflater吹气= NULL;
公共CustomAdapterGiftsharealert(上下文mainActivity,ArrayList的<串GT;的ListData){
// TODO自动生成构造函数存根
上下文= mainActivity;
this.listData =的ListData; 充气=(LayoutInflater)上下文。
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
返回listData.size();
} @覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回的位置;
} @覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
} 公共类持有人
{
TextView的电视;
ImageView的IMG;
TextView的消息;
TextView的点;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
// TODO自动生成方法存根
最终持有人持有人=新的持有人();
查看rowView;
rowView = inflater.inflate(R.layout.list_item_frndsfmly,NULL);
holder.tv =(TextView中)rowView.findViewById(R.id.frndsfamly_name); 返回rowView;
}}
解决方案
listiview什么都不显示
块引用>由于在目前的code以下问题:
1 在里面添加条目之前不进行初始化的ArrayList。做到这一点的:
私人的ArrayList<串GT; statedata =新的ArrayList<串GT;();
2 因为问题是
getView
方法:@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
// TODO自动生成方法存根
...
holder.tv =(TextView中)rowView.findViewById(R.id.frndsfamly_name);
//从的ListData设置文本的TextView
holder.tv.setText(listData.get(位置));
返回rowView;
}I am using json parsing in my app,i am getting following response,and i want to display name from my response to listiview,but my app got crash,can any one tell me what is the mistake
[ { "friend_id": "1", "user_spoc_dob": "1993-11-11", "status": "true", "spouse_name": "lily", "occ": "spoc" }, { "friend_id": "1", "user_mother_dob": "1974-12-12", "status": "true", "mother_name": "sita", "message": "Address details Not available", "occ": "mom" }, { "friend_id": "1", "user_father_dob": "1972-11-19", "status": "true", "father_name": "ram", "message": "Address details Not available", "occ": "dad" }, { "friend_id": "1", "user_dob": "1994-02-20", "status": "true", "user_fname": "Su", "occ": "spl" } ]
Mainactiviyt.java
class LoadAllStates extends AsyncTask<String, String, ArrayList<String>> { private ProgressDialog pDialog; private String test; private ArrayList<String> testlist; private String oocs; private String ids; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(TestFamilyocaasion.this); pDialog.setMessage("Please wait.."); pDialog.setIndeterminate(true); pDialog.setCancelable(true); pDialog.show(); } protected ArrayList<String> doInBackground( String... args) { ServiceHandler sh = new ServiceHandler(); // Making a request to url and getting response statedata = new ArrayList<String>(); testlist=new ArrayList<String>(); String jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); Log.d("Response: ", "> " + jsonStr); if (jsonStr != null) { try { jsonObj = new JSONArray(jsonStr); for (int i = 0; i < jsonObj.length(); i++) { JSONObject c = jsonObj.getJSONObject(i); String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null; wifename= (c.has("spouse_name")) ? c.getString("spouse_name") : null; String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null; mothersname= (c.has("mother_name")) ? c.getString("mother_name") : null; String fatherbday= (c.has("user_father_dob")) ? c.getString("user_father_dob") : null; fathername= (c.has("father_name")) ? c.getString("father_name") : null; String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null; ownname= (c.has("user_fname")) ? c.getString("user_fname") : null; occs= (c.has("occ")) ? c.getString("occ") : null; ids= (c.has("friend_id")) ? c.getString("friend_id") : null; System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday); // if test, to avoid adding null values if(wifename!=null) statedata.add(wifename); if(mothersbday!=null) statedata.add(mothersname); if(fatherbday!=null) statedata.add(fathername); if(ownbbday!=null) statedata.add(ownname); if(occs!=null) testlist.add(occs); } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Couldn't get any data from the url"); } return statedata; } protected void onPostExecute(ArrayList<String> result) { super.onPostExecute(result); if (pDialog.isShowing()) pDialog.dismiss(); aList = new ArrayList<String>(); aList.addAll(result); adapter = new CustomAdapterGiftsharealert(TestFamilyocaasion.this, result); listviw.setAdapter(adapter); adapter.notifyDataSetChanged(); } }
Adapter
private class CustomAdapterGiftsharealert extends BaseAdapter { // String [] result; Context context; // int [] imageId; private ArrayList<String> listData; private LayoutInflater inflater=null; public CustomAdapterGiftsharealert(Context mainActivity, ArrayList<String> listData) { // TODO Auto-generated constructor stub context=mainActivity; this.listData=listData; inflater = ( LayoutInflater )context. getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return listData.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public class Holder { TextView tv; ImageView img; TextView message; TextView points; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final Holder holder=new Holder(); View rowView; rowView = inflater.inflate(R.layout.list_item_frndsfmly, null); holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name); return rowView; } }
解决方案listiview nothing is displaying
Because have following issues in current code:
1. Not initializing ArrayList before adding items in it. do it as:
private ArrayList<String> statedata=new ArrayList<String>();
2. Because problem is
getView
method :@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ... holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name); //set text to TextView from listData holder.tv.setText(listData.get(position)); return rowView; }
这篇关于如何显示listiview不同的JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文