在Android textview中显示字符串

时间:2018-06-02 19:34:30

标签: java android

我尝试在文本视图1中显示字符串item_1item_2,而不显示文本视图2.

在一些专家的帮助下,我创建了以下代码。

如何在文本视图1和文本视图2中显示字符串item_1item_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);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须使用方法setText(Charsequence)并在UI线程上运行代码。 但之前,您必须重命名item_1 s item_2TextView,因为您的//......... 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); } }); 具有相同的名称。在具有字符串值的代码下方重命名为:

Sub DoStuff1()

Dim WS As Worksheet
Dim LR As Long, FR As Long
Dim CL As Range
Application.ScreenUpdating = False 'Turn the screen refresh off

For Each WS In ThisWorkbook.Sheets 'Loop through your sheets
    WS.Activate
StartHere: LR = WS.Cells(Rows.Count, "A").End(xlUp).Row - 1 'Get the dynamic last used row
    Set CL = WS.Columns(1).Find(What:=WS.Name, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not CL Is Nothing Then
        FR = CL.Row 'Get the row which is the value
        If FR > 2 And FR < LR Then 'If larger than 2 but smaller than last used row then
            WS.Range(Cells(2, 1), Cells(FR - 1, 2)).Delete Shift:=xlUp
            GoTo StartHere
        ElseIf FR = 2 And FR < LR Then 'If FR = 2 but still some rows between FR and LR
            WS.Range(Cells(FR + 1, 1), Cells(LR, 2)).Delete Shift:=xlUp
            GoTo StartHere
        ElseIf FR = LR And FR > 2 Then 'If A is the lastrow with a value but rows between 2 and FR
            WS.Range(Cells(2, 1), Cells(FR - 1, 2)).Delete Shift:=xlUp
            GoTo StartHere
        Else
            'If there is only the startrow, the foundrow with value and the very last row left...
        End If
    End If
Next WS

Application.ScreenUpdating = True 'Turn the screen refresh back on
End Sub