ListView自定义适配器不为Loop添加()

时间:2015-05-13 07:33:01

标签: android json list loops view

Good Day伙计们,我有像这样的json数组

{"id":"1114","user_id":"60","paq_type":"Electronics","description":"Karen Brso","title":"xvxcv","notes":"sdf","price":"410.00","size_x":"234","size_y":"234","size_z":"23","weight":"23","start_date":"2015-04-14","end_date":"2015-04-22","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"7 Paronian d\/st","from_apt":"","from_longitute":"44.50389862060547","from_latitude":"40.17966024053021","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"Antarain Street","to_apt":"","to_longitude":"44.511966705322266","to_latitude":"40.19395447720183","creator_first_name":"Miqo","creator_last_name":"Dallaqyan","creator_phone":"12345677","creator_email":"c","creator_file":"storage\/user\/60\/Penguins-2015-04-11-19-40-15.jpg","status":"1","candidates":null,"files":[{"file_path":"storage\/paq\/1114\/1428681680Koala.jpg","file_id":"235"}]},{"id":"1115","user_id":"60","paq_type":"Books","description":"kyk","title":"yuky","notes":"ykyk","price":"757.00","size_x":"6464","size_y":"646","size_z":"64","weight":"46","start_date":"2015-04-11","end_date":"2015-04-18","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"Yeghvard Highway","from_apt":"","from_longitute":"44.50334072113037","from_latitude":"40.22662974852171","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"9th Street","to_apt":"","to_longitude":"44.49767589569092","to_latitude":"40.2177168941058","creator_first_name":"Miqo","creator_last_name":"Dallaqyan","creator_phone":"12345677","creator_email":"c","creator_file":"storage\/user\/60\/Penguins-2015-04-11-19-40-15.jpg","status":"1","candidates":null,"files":null},{"id":"1116","user_id":"60","paq_type":"Books","description":"sdfgs","title":"ergdrfg","notes":"dfgdsfg","price":"330.00","size_x":"4532","size_y":"5344","size_z":"534","weight":"3","start_date":"2015-04-10","end_date":"2015-04-12","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"1st line of the Aygedzor Street","from_apt":"","from_longitute":"44.50192451477051","from_latitude":"40.189758316404536","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"7 Khanjyan Street","to_apt":"","to_longitude":"44.51617240905762","to_latitude":"40.17421714768112","creator_first_name":"Miqo","creator_last_name":"Dallaqyan","creator_phone":"12345677","creator_email":"c","creator_file":"storage\/user\/60\/Penguins-2015-04-11-19-40-15.jpg","status":"1","candidates":null,"files":null},{"id":"1117","user_id":"60","paq_type":"Books","description":"sdfgs","title":"ergdrfg","notes":"dfgdsfg","price":"330.00","size_x":"4532","size_y":"5344","size_z":"534","weight":"3","start_date":"2015-04-11","end_date":"2015-04-13","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"1st line of the Aygedzor Street","from_apt":"","from_longitute":"44.50192451477051","from_latitude":"40.189758316404536","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"7 Khanjyan Street","to_apt":"","to_longitude":"44.51617240905762","to_latitude":"40.17421714768112","creator_first_name":"Miqo","creator_last_name":"Dallaqyan","creator_phone":"12345677","creator_email":"c","creator_file":"storage\/user\/60\/Penguins-2015-04-11-19-40-15.jpg","status":"1","candidates":null,"files":null},{"id":"1118","user_id":"60","paq_type":"Books","description":"ghdf","title":"dghdf","notes":"ghdfgh","price":"150.00","size_x":"4532","size_y":"123","size_z":"1423","weight":"231","start_date":"2015-04-11","end_date":"2015-04-22","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"Vahagn Davtyan Street","from_apt":"","from_longitute":"44.51119422912598","from_latitude":"40.20096935364624","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"Simeon Yerevantsi Street","to_apt":"","to_longitude":"44.50037956237793","to_latitude":"40.18202096431908","creator_first_name":"Miqo","creator_last_name":"Dallaqyan","creator_phone":"12345677
05-13 11:27:12.744  14750-14898/com.paqqy D/anasun﹕ [{"id":"1194","user_id":"61","paq_type":"Electronics","description":"werdszfsdf","title":"werwer","notes":"werwer","price":"420.00","size_x":"2","size_y":"23","size_z":"3","weight":"3","start_date":"2015-04-23","end_date":"2015-04-30","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"Michurin Street","from_apt":"","from_longitute":"44.489994049072266","from_latitude":"40.14893055361652","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"12 9 of May Street","to_apt":"","to_longitude":"44.503211975097656","to_latitude":"40.15916439538877","creator_first_name":"Hovsep","creator_last_name":"Markosyan","creator_phone":"077310128","creator_email":"markosyanhoso@mail.ru","creator_file":"storage\/user\/61\/Lighthouse-2015-04-12-00-57-14.jpg","status":"1","candidates":null,"files":[{"file_path":"storage\/paq\/1194\/1428700691Tulips.jpg","file_id":"242"}]},{"id":"1203","user_id":"61","paq_type":"Books","description":"sdfsfasdasdh","title":"iPhone","notes":"ik","price":"200.00","size_x":"44","size_y":"22","size_z":"88","weight":"99","start_date":"2015-04-25","end_date":"2015-04-27","from_country":"Armenia","from_state":"Yerevan","from_city":"Yerevan","from_street":"8 Garegin Nzhdeh Street","from_apt":"","from_longitute":"44.486622512340546","from_latitude":"40.15115498733384","to_country":"Armenia","to_state":"Yerevan","to_city":"Yerevan","to_street":"6 Garegin Nzhdeh Street","to_apt":"","to_longitude":"44.48618799448013","to_latitude":"40.15090897059796","creator_first_name":"Hovsep","creator_last_name":"Markosyan","creator_phone":"077310128","creator_email":"markosyanhoso@mail.ru","creator_file":"storage\/user\/61\/Lighthouse-2015-04-12-00-57-14.jpg","status":"1","candidates":null,"files":[{"file_path":"storage\/paq\/1203\/142894802212182_small_550 - Copy (2).jpg","file_id":"275"}]}]

我正在解析这个json并让它像这样循环。

public void addpaqs() {
    try {
        JSONArray array = new JSONArray(paqsresponse);

        for (int i = 0; i < array.length(); i++) {
            JSONObject row = array.getJSONObject(i);
            tolongitude = row.getString("to_longitude");
            tolatitude = row.getString("to_latitude");
            creatorfirstname = row.getString("creator_first_name");
            creatorlastname = row.getString("creator_last_name");
            paqtype = row.getString("paq_type");
            startdate = row.getString("start_date");
            enddate = row.getString("end_date");
            fromplace = row.getString("from_country");
            toplace = row.getString("to_country");
            fromcity = row.getString("from_city");
            tocity = row.getString("to_city");
            String price = row.getString("price");
            fromlatitude = row.getString("from_latitude");
            fromlongitude = row.getString("from_longitute");
            tolatitude = row.getString("to_latitude");
            tolongitude = row.getString("to_longitude");
            Log.d("fromplace", fromplace);
            double tolongdouble = Double.parseDouble(tolongitude);
            double tolatdouble = Double.parseDouble(tolatitude);
            Paqs paqs = new Paqs(R.drawable.cross,R.drawable.accountimage, creatorfirstname+creatorlastname,price, paqtype,startdate+"/"+enddate,fromplace+fromcity,toplace+tocity);
            paqsarray.add(paqs);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

我创建了自定义适配器,以便能够将此JSON中的所有信息放入ListView,这是我的自定义适配器类;

package com.paqqy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends BaseAdapter {

    Context context;

    protected List<Paqs> listCars;
    LayoutInflater inflater;

    public CustomAdapter(Context context, List<Paqs> listCars) {
        this.listCars = listCars;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public int getCount() {
        return listCars.size();
    }

    public Paqs getItem(int position) {
        return listCars.get(position);
    }

    public long getItemId(int position) {
        return listCars.get(position).getDrawableId();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.card_item,
                    parent, false);

            holder.name = (TextView) convertView
                    .findViewById(R.id.tv_name_id);
            holder.paqtype = (TextView) convertView
                    .findViewById(R.id.tv_paq_id);
            holder.txtPrice = (TextView) convertView
                    .findViewById(R.id.tv_price_id);
            holder.date= (TextView) convertView
                    .findViewById(R.id.tv_date_id);
            holder.from= (TextView) convertView
                    .findViewById(R.id.tv_from_id);
            holder.to= (TextView) convertView
                    .findViewById(R.id.tv_to_id);
            holder.imgCar = (ImageView) convertView.findViewById(R.id.iv_general_id);
            holder.paqsimage = (ImageView) convertView.findViewById(R.id.iv_paq_general_id);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Paqs paqs = listCars.get(position);
        holder.name.setText(paqs.getFirstname());
        holder.paqtype.setText(paqs.getPaqtype());
        holder.date.setText(paqs.getDate());
        holder.from.setText(paqs.getFrom());
        holder.to.setText(paqs.getTo());
        holder.txtPrice.setText(" $" + paqs.getPrice());
        holder.imgCar.setImageResource(paqs.getDrawableId());
        holder.paqsimage.setImageResource(paqs.getPaqsimage());
        return convertView;
    }

    private class ViewHolder {
        TextView name;
        TextView paqtype;
        TextView date;
        TextView from;
        TextView to;
        TextView txtPrice;
        ImageView imgCar;
        ImageView paqsimage;
    }

}

这是我的适配器获取列表的Paqs.class文件

    package com.paqqy;
    public class Paqs {

    private int drawableId;
    private String firstname;
    private String lastname;
    private String price;
    private String paqtype;
    private String date;
    private String from,to;
    private int paqsimage;

    public Paqs(int drawableId,int paqsimage, String firstname, String price,String paqtype,String date,String from,String to) {
        super();
        this.drawableId = drawableId;
        this.paqsimage = paqsimage;
        this.firstname = firstname;
        this.price = price;
        this.paqtype = paqtype;
        this.date = date;
        this.from = from;
        this.to = to;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public void setFrom(String from){
        this.from = from;
    }
    public void setTo(String to){
        this.to = to;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public void setPaqtype(String paqtype) {
        this.paqtype = paqtype;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getLastname() {
        return lastname;
    }
public  String getFrom(){
    return from;
}
    public String getTo(){
        return to;
    }
    public String getPrice() {
        return price;
    }
    public String getPaqtype() {
        return paqtype;
    }
    public String getDate(){
        return date;
    }

    public int getDrawableId() {
        return drawableId;
    }

    public void setDrawableId(int drawableId) {
        this.drawableId = drawableId;
    }
    public int getPaqsimage() {
        return paqsimage;
    }

    public void setPaqsimage(int paqsimage) {
        this.paqsimage = paqsimage;
    }

}

问题是循环之后我只在ListView内看到2个值,而且仅此而已......但是在我的JSON我有大量的值,你可以看到,所以我做错了什么在我的循环?我不知道也不知道问题是什么。谢谢你!

1 个答案:

答案 0 :(得分:0)

所以,有一件事情可能发生,对不起,我没有&#39;我真的想读你所有的json,我只是猜测tolongitude = row.getString("to_longitude");这行可以抛出一个异常,就像代码中获取值的所有行一样,所以,如果没有带有该名称的字符串其中一个对象,他不会补充。 解决方案use object.optString(name),只需将opt添加到您的所有获取方法中,这应该可行,如果有帮助,请发布给我