【问题标题】:Set the text of a Textview from a returned String array从返回的字符串数组中设置 Textview 的文本
【发布时间】:2017-03-07 23:19:45
【问题描述】:
public String[] getpathsFromJSON (String JSONStringparam) throws JSONException{

    JSONObject JSONString = new JSONObject(JSONStringparam);

    JSONArray seriearray = JSONString.getJSONArray("results");
    String [] result = new String [seriearray.length()];

    for (int i = 0; i<seriearray.length();i++){
        JSONObject serie = seriearray.getJSONObject(i);
        String serienaam = serie.getString("name");
        result[i] = serienaam;
    }
    return result;
}

我想使用返回的结果之一在文本视图中显示。 最好的方法是什么?

【问题讨论】:

    标签: java android return


    【解决方案1】:

    您只想使用一个结果?

    TextView view = (TextView) findViewById(R.id.yourTextViewResourceId); 
    
    view.setText(result[some_int_in_your_array]);
    

    由于您正在为 Android 编程,我假设您使用的是 Android Studio,因此您可以在 GUI 界面中为 XML 创建文本视图并使用 findViewById。

    如果你想全部显示:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutViewEx);
    
    for (int i = 0; i < result.length(); i++) {
            TextView textView = new TextView(getActivity());           
            textView.setText(result[i]);
            linearLayout.addView(textView);
        }
    

    【讨论】:

    • ArrayAdapter&lt;String&gt;ListView 中会更好
    • @GerMex 非常感谢您!正是我在寻找的地方。是的,我使用的是 android studio。
    • @cricket_007 你的意思是我想把它们都展示出来!那么我最好使用 listview + 适配器?
    • @rickkorsten 正确。 ListViews 在滚动数据列表方面比 LinearLayouts 进行了更好的优化。
    • @cricket_007 如何在方法外或其他方法中使用结果?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多