I have a model

List<Integer> products = new ArrayList<Integer>(); 

And getters and setters are written to it:

 public List<Integer> getProducts() { return products; } public void setProducts (List<Integer> products ) { this.products = products ; } 

The task is to display the data of this list in my activation.

For example, to make a similar for the name field, which I also use from the model, I did the following: 1. Created a field in xml

  <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> 

2. Announced in activation:

 TextView name; 

then find this item

 TextView name = (TextView) findViewById(R.id.name); 

And display it in the activit:

 name.setText(model.getName()); 
  • one
    What do you mean by the word объявить ? Something tells me that you have some kind of your own, and not a generally accepted definition of this concept. Because of this, the question becomes incomprehensible. - post_zeew
  • @post_zeew display) as I displayed the product name) - Inkognito
  • And how do you want it to look like? - Sergey Gornostaev
  • @SergeyGornostaev well, it doesn’t matter to me for the first time, for example name I declared as a simple TextView with parameters in width and height) the main problem for me is how it’s possible to list later. The main thing that he was displayed in the activit - Inkognito Nov.
  • Well, if there is no difference, then name.setText(model.getProducts()); should arrange. - Sergey Gornostaev

1 answer 1

If I understood correctly, each element of the list corresponds to a single instance of the Model class, and this class, among other things, also contains some list.

In the Model class, implement the method that will return the ArrayList text representation in the required format:

 public String getProductsAsString() { if (products == null || products.isEmpty()) { return ""; } else { StringBuilder stringBuilder = new StringBuilder(); for (int i=0; i<products.size(); i++) { stringBuilder.append(products.get(i)); if (i+1 != products.size()) { stringBuilder.append("\n"); } } return stringBuilder.toString(); } } 

And then call him:

 name.setText(model.getProductsAsString()); 

If you need, for example, to handle clicks on the elements of this sublist , then in the adapter for RecyclerView you need to create another ViewHolder , which will correspond to an element of this sublist .

  • @Morozov; Show the entire model file with the addition of my method and the code in which you call this method. You first wrote that you had List<Integer> products , and now in some mysterious way, View products . - post_zeew
  • updated the question. Like everything as you indicated - Inkognito
  • @Inkognito, The kids variable in the populateTextViews() method must be of type TextView . - post_zeew Nov.
  • put the ListView, displays in red the 'setText' method. Cannot resolve method 'java.lang.String' - Inkognito
  • one
    @Inkognito; If this method is used somewhere else, you can take it somewhere. - post_zeew