0

我已经被这个问题困了两个星期了,我希望有人能帮助我。提前致谢。

我想在一个活动中创建一个视图,该视图由标题视图和底部的列表视图组成。到目前为止,我已经成功地将数据从 JSON 获取到 listView,因为那里的大多数教程都只是关于 JSON 和 listView。

我的问题是,我怎样才能让下面的数据显示在标题、简单视图(非 listView)甚至我想要的视图上......

JSON文件

{
 "photos" : [
  {
     "thumbnail" : "http://www.myserver.com/01.jpg",
  },
  {
     "thumbnail" : "http://www.myserver.com/02.jpg",
  },
  {
     "thumbnail" : "http://www.myserver.com/03.jpg",
  },
],
"info" : [
  {
     "id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"
   }
 ];
"owners" : [
   {
     "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"
    },
    {
     "ownerid" : "76832",
     "name" : "Kurt",
     "year_own" : "1986-1995"
    },
    {
     "ownerid" : "236471",
     "name" : "Alma",
     "year_own" : "1999-2005"
    }
   ]
}

列表类

public class car_detail {
    private List<CAR_INFO_MODEL> photos;
    private List<CAR_INFO_MODEL> info;
    private List<CAR_INFO_MODEL> owners;

public List<CAR_INFO_MODEL> getPhotos(){
    return photos;
}
public void setPhotoList(List<CAR_INFO_MODEL> photos){
    this.photos = photos;
}

    public List<CAR_INFO_MODEL> getInfos(){
    return info;
}
public void setInfoList(List<CAR_INFO_MODEL> info){
    this.info = info;
}

    public List<CAR_INFO_MODEL> getOwners(){
    return owners;
}
public void setOwnerList(List<CAR_INFO_MODEL> owners){
    this.owners = owners;
}
}

模型类

public class CAR_INFO_MODEL {
private String description;
private String year;

private String thumbnail;

private String name;
private String year_own;

public String getDesc() {
    return description;
}
public void setDesc(String description) {
    this.description = description; 
    }
    public String getYear() {
    return year;
}
public void setYear(String year) {
    this.year = year; 
    }
    public String getPhoto() {
    return thumbnail;
}
public void setPhoto(String thumbnail) {
    this.thumbnail = thumbnail; 
    }
    public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name; 
    }
    public String getYearown() {
    return year_own;
}
public void setYearown(String year_own) {
    this.year_own = year_own; 
    }
}

活动类(我使用 AsyncTask 查询并获取 JSON 响应),我可以成功地从 JSON 中获取信息对象,但不知道如何获取照片和所有者。

 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardetail_layout);

    lv = (ListView) findViewById(R.id.list_cardetail);

    carArray = new ArrayList<CAR_INFO_MODEL>();
    carDetailAdapter = new carDetailAdapter(carActivity.this, R.layout.caritem_layout, infoArray);
            lv.setAdapter(carDetailAdapter);  
            try {
                new DealSync().execute("http://www.myserver.com/carsquery/");
            } catch(Exception e) {}
 }


 String json = client.getResponse();
 list = new Gson().fromJson(json, car_detail.class); 

 return list;

 protected void onPostExecute(car_detail infolist) {

        for(CAR_INFO_MODEL lm : infolist.getInfo())
        {
            infoArray.add(lm);
        }
        carDetailAdapter.notifyDataSetChanged();
    }

适配器类(我可以将信息显示为单个项目列表,但我不知道如何从数据中获取照片和所有者(底部列表视图)并显示它。)

public class carDetailAdapter extends ArrayAdapter<CAR_INFO_MODEL> {
int resource;
String response;
Context context;
private LayoutInflater dInflater;

public carDetailAdapter(Context context, int resource, List<CAR_INFO_MODEL> objects) {
        super(context, resource, objects);
        this.resource = resource;
        dInflater = LayoutInflater.from(context);
}
static class ViewHolder {

    TextView description;

}
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    CAR_INFO_MODEL lm = (CAR_INFO_MODEL) getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        convertView = dInflater.inflate(R.layout.carDetail_layout, null);
        holder = new ViewHolder();
        holder.description = (TextView) convertView.findViewById(R.id.cardesc);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.description.setText(lm.getDesc());

    return convertView;
}

}

这可能听起来很复杂,只是希望有人能给我帮助。非常感谢

4

2 回答 2

3

