我的arraylist中的字符串迭代到textview不起作用

时间:2017-06-30 03:44:56

标签: java android arraylist

您好我想根据我从JSONObject从我的arraylist获得的数据来设置textview的文本。但是当我尝试将我的arraylist迭代到我的textview时,我的应用程序停止工作。你能帮助我解决我的问题。 Thaaanks!这是我的代码。谢谢

            AsyncClass taskSpecialty = new AsyncClass(DetailActivity.this, postData, false, new AsyncResponse() {
            @Override
            public void processFinish(String s) {
                Log.d("DetailAcitivty", s);
                try {
                    JSONObject parentObject = new JSONObject(s);
                    JSONArray parentArray = parentObject.getJSONArray("result");

                    StringBuffer finalBufferedData = new StringBuffer();
                    for (int i=0 ; i<parentArray.length(); i++){
                        JSONObject finalObject = parentArray.getJSONObject(i);
                        String spcltID = finalObject.getString("sName");
                        finalBufferedData.append(spcltID + "\n");
                    }
                    ArrayList<String> listdata = new ArrayList<String>();
                    for(int i =0; i<parentArray.length(); i++){
                        listdata.add(parentArray.getString(i));
                    }
                    LinearLayout llSpecialty = (LinearLayout) findViewById(R.id.llSpecialty);

                    Iterator<String> iterator = listdata.iterator();
                    while (iterator.hasNext()){
                        TextView textView = new TextView(DetailActivity.this);
                        textView.setText(listdata.get(1));
                        llSpecialty.addView(textView);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        taskSpecialty.execute("http://10.0.2.2/wingman/specialty.php");

2 个答案:

答案 0 :(得分:1)

您需要在while循环中调用iterator.next()

textView.setText(iterator.next());

答案 1 :(得分:1)

您可以按照以下方式执行此操作:

String str="";
while (iterator.hasNext()){
     TextView textView = new TextView(DetailActivity.this);
     str+=iterator.next();
     llSpecialty.addView(textView);
}

textView.setText(str);