2016-12-09 50 views
0

我想創建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); 
     } 
    } 
} 
+0

我認爲你是多次放置值 – Vinodh

+0

可能是一個不能重置的緩衝區字符串。因此,在你把價值放在你的價值之前,要有價值「,」它保留前一個值在記憶中。所以,而不是做:'1 - >「」 - > 2 - >「」 - > 3',它會做'1 - > 2 - > 3',所以字符串做'1 - > 12 - > 123' 1→2→3「。明確地說,檢查你的字符串在插入之間是否被正確重置 –

+0

@dondo:你可以在你將jsonObject放入每個項目的地方分享代碼嗎? – Vinodh

回答

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完全填充後,將數組插入b中。

for (int i=0;i<=size;i++){ 
     jArray.put(adapter3.getItem(i).getJsonObject()); 
} 
b.append(jArray); 
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); 
0

必須設置Id和ratingStar或通過throught getJsonObject的價值,我認爲通過閱讀代碼,您有ID的靜態值的第二ratingStar

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