我正在尝试在文本视图1和文本视图2中显示字符串item_1
和item_2
。
在一些专家的帮助下,我创建了下面的代码。
如何在文本视图1和文本视图2中显示字符串item_1
和item_2
?
我的Java代码:
public class Prices extends AppCompatActivity {
TextView item_1 , item_2 ,a3 ,a4 ,a5 ,a6;
String url = "https://000000/app/almaraa/show.php";
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prices);
TextView item_1 = (TextView) findViewById(R.id.item_1);
TextView item_2 = (TextView) findViewById(R.id.item_2);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
final Globalv globalv = (Globalv) getApplicationContext();
try {
JSONArray jsonArray = response.getJSONArray("allmess");
for (int i = 0; i < response.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
String id = respons.getString("id");
String item_1 = respons.getString("item_1");
String item_2 = respons.getString("item_2");
}
JSONObject respons2 = jsonArray.getJSONObject(0);
String id = respons2.getString("id");
globalv.setTotal_threads(Integer.parseInt(String.valueOf(id)));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
}
发布于 2018-06-03 03:49:47
您必须使用方法setText(Charsequence)并在UI线程上运行代码。但在此之前,您必须重命名String
的item_1
和item_2
,因为您的TextView
具有相同的名称。下面是重命名为字符串值的代码:
//......... inside your jsonObjectRequest
//rename item_1 and item_2
String myItem_1 = respons.getString("item_1");
String myItem_2 = respons.getString("item_2");
Prices.this.runOnUiThread(new Runnable() {
public void run() {
item_1.setText(myItem_1);
item_2.setText(myItem_2);
}
});
https://stackoverflow.com/questions/50660466
复制相似问题