我想創建jsonObject
與輸出,的Android - 循環的getJsonObject出錯
這是我用來獲取jsonObject
代碼:
public JSONObject getJsonObject() {
JSONObject obj = new JSONObject();
try {
obj.put("id", id);
obj.put("nilai", ratingStar);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
但是當我循環利用內幕按鈕這段代碼的JSON .onClick:
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);
}
}
}
我認爲你是多次放置值 – Vinodh
可能是一個不能重置的緩衝區字符串。因此,在你把價值放在你的價值之前,要有價值「,」它保留前一個值在記憶中。所以,而不是做:'1 - >「」 - > 2 - >「」 - > 3',它會做'1 - > 2 - > 3',所以字符串做'1 - > 12 - > 123' 1→2→3「。明確地說,檢查你的字符串在插入之間是否被正確重置 –
@dondo:你可以在你將jsonObject放入每個項目的地方分享代碼嗎? – Vinodh