Skip to main content
4 de 4
replaced http://pt.stackoverflow.com/ with https://pt.stackoverflow.com/

Criar TextView's dinamicamente

Tenho um TextView e gostaria que, em vez do preenchimento com um Arraylist, com todos os itens em seu setText, que ele fosse criado várias vezes na tela, com os respectivos itens deste array, em cada setText de cada TextView.

Segue o código:

public class RideRequestActivity extends AppCompatActivity implements CommunityRequestView {

    private SessionManager user;
    private String departure;
    private String arrival;



    @BindView(R.id.request_email)
    TextView email;


    @BindView(R.id.request_name)
    TextView name;


    @BindView(R.id.request_photo)
    ImageView photo;


    @BindView(R.id.textView_first)
    TextView first;

    @BindView(R.id.textView_second)
    TextView second;

    @BindView(R.id.textView_third)
    TextView third;

    @BindView(R.id.button0)
    CheckBox button0;

    @BindView(R.id.button1)
    CheckBox button1;

    @BindView(R.id.button2)
    CheckBox button2;

    @BindView(R.id.button3)
    CheckBox button3;

    @BindView(R.id.button4)
    CheckBox button4;

    @BindView(R.id.button5)
    CheckBox button5;

    @BindView(R.id.send_offer_request)
    Button send_offer_request;

    @BindView(R.id.send_ask_request)
    Button send_ask_request;

    @BindView(R.id.txtInterests)
    TextView mListView;

    @BindView(R.id.img_favorite)
    ImageView imgFavorite;

    CommunityPresenter presenter;
    UserCommunity selectedUser;
    UsersMySQLiteHelper serviceDB;
    private List<UserCommunity> userInterest = new ArrayList<UserCommunity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setEnterTransition(new Fade());
        }
        setContentView(R.layout.activity_ride_request);
        serviceDB = new UsersMySQLiteHelper(AppController.getInstance().getApplicationContext());

        ButterKnife.bind(this);
        presenter = new CommunityPresenter(this);


        first.setText(presenter.getDay(0));
        second.setText(presenter.getDay(1));
        third.setText(presenter.getDay(2));

        user = new SessionManager();

        selectedUser = CommunityService.i(getContext()).selectedUser;
        reloadView();

        send_offer_request.setOnClickListener(presenter.sendRequestOnClickListener(0));
        send_ask_request.setOnClickListener(presenter.sendRequestOnClickListener(1));
        imgFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(selectedUser.favorite == 0) {
                    selectedUser.favorite = 1;
                    imgFavorite.setImageResource(R.drawable.ic_star_black_on_48px);
                }else{
                    selectedUser.favorite = 0;
                    imgFavorite.setImageResource(R.drawable.ic_star_border_black_off_48px);
                }
                selectedUser.save(serviceDB, user.getLoggedUser().getCompany());
            }
        });

        populateInterests();
        getPoints();

        imgFavorite = (ImageView)findViewById(R.id.img_favorite);
        if (selectedUser.favorite == 0){
            imgFavorite.setImageResource(R.drawable.ic_star_border_black_off_48px);
        }else{
            imgFavorite.setImageResource(R.drawable.ic_star_black_on_48px);
        }




    }

    private void getPoints() {
    }

    private void populateInterests() {
        RequestManager.UsersInterests(selectedUser.id, new FutureCallback<String>() {
            @Override
            public void onCompleted(Exception e, String result) {
                if(e== null || result != null) {
                    UserCommunity user = new Gson().fromJson(new JsonParser().parse(result).getAsJsonObject().get("data").toString(),
                            UserCommunity.class);

                    selectedUser.interests = user.interests;
                    String name = "";
                    TextView txt_Interests = (TextView) findViewById(R.id.txtInterests);
                    for (UserCommunity.Interest interest: selectedUser.interests
                         ) {
                           name += interest.name + " ";
                           txt_Interests.setText(name);
                    }
                    String point = "";
                    TextView points = (TextView)findViewById(R.id.txt_Points);
                    selectedUser.points = user.points;
                    point = selectedUser.points + "\npontos";
                    points.setText(point);

                }
            }
        });

    }

XML:

<RelativeLayout
    android:id="@+id/relative_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/request_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/avatar5"
        android:transitionName="infoboxImage" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/request_photo"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/img_favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:src="@drawable/ic_star_border_black_off_48px" />

        <TextView
            android:id="@+id/request_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/img_favorite"
            android:padding="5dp"
            android:text="Abraao Barros Lacerda"
            android:textAppearance="@android:style/TextAppearance.Material.Display1"
            android:textSize="25sp"
            android:transitionName="infoboxName" />

        <TextView
            android:id="@+id/request_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/request_name"
            android:padding="5dp"
            android:text="[email protected]" />

        <ImageView
            android:id="@+id/img_points"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignRight="@+id/request_name" />

        <TextView
            android:id="@+id/txt_Points"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/img_points"
            android:layout_below="@+id/img_points"
            android:text="50.000 pontos" />

        <LinearLayout
            android:id="@+id/linear_interests"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txt_Points"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txtInterests"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="6dp"
                android:text="INTERESTS"
                android:textStyle="bold"
                android:background="@drawable/txt_border_circle"/>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/linear_interests">

            <android.support.v7.widget.CardView
                android:id="@+id/card_1"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView_first"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:padding="10dp"
                        android:text="Segunda-feira"
                        android:textAppearance="@android:style/TextAppearance.Material.Small"
                        android:textSize="14sp" />

                    <CheckBox
                        android:id="@+id/button0"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:text="Volta -18:00" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/card_2"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_below="@+id/card_1"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView_second"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="Quarta-Feira"
                        android:textAppearance="@android:style/TextAppearance.Material.Small" />

                    <CheckBox
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Volta -18:00" />


                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/card_3"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_below="@id/card_2"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView_third"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="Quinta-Feira"
                        android:textAppearance="@android:style/TextAppearance.Material.Small" />

                    <CheckBox
                        android:id="@+id/button4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Volta -18:00" />


                </LinearLayout>
            </android.support.v7.widget.CardView>

            <Button
                android:id="@+id/send_offer_request"
                style="@style/ByndButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_below="@+id/card_3"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:paddingTop="10dp"
                android:text="Oferecer carona" />

            <Button
                android:id="@+id/send_ask_request"
                style="@style/ByndButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_below="@id/card_3"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:paddingTop="10dp"
                android:text="Pedir carona" />
        </RelativeLayout>


    </RelativeLayout>


</RelativeLayout>

Melhoria para esta questão, que eu mesmo coloquei: Como preencher setText do TextView a partir de um ArrayList?

Henrique
  • 1mil
  • 13
  • 34