我注意到你的逻辑中有一些问题

  • 首先,您使用的 JSON 字符串不是 JSON 格式的。GSON 解析器无法解析相同的字符串。

问题 :

  • thumbnail照片变量末尾有一个额外的逗号
  • info 对象以;而不是结尾,

更正的 JSON 字符串在这里:

{
 "photos" : [
  {
     "thumbnail" : "http://www.myserver.com/01.jpg"
  },
  {
     "thumbnail" : "http://www.myserver.com/02.jpg"
  },
  {
     "thumbnail" : "http://www.myserver.com/03.jpg"
  }
],
"info" : [
  {
     "id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"
   }
 ],
"owners" : [
   {
     "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"
    },
    {
     "ownerid" : "76832",
     "name" : "Kurt",
     "year_own" : "1986-1995"
    },
    {
     "ownerid" : "236471",
     "name" : "Alma",
     "year_own" : "1999-2005"
    }
   ]
}

现在我发现的逻辑错误

  • 您的CarDetailsJSON 数据包含三个对象。1. 照片列表 2. Info 对象和 3. 所有者列表
  • 但是您的实现将其作为三个CAR_INFO_MODEL列表处理,其中包含此类中的所有数据。通过这样做,您的 JSON 中存在的所有分类都已丢失。
  • 这是你的问题

要解决此问题,请将您的数据分类为 JSON 表示的数据。如下所示

Photo班级_

public class Photo {

    /**
     * Constructor for Photo.
     * @param thumbnail <tt></tt>
     */
    public Photo(String thumbnail) {
        super();
        this.thumbnail = thumbnail;
    }

    private String thumbnail;

    /**
     * Gets the thumbnail.
     * 
     * @return <tt> the thumbnail.</tt>
     */
    public String getThumbnail() {
        return thumbnail;
    }

    /**
     * Sets the thumbnail.
     *
     * @param thumbnail <tt> the thumbnail to set.</tt>
     */
    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Photo [thumbnail=" + thumbnail + "]";
    }



}

Info班级_

public class Info {
    /*"id" : "1",
     "description" : "My White Toyota",
     "country" : "Japan",
     "year" : "1982"

     * */

    private String id;
    /**
     * Constructor for Info.
     * @param id
     * @param description
     * @param country
     * @param year <tt></tt>
     */
    public Info(String id, String description, String country, String year) {
        super();
        this.id = id;
        this.description = description;
        this.country = country;
        this.year = year;
    }
    private String description;
    private String country;
    private String year;
    /**
     * Gets the id.
     * 
     * @return <tt> the id.</tt>
     */
    public String getId() {
        return id;
    }
    /**
     * Sets the id.
     *
     * @param id <tt> the id to set.</tt>
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * Gets the description.
     * 
     * @return <tt> the description.</tt>
     */
    public String getDescription() {
        return description;
    }
    /**
     * Sets the description.
     *
     * @param description <tt> the description to set.</tt>
     */
    public void setDescription(String description) {
        this.description = description;
    }
    /**
     * Gets the country.
     * 
     * @return <tt> the country.</tt>
     */
    public String getCountry() {
        return country;
    }
    /**
     * Sets the country.
     *
     * @param country <tt> the country to set.</tt>
     */
    public void setCountry(String country) {
        this.country = country;
    }
    /**
     * Gets the year.
     * 
     * @return <tt> the year.</tt>
     */
    public String getYear() {
        return year;
    }
    /**
     * Sets the year.
     *
     * @param year <tt> the year to set.</tt>
     */
    public void setYear(String year) {
        this.year = year;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Info [id=" + id + ", description=" + description + ", country="
                + country + ", year=" + year + "]";
    }



}

“所有者”类

public class Owner {
    /*
     * "ownerid" : "5633432",
     "name" : "Jackson",
     "year_own" : "1982-1985"

     * */

    private String ownerid;
    /**
     * Constructor for Owner.
     * @param ownerid
     * @param name
     * @param year_own <tt></tt>
     */
    public Owner(String ownerid, String name, String year_own) {
        super();
        this.ownerid = ownerid;
        this.name = name;
        this.year_own = year_own;
    }
    private String name;
    private String year_own;
    /**
     * Gets the ownerid.
     * 
     * @return <tt> the ownerid.</tt>
     */
    public String getOwnerid() {
        return ownerid;
    }
    /**
     * Sets the ownerid.
     *
     * @param ownerid <tt> the ownerid to set.</tt>
     */
    public void setOwnerid(String ownerid) {
        this.ownerid = ownerid;
    }
    /**
     * Gets the name.
     * 
     * @return <tt> the name.</tt>
     */
    public String getName() {
        return name;
    }
    /**
     * Sets the name.
     *
     * @param name <tt> the name to set.</tt>
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * Gets the year_own.
     * 
     * @return <tt> the year_own.</tt>
     */
    public String getYear_own() {
        return year_own;
    }
    /**
     * Sets the year_own.
     *
     * @param year_own <tt> the year_own to set.</tt>
     */
    public void setYear_own(String year_own) {
        this.year_own = year_own;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Owner [ownerid=" + ownerid + ", name=" + name + ", year_own="
                + year_own + "]";
    }


}

最后是CarDetail班级

public class CarDetail {

    private List<Photo> photos;
    private List<Info> info;
    private List<Owner> owners;
    /**
     * Gets the photos.
     * 
     * @return <tt> the photos.</tt>
     */
    public List<Photo> getPhotos() {
        return photos;
    }
    /**
     * Sets the photos.
     *
     * @param photos <tt> the photos to set.</tt>
     */
    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
    }
    /**
     * Gets the info.
     * 
     * @return <tt> the info.</tt>
     */
    public List<Info> getInfo() {
        return info;
    }
    /**
     * Sets the info.
     *
     * @param info <tt> the info to set.</tt>
     */
    public void setInfo(List<Info> info) {
        this.info = info;
    }
    /**
     * Gets the owners.
     * 
     * @return <tt> the owners.</tt>
     */
    public List<Owner> getOwners() {
        return owners;
    }
    /**
     * Sets the owners.
     *
     * @param owners <tt> the owners to set.</tt>
     */
    public void setOwners(List<Owner> owners) {
        this.owners = owners;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "CarDetail [photos=" + photos + ", info=" + info + ", owners="
                + owners + "]";
    }



}

并使用以下这些类来解析和获取存储在每个对象中的详细信息(即照片、所有者和信息)

String json = "{" + 
" \"photos\" : [" + 
"  {" + 
"     \"thumbnail\" : \"http://www.myserver.com/01.jpg\"" + 
"  }," + 
"  {" + 
"     \"thumbnail\" : \"http://www.myserver.com/02.jpg\"" + 
"  }," + 
"  {" + 
"     \"thumbnail\" : \"http://www.myserver.com/03.jpg\"" + 
"  }" + 
"]," + 
"\"info\" : [" + 
"  {" + 
"     \"id\" : \"1\"," + 
"     \"description\" : \"My White Toyota\"," + 
"     \"country\" : \"Japan\"," + 
"     \"year\" : \"1982\"" + 
"   }" + 
" ]," + 
"\"owners\" : [" + 
"   {" + 
"     \"ownerid\" : \"5633432\"," + 
"     \"name\" : \"Jackson\"," + 
"     \"year_own\" : \"1982-1985\"" + 
"    }," + 
"    {" + 
"     \"ownerid\" : \"76832\"," + 
"     \"name\" : \"Kurt\"," + 
"     \"year_own\" : \"1986-1995\"" + 
"    }," + 
"    {" + 
"     \"ownerid\" : \"236471\"," + 
"     \"name\" : \"Alma\"," + 
"     \"year_own\" : \"1999-2005\"" + 
"    }" + 
"   ]" + 
"}";
// parsing the JSON string to CardDetail object
 CarDetail detail=new Gson().fromJson(json,CarDetail.class);

 // retrieving data from the CardDetail object
 List<Photo> photos = detail.getPhotos();
 System.out.println("retrieving Photos");
 for (Photo photo : photos) {
    System.out.println(photo);
}

 List<Info> info = detail.getInfo();
 System.out.println("retrieving Info");
 for (Info info2 : info) {
    System.out.println(info2);
}
 System.out.println("retrieving Owners");
 List<Owner> owners = detail.getOwners();
 for (Owner owner : owners) {
    System.out.println(owner);
}

现在在您的视图中使用检索到的数据。如果您需要任何帮助/帮助,请告诉我

于 2012-07-11T12:54:58.863 回答
0

使用http://developer.android.com/reference/org/json/package-summary.html

或者google-gson,如果你真的想序列化/反序列化 json。

于 2012-07-11T11:42:05.660 回答