数组列表显示在文本视图中

时间:2016-05-11 13:36:33

标签: android arraylist textview

我想在我尝试使用for循环的textview中显示来自api响应的数组列表,但它只显示一次,我一次需要整个列表。当我尝试使用listview但它有滚动动作时,我不需要滚动动作。我想一次显示数组列表。请帮助我..

这是我使用listview的代码:

private void prepareEmergencyAll(JSONArray emergency_all) {
   LinearLayout myLayout1 = (LinearLayout)findViewById(R.id.listTL);
    ListView emgcyall_list = (ListView)findViewById(R.id.emgcyallLV);
   // LinearLayout myLayout2 = (LinearLayout) findViewById(R.id.phonelistTL);

    for(int i=0;i<emergency_all.length();i++){
        Emergency_all emglist = new Emergency_all();
        try {
            JSONObject single = emergency_all.getJSONObject(i);
            emglist.setFullname(single.getString("fullname"));
            emglist.setPhone(single.getString("phone"));
            list.add(emglist);
       //     emgcyall_list.setAdapter(adapter1);

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


    adapter1=new EmgcyAdapter(ViewActivity.this,list);
    emgcyall_list.setAdapter(adapter1);

  //  adapter1 = new EmgcyAdapter(,list);








    }

3 个答案:

答案 0 :(得分:0)

您可以使用StringBuilder

StringBuilder builder = new StringBuilder();
for (String s : yourArrayList) {
  builder.append(s).append(" ");
}
yourTextView.setText(builder.toString());

答案 1 :(得分:0)

ArrayList<String> a = new ArrayList<>();
        String end ="";
        for(int i = 0;i<a.size();i++){
            end = end + "\n"+a.get(i);
        }

如果您无法理解StringBuilder:)

,可以试试这个

答案 2 :(得分:0)

我得到了listview高度动态变化的解决方案.. 这是代码..

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i <listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        // listItem.measure(0, 0);
        listItem.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
}

并在主要活动中添加以下内容。

 listview.setAdapter(adapter);
    Utility.setListViewHeightBasedOnChildren(listview);