0

I'm trying to split the names that , I have in my DB using for loop and adding to a different LinearLayout each name, Right now I get a name but I Can't see more names if I try object show me the full array but doesn't split the names .

public void onResponse(String response) {
                Log.e("Response: ", response.toString());

                try {
    JSONObject jObj = new JSONObject(response);
                        String getObject = jObj.getString("name");
                        JSONArray jsonArray = new JSONArray(getObject);

            for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                                    StringBuilder sb = new StringBuilder();

                                    Log.d("myTag",""+jsonArray);

                                    title = jsonObject.getString(TITLE);
                                    sb.append(jsonArray.getString(i));
                                    mValue.setText(sb.toString());
                                    mValue.setId(i);

                                    //ADD text View to Linear Layout
                                    ((LinearLayout) linear).removeAllViews();
                                    ((LinearLayout) linear).addView(mValue);
        }
Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22

1 Answers1

0
public void onResponse(String response) {
                Log.e("Response: ", response.toString());

                try {
    JSONObject jObj = new JSONObject(response);
                        String getObject = jObj.getString("name");
                        JSONArray jsonArray = new JSONArray(getObject);
            ((LinearLayout) linear).removeAllViews();
            for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                                    StringBuilder sb = new StringBuilder();

                                    Log.d("myTag",""+jsonArray);

                                    title = jsonObject.getString(TITLE);
                                    sb.append(jsonArray.getString(i));
                                    //The line below creates a new textview
                                    TextView mValue = new TextView(this);
                                    mValue.setText(sb.toString());
                                    mValue.setId(i);

                                    //ADD text View to Linear Layout

                                    ((LinearLayout) linear).addView(mValue);
        }

You are removing all the views from the LinearLayout in every loop of the for loop. Take the line ((LinearLayout) linear).removeAllViews(); out of the for loop like I have posted above.

Edit: Look at the updated code. The reason why you were getting that is because you were trying to reuse the textview when you need to create a new one every time in the for loop

Napster
  • 1,353
  • 1
  • 9
  • 11
  • yes, I tried before with this the problem is that launch this error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. – rigo romero Jul 31 '19 at 21:42
  • Updated the answer – Napster Aug 01 '19 at 20:46