gpt4 book ai didi

java - 如何将String数组设置到TextView中?

转载 作者:行者123 更新时间:2023-11-29 08:40:54 25 4
gpt4 key购买 nike

我正在尝试将一个数组的字符串设置到 TextView 中。我正在尝试这样的事情:

public class promo extends Fragment {
private int date;
private String[] gp_market = new String[5];
Calendar calendar = Calendar.getInstance();
TextView super_1;
public promo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_news,
container, false);
super_1 = (TextView) view.findViewById(R.id.super_1);
final RequestQueue gpQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
date = calendar.get(Calendar.MONTH);
final String url = "myurl_webservice.php?date="+date; //edited
final JsonArrayRequest gp_request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response)
try
for(int i=0; i < response.length(); i++){
JSONObject gp_object = response.getJSONObject(i);
gp_market[i] = gp_object.getString("product");
}
//I can see the data, the parsing is working.
Toast.makeText(getActivity().getApplicationContext(), gp_market[0], Toast.LENGTH_LONG).show();
}catch (JSONException e){
Toast.makeText(getActivity().getApplicationContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(), error.toString() , Toast.LENGTH_SHORT).show();
}
});
gpQueue.add(gp_request);
super_1.setText(gp_market[0]);
return view;
}
}

但是文本是空的。正确的做法是什么?

最佳答案

你有TextView的id吗?试试这个:


主 Activity .java

String[] name_array = new String[2];
name_array[0]="test";

TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(name_array[0]);

activity_main.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt"/>

关于java - 如何将String数组设置到TextView中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40318009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com