Как использовать myLayout.addView (View v) внутри метода onPostExecute в AsyncTask - PullRequest
0 голосов
/ 02 января 2019

Что я должен сделать, чтобы добавить новые TextViews (с текстом "inf1" и "inf2" из кода) в существующий линейный макет из метода onPostExecute в AsyncTask?

    protected void onPostExecute(String response) {
        String inf1="";
        String inf2="";
    try {

        JSONObject jsonObject =new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray("result");


        for(int x=0; x < result.length(); x++) {
            JSONObject collegeData = result.getJSONObject(x);
            inf1 = collegeData.getString("title");
            inf2 = collegeData.getString("text");

            // Here I want to put these "inf1" and "inf2 strings into
            // textviews and add these textviews into an existing linear layout
            // with id: feedFetch"               

        }


    } catch (JSONException e) {
        e.printStackTrace();
    }


}

1 Ответ

0 голосов
/ 03 января 2019

Найдите представление, затем добавьте к нему ваши дочерние представления:

    LinearLayout layout_named_feedFetch=(LinearLayout)findViewById(R.id.feedFetch);
    //or call the_parent_view_of_feedFetch.findViewById() if it's the child another view; 
    for(int x=0; x < result.length(); x++) {
        JSONObject collegeData = result.getJSONObject(x);
        inf1 = collegeData.getString("title");
        inf2 = collegeData.getString("text");
        //create the new views
        TextView tv=new TextView(getApplicationContext());
        //add them to where you want
        layout_named_feedFetch.addView(tv);
        //assign values to them
        tv.setText(inf1+" "+inf2);
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...