我一直在這個問題上停留了兩個星期,希望有人能幫助我。提前致謝。Android GSON/JSON反序列化並顯示在適配器中
我想在一個活動中創建一個視圖,該活動由底部的標題視圖和列表視圖組成。到目前爲止,我已經成功地從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"
}
]
}
List類
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 respose),我可以成功從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;
}
}
這聽起來很複雜,只希望有人能給我一個幫助。謝謝你這麼多
您是否需要從應用程序服務器下載圖片文件? – 2012-07-11 11:42:43
@RyanGray是的,的確,我想我會使用universalimageloader ...謝謝 – 2012-07-11 11:46:20