我在我的应用程序中使用json解析,我得到以下响应,我想显示我对listiview的响应中的名称,但是我的应用程序崩溃了,任何人都可以告诉我这是什么错误
android-sdk-windows\platforms\android-23\optional\optional.json
Mainactiviyt.java
[
{
"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"
}
]
适配器
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();
}
}
答案 0 :(得分:1)
listiview没有显示
因为当前代码中存在以下问题:
1。在添加项目之前不初始化ArrayList。这样做:
private ArrayList<String> statedata=new ArrayList<String>();
2。因为问题是getView
方法:
@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;
}
答案 1 :(得分:1)
在你的getView()方法
中holder.tv.setText(listData.get(position));
缺少。您不在文本视图中设置任何文本。这就是为什么它是空的