我有一个应用程序,其中我从json检索数据并在列表视图中显示它,我得到一个问题只有单个数据在列表中可见,我试图找出问题,但什么都没得到,我要去哪里错误请帮我解决。在日志中我得到不同的数据,但是将它添加到数组列表中多次获得单个数据重复,并且没有其他项目可见。
BussinessActivity
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bussiness_details);
Log.i("DetailsUrl.Bussinessurl", DetailsUrl);
new JSONAsyncTask().execute(DetailsUrl);
weblink=(TextView)findViewById(R.id.txtweb);
list1 = (ListView) findViewById(R.id.list1);
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(getApplication(),
//catagery.get(position).getcategory_name(),
//Toast.LENGTH_LONG).show();
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(BussinessDetails.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
catageries = new ArrayList<ProfileDetails>();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
// ------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONObject jarray = jsono.getJSONObject("business");
ProfileDetails category = new ProfileDetails();
//String business_id = jarray.getString("business_id");
//Log.i("business_id", business_id);
//String business_name = jarray.getString("business_name");
//Log.i("business_name", business_name);
String web_site = jarray.getString("web_site");
category.setData1(web_site);
category.setNumber2(web_site);
//weblink.setText(web_site);
Log.i("web_site", web_site);
//String about_business = jarray.getString("about_business");
//Log.i("web_site", web_site);
JSONObject phn = jarray.getJSONObject("Phones");
List<String> listArray = new ArrayList<String>();
Iterator iter = phn.keys();
int count=0;
while(iter.hasNext())
{
String key = (String)iter.next();
// listArray.add((String)iter.next());
Log.i("Name",key);
count +=1;
// String key = (String)iter.next();
// Object o = phn.get(key);
if( phn.get(key) instanceof JSONArray ){
JSONArray arry = phn.getJSONArray(key);
int size = arry.length();
for (int i = 0; i < size; i++) {
String CC_info_left = arry.getJSONObject(i).getString("CC_info_left");
Log.i("CC_info_left", CC_info_left);
String CC_info_right = arry.getJSONObject(i).getString("CC_info_right");
Log.i("CC_info_right", CC_info_right);
String CC_info_short_desc = arry.getJSONObject(i).getString("CC_info_short_desc");
Log.i("CC_info_short_desc", CC_info_short_desc);
String CC_info_short_desc_align = arry.getJSONObject(i).getString("CC_info_short_desc_align");
Log.i("CC_info_short_desc_align", CC_info_short_desc_align);
category.setName(key);
category.setNumber1(CC_info_right);
category.setData(CC_info_left);
category.setDescription(CC_info_short_desc);
// Log.i("category", category.toString());
catageries.add(category);
}
}
}
//Collections.sort(listArray);
// System.out.println(listArray);
JSONObject EmailAddress = jarray.getJSONObject("Emails");
Iterator iter1 = EmailAddress.keys();
int count1=0;
while(iter1.hasNext()){
String key1 = (String)iter1.next();
Log.i("Name1",key1);
count +=1;
// String key = (String)iter.next();
// Object o = phn.get(key);
if( EmailAddress.get(key1) instanceof JSONArray ){
JSONArray arry1 = EmailAddress.getJSONArray(key1);
int size1 = arry1.length();
for (int i = 0; i < size1; i++) {
String CC_info_left1 =arry1.getJSONObject(i).getString("CC_info_left");
String CC_info_right1 = arry1.getJSONObject(i).getString("CC_info_right");
String CC_info_short_desc1 = arry1.getJSONObject(i).getString("CC_info_short_desc");
String CC_info_short_desc_align1 =arry1.getJSONObject(i).getString("CC_info_short_desc_align");
category.setEmail(key1);
category.setEmailleft(CC_info_left1);
category.setEmailright(CC_info_right1);
category.setEmailDesc(CC_info_short_desc1);
catageries.add(category);
}
}
}
// /}
// }
return true;
}
// ------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
//adapter.notifyDataSetChanged();
if (result == false){
Toast.makeText(BussinessDetails.this,
"Unable to fetch data from server", Toast.LENGTH_LONG)
.show();
}else{
adapter = new DetailAdaptor(BussinessDetails.this, R.layout.list_details, catageries);
list1.setAdapter(adapter);
//emailAdaptor=new EmailAdaptor(BussinessDetails.this, R.layout.list_details, catagery);
//list2.setAdapter(emailAdaptor);
}
}
}
DetailAdaptor
public class DetailAdaptor extends ArrayAdapter<ProfileDetails> {
ArrayList<ProfileDetails> detailList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public DetailAdaptor(Context context, int resource, ArrayList<ProfileDetails> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
detailList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.title_from_phone);
holder.tvData2 = (TextView) v.findViewById(R.id.data_from_phone1);
holder.tvData1 = (TextView) v.findViewById(R.id.data_from_phone2);
holder.tvDescription = (TextView) v.findViewById(R.id.data_from_phone3);
holder.email = (TextView) v.findViewById(R.id.title_for_email);
holder.email_left = (TextView) v.findViewById(R.id.email_left);
holder.email_right = (TextView) v.findViewById(R.id.email_right);
holder.emailDescription = (TextView) v.findViewById(R.id.email_desc);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
holder.tvName.setText(detailList.get(position).getName());
holder.tvDescription.setText(detailList.get(position).getDescription());
holder.tvData1.setText(detailList.get(position).getNumber1());
holder.tvData2.setText(detailList.get(position).getData());
holder.email.setText(detailList.get(position).getEmail());
holder.email_left.setText(detailList.get(position).getEmailleft());
holder.email_right.setText(detailList.get(position).getEmailright());
holder.emailDescription.setText(detailList.get(position).getEmailDesc());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvData1;
public TextView tvData2;
public TextView email;
public TextView emailDescription;
public TextView email_left;
public TextView email_right;
}
}
ProfileDetails
public class ProfileDetails {
private String title;
private String email;
private String email_left;
private String email_right;
private String email_desc;
private String number1;
private String number2;
private String description;
private String detail1;
private String detail2;
//private String children;
//private String image;
public ProfileDetails() {
// TODO Auto-generated constructor stub
}
public ProfileDetails(String name,String num, String num2, String desc,
String data1, String data2,String emails,String emailleft ,String emailright,String emaildesc) {
super();
title = name;
description = desc;
number1 = num;
number2 = num2;
detail1 = data1;
detail2 = data2;
email =emails;
email_left=emailleft;
email_right=emailright;
email_desc=email_desc;
//this.children = children;
//this.image = image;
}
public String getName() {
return title;
}
public void setName(String name) {
title = name;
}
public String getEmail() {
return email;
}
public void setEmail(String ename) {
email = ename;
}
public String getEmailleft() {
return email_left;
}
public void setEmailleft(String Eleft) {
email_left = Eleft;
}
public String getEmailright() {
return email_right;
}
public void setEmailright(String Eright) {
email_right = Eright;
}
public String getEmailDesc() {
return email_desc;
}
public void setEmailDesc(String Edesc) {
email_desc = Edesc;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getData() {
return detail1;
}
public void setData(String data) {
detail1 = data;
}
public String getData1() {
return detail2;
}
public void setData1(String data) {
detail2 = data;
}
public String getNumber1() {
return number1;
}
public void setNumber1(String num) {
number1 = num;
}
public String getNumber2() {
return number2;
}
public void setNumber2(String num) {
number2 = num;
}
/*public String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
*/
}
BussinessDetail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<View
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="@+id/rel1"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Start a New ChaBu"
android:textColor="#030303"
android:textSize="12dp" />
<ImageView
android:id="@+id/chatIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@drawable/new_chabu_icon" />
</RelativeLayout>
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/rel1"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/line"
android:layout_marginTop="5dp"
android:scrollbars="none"
android:textColor="@android:color/black"
tools:listitem="@layout/list_details" >
</ListView>
</RelativeLayout>
List_details
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rel2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/line"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Customer Care Numbers"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/line4"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/rel2"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/txtlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/line4"
android:background="#E6E6E6" >
<TextView
android:id="@+id/title_from_phone"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text=""
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="@+id/rel1"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="@+id/txtlay"
android:background="#E6E6E6" >
<TextView
android:id="@+id/data_from_phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="@+id/data_from_phone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="@+id/data_from_phone3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/data_from_phone1"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="" />
</RelativeLayout>
<View
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="@+id/whitelay"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/whitelay"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@+id/rel1"
android:background="@android:color/white"
android:orientation="vertical" >
</LinearLayout>
<View
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/line1"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/rel3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/whitelay"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="@+id/title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Customer Care Emails"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/line5"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/rel3"
android:layout_marginTop="10dp"
android:visibility="gone"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/txtlay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_below="@+id/line5"
android:background="#E6E6E6" >
<TextView
android:id="@+id/title_for_email"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text=""
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/line6"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/txtlay1"
android:visibility="gone"
android:layout_marginTop="0dp"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/rel4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:visibility="gone"
android:layout_below="@+id/line6"
android:background="#E6E6E6" >
<TextView
android:id="@+id/email_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="@+id/email_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="@+id/email_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/email_left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rel5"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/rel4"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="@+id/title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="About Us"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="@+id/line9"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/title5"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
</RelativeLayout>
<View
android:id="@+id/line11"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/rel5"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<RelativeLayout
android:id="@+id/rel6"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/line10"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="@+id/about_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Website"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/line10"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/rel6"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/rel5"
android:layout_marginLeft="10dp"
android:layout_marginTop="18dp"
android:visibility="gone"
android:text="ffdhjggjhkkghjg"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="@+id/line10"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/txt"
android:layout_marginTop="10dp"
android:visibility="gone"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/txtweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/rel6"
android:layout_marginLeft="10dp"
android:layout_marginTop="18dp"
android:text="ffdhjggjhkkghjg"
android:visibility="gone"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="@+id/line12"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/txtweb"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray" />
</RelativeLayout>
答案 0 :(得分:2)
您在预执行中创建一次类别对象,然后您不创建新对象,以便每次在循环中创建类别对象时在数组列表中设置类别最后一个值并尝试它正在运行