如何在textview android中显示字符串?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (224)

我是Android新手,刚开始学习。

  • 我正在尝试的是在文本视图1中显示字符串item_1和item_2,并且文本视图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);
    }
}
提问于
用户回答回答于

你必须用方法setText:

//......... 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);
    }
});

扫码关注云+社区

领取腾讯云代金券