0

So I have this little problem I can't deal with... Check code and read Logcat, any ideas?

CODE:

for(int i = 0; i < jArray.length(); ++i){
    JSONObject json_data = jArray.getJSONObject(i);

    //                                      
    String paskaita = json_data.getString("paskaita");
    String laikas = json_data.getString("laikas");
    String savaite = json_data.getString("savaite");                                        
    String auditorija = json_data.getString("auditorija");
    String destytojas = json_data.getString("destytojas");                                      
    String dalykas = json_data.getString("dalykas");
    String tipas = json_data.getString("tipas");


    //Just adding view                              
    TextView pask = (TextView) Item.findViewById(R.id.paskaita);
    pask.setText(paskaita);

    TextView laik = (TextView) Item.findViewById(R.id.laikas);
    laik.setText(laikas);

    TextView sav = (TextView) Item.findViewById(R.id.savaite);
    sav.setText(savaite);

    TextView dest = (TextView) Item.findViewById(R.id.destytojas);
    dest.setText(destytojas);

    TextView aud = (TextView) Item.findViewById(R.id.auditorija);
    aud.setText(auditorija);

    TextView dal = (TextView) Item.findViewById(R.id.dalykas);
    dal.setText(dalykas);

    TextView tip = (TextView) Item.findViewById(R.id.tipas);
    tip.setText(tipas);

    ln.addView(Item);

}

Logcat output:

12-07 19:00:27.558: E/AndroidRuntime(280): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • Instead of using `getString`, use [optString(name, fallback)](http://developer.android.com/reference/org/json/JSONObject.html#optString), see if this fix your problem. – Wenhui Dec 07 '13 at 18:51
  • why do you use ++i instead of i++? – vandus Dec 07 '13 at 18:56
  • But my problem is jArray size, as I said above that if jArray is [{...}] it works, but if it's [{...},{...},{...}] it doesn't. –  Dec 07 '13 at 18:56
  • can you paste the LogCat output? – vandus Dec 07 '13 at 18:57
  • @vandus `++i` can be [faster in `C` type languages](http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c) and some people just like that style. There is 0 difference for `for` loops in Java. Just a matter of style. – zapl Dec 07 '13 at 19:01
  • @vandus I edited post with it. –  Dec 07 '13 at 19:06
  • well, the Item view apparently already has a parent view. if the ln already contains the Item view, just update it, or create a new view instead of the Item one and add it to ln. since you add more items in the second case, you need a different view for each item to add to ln. – vandus Dec 07 '13 at 19:09
  • So the problem is this line? `ln.addView(Item);` –  Dec 07 '13 at 19:12
  • It works because there is only one item, so you are not reusing the views that are already attached to `ln`. – Wenhui Dec 07 '13 at 19:14
  • I see. What's the best way to deal with it? `jArray.length()` is 3, so FOR loop will be activating `ln.addView(Item)` for 3 times. It is obvious that I need it first time, how about second and third time? I can't just delete it. –  Dec 07 '13 at 19:19
  • See my answer, basically, you can either create a layout that contains all the TextView's you need, and then inflate the layout every loop, or you can create TextView's programmatically ( not inflate from layout file), then you will not need to worry this problem. It really depends how you design your UI. – Wenhui Dec 07 '13 at 19:25

1 Answers1

1

OK, it has nothing to do with your JSONObject.

The Logcat says it all. You add a TextView that has already had parent. So you may want to create a layout, and in your for loop

for(int i = 0; i < jArray.length(); ++i){
JSONObject json_data = jArray.getJSONObject(i);

//                                      
String paskaita = json_data.getString("paskaita");
String laikas = json_data.getString("laikas");
String savaite = json_data.getString("savaite");                                        
String auditorija = json_data.getString("auditorija");
String destytojas = json_data.getString("destytojas");                                      
String dalykas = json_data.getString("dalykas");
String tipas = json_data.getString("tipas");

// Create a new view every time.
View Item = View.inflate(ln.getContext(), layoutID, null);


//Just adding view                              
TextView pask = (TextView) Item.findViewById(R.id.paskaita);
pask.setText(paskaita);

TextView laik = (TextView) Item.findViewById(R.id.laikas);
laik.setText(laikas);

TextView sav = (TextView) Item.findViewById(R.id.savaite);
sav.setText(savaite);

TextView dest = (TextView) Item.findViewById(R.id.destytojas);
dest.setText(destytojas);

TextView aud = (TextView) Item.findViewById(R.id.auditorija);
aud.setText(auditorija);

TextView dal = (TextView) Item.findViewById(R.id.dalykas);
dal.setText(dalykas);

TextView tip = (TextView) Item.findViewById(R.id.tipas);
tip.setText(tipas);

ln.addView(Item);

}

Wenhui
  • 648
  • 4
  • 13
  • Thank you so much! It works. I'm new at Android and I love this page. –  Dec 07 '13 at 19:28