Android - 为getJsonObject循环出错

时间:2016-12-09 08:29:57

标签: android

我想用输出

创建jsonObject

这是我用来获取jsonObject的代码:

public JSONObject getJsonObject() {
    JSONObject obj = new JSONObject();
    try {
        obj.put("id", id);
        obj.put("nilai", ratingStar);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return obj;
}

但是当我使用button.onClick中的代码循环json时:

 int size = adapter3.getCount();
 StringBuilder b = new StringBuilder();
 JSONArray jArray = new JSONArray();
 for (int i=0;i<=size;i++){
      b.append(jArray.put(adapter3.getItem(i).getJsonObject()));
 }
 TextView Star = (TextView)findViewById(R.id.test);
 Star.setText(b.toString());

为什么输出出错?

输出结果如下:

[{"id":"1","nilai":"1"},
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},
[{"id":"3","nilai":"1"},
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},
[{"id":"3","nilai":"1"},
[{"id":"4","nilai":"1"},
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},
[{"id":"3","nilai":"1"},
[{"id":"4","nilai":"1"},
[{"id":"5","nilai":"1"},

就像输出重复来自1 | 1_2 | 1_2_3 | 1_2_3_4 | 1_2_3_4_5

的id

我做错了吗?为什么输出像那样重复

修改

Pertanyaan.java

public class Pertanyaan {
private String id;
private float ratingStar;
private String ask;

Pertanyaan(String id, int ratingStar, String ask) {
    this.id = id;
    this.ratingStar = ratingStar;
    this.ask = ask;
}

public String getId() {
    return id;
}

float getRatingStar() {
    return ratingStar;
}

void setRatingStar(float ratingStar) {
    this.ratingStar = ratingStar;
}

public String getAsk() {
    return ask;
}

public JSONObject getJsonObject() {
    JSONObject obj = new JSONObject();
    try {
        obj.put("id", id);
        obj.put("nilai", ratingStar);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return obj;
}
}

PertanyaanAdapter.java

class PertanyaanAdapter extends ArrayAdapter<Pertanyaan> {

    private AppCompatActivity activity;
    private List<Pertanyaan> movieList;

    PertanyaanAdapter(AppCompatActivity context, int resource, List<Pertanyaan> objects) {
        super(context, resource, objects);
        this.activity = context;
        this.movieList = objects;
    }

    @Override
    public Pertanyaan getItem(int position) {
        return movieList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
            //holder.ratingBar.getTag(position);
        }

        holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(position));

        holder.ratingBar.setTag(position);
        holder.ratingBar.setRating(getItem(position).getRatingStar());
        holder.movieName.setText(getItem(position).getAsk());
        holder.id.setText(getItem(position).getId());

        return convertView;
    }

    private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final int position) {
        return new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                Pertanyaan item = getItem(position);
                assert item != null;
                item.setRatingStar(v);
                Log.i("Adapter", "star: " + v);
            }
        };
    }

    private static class ViewHolder {
        private RatingBar ratingBar;
        private TextView movieName;
        private TextView id;

        ViewHolder(View view) {
            ratingBar = (RatingBar) view.findViewById(R.id.rate_img);
            movieName = (TextView) view.findViewById(R.id.text);
            id = (TextView) view.findViewById(R.id.textid);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

for (int i=0;i<=size;i++){
      b.append(jArray.put(adapter3.getItem(i).getJsonObject()));
}

你的jArray:

i = 0
[{"id":"1","nilai":"1"},

i = 1
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},

i = 2
[{"id":"1","nilai":"1"},
[{"id":"2","nilai":"1"},
[{"id":"3","nilai":"1"},

错误很简单。您不断向jArray添加数据,并将整个数组重新插入b中。 在i = 0时,它有1个值。 但是在i = 1时,由于你新字符串放在其中,它仍然保留了前一个字符串,所以你在数组中有2个值,依此类推。

解决方法是在完全填充数组后,将数组仅插入b中一次。

for (int i=0;i<=size;i++){
      jArray.put(adapter3.getItem(i).getJsonObject());
}
b.append(jArray);

答案 1 :(得分:1)

 int size = adapter3.getCount();
 StringBuilder b = new StringBuilder();
 JSONArray jArray = new JSONArray();
 for (int i=0;i<=size;i++){
     jArray.put(adapter3.getItem(i).getJsonObject());
 }
 b.append(jArray.toString());
 TextView Star = (TextView)findViewById(R.id.test);
 Star.setText(b);

答案 2 :(得分:0)

你必须设置Id和ratingStar或者通过getJsonObject传递值,我想通过读取该代码,你有静态值id和nd ratingStar

public JSONObject getJsonObject() {
JSONObject obj = new JSONObject();
try {
    obj.put("id", id);
    obj.put("nilai", ratingStar);
} catch (JSONException e) {
    e.printStackTrace();
}
return obj;
}