ReciclerView android studio

0

I have a project and I do not know how to edit it.

The problem I have is that I need a title and a description. But right now both the title and the description are filled with the same information. How do I modify it? What do I have to do?

I put the code:

The main:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> dades = new ArrayList<String>();

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rView);
clerView.setLayoutManager(new LinearLayoutManager(this));
        Adaptador adapter = new Adaptador(this, dades);
        recyclerView.setAdapter(adapter);

        recyclerView.setItemAnimator(new DefaultItemAnimator());

        for(int i = 2; i <= 1; i++){
            dades.add(new String("Dada " + i));
        }
        adapter.notifyDataSetChanged();
    }
}

Adapter:

public class Adaptador extends RecyclerView.Adapter<Adaptador.ElMeuViewHolder> {
    private ArrayList<String> items;
    private Context context;

    public Adaptador(Context context, ArrayList<String> items) {
        this.context = context;
        this.items= items;
    }

    @Override
    public Adaptador.ElMeuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fila, null);
        ElMeuViewHolder viewHolder = new ElMeuViewHolder(itemLayoutView);
        return viewHolder;
    }
    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public void onBindViewHolder(ElMeuViewHolder viewHolder, int position) {

        viewHolder.vTitle.setText(items.get(position));
        viewHolder.vDescripcio.setText(items.get(position));

    }

    public static class ElMeuViewHolder extends RecyclerView.ViewHolder {
        //Només conté un TextView
        protected TextView vTitle;
        protected TextView vDescripcio;
        public ElMeuViewHolder(View v) {
            super(v);

            vTitle = (TextView) v.findViewById(R.id.title);
            vDescripcio = (TextView) v.findViewById(R.id.descripcio);

        }
    }
}

Now the result you give me is:

Dada1 (Title) dada1 (description)

I explain myself a little better that I think I have not understood, I would like to know how I can put myself values in title and description.

Por ejemplo 
Titulo: Pelicula
Descripción: De cine

Titulo: Musical
Descripcion : De cine

Ahora mismo lo que tengo es:
Dada 0 
Dada 0

Dada 1 
Dada 1

Can you help me?

thanks!

    
asked by Montse Mkd 11.10.2018 в 12:20
source

1 answer

2

The dades of type ArrayList<String> contains a single element that is assigned in the for loop:

for(int i = 2; i <= 1; i++){
   dades.add(new String("Dada " + i));
}

Since the variable i is assigned initial value 2, the loop only traverses one iteration. At the end of the first pass, the value of i is increased, and when checking the condition i<=1 , the loop ends because at that moment i=3 .

Subsequently, in:

public void onBindViewHolder(ElMeuViewHolder viewHolder, int position) {

   viewHolder.vTitle.setText(items.get(position));
   viewHolder.vDescripcio.setText(items.get(position));
}

The field vTitle and the field vDescripcio are assigned the same value items.get(position) since the variable position has the same value.

To add different values and not produce any error due to overflow, the first thing would be to modify the for loop to add in dades two different values, for example, starting variable i with value 0:

for(int i = 0; i <= 1; i++){
   dades.add(new String("Dada " + i));
}

In this way we will have two Strings ("Given 0" and "Given 1") in object dades .

Then, to have a different value in vTitle and vDescripcio it is enough to increase the value of the variable position with position + 1 for the description as in the following code:

public void onBindViewHolder(ElMeuViewHolder viewHolder, int position) {

   viewHolder.vTitle.setText(items.get(position));
   viewHolder.vDescripcio.setText(items.get(position + 1));
}

In this way, vTitle would be assigned the first element of items "Given 0" and a vDescripcio the second "Given 1"

EDITO

I leave the previous part so that the clarifications of the loop remain and the assignment to vTitle and vDescripcio

If you want to put values to those fields yourself, then you do not need the for loop.

Add two elements to dades with the string you want:

...
recyclerView.setItemAnimator(new DefaultItemAnimator());

dades.add(new String("Título: Película));
dades.add(new String("Descripción: De cine"));

adapter.notifyDataSetChanged();

...

In the assignment to the title and description variables, you should only use position as indicated above:

public void onBindViewHolder(ElMeuViewHolder viewHolder, int position) {

   viewHolder.vTitle.setText(items.get(position));
   viewHolder.vDescripcio.setText(items.get(position + 1));
}

You could create an array of arrays, and manually assign several values to have different options according to the position of the RecyclerView , and using a loop for :

Declare dades as a ArrayList matrix:

ArrayList<ArrayList<String>> dades = new ArrayList<ArrayList<String>>();

You create an ArrayList object that has two values, one for title and another for description:

ArrayList<String> elemento;

In a for loop, add the values of each field and add the created element to dades:

for(int i = 0; i < 2; i++){
    dade = new ArrayList<String>();
    switch (i) {
        case 0:
            dade.add(new String("Titulo: Película"));
            dade.add(new String("Descripción: de cine"));
        break;
        case 1: 
            dade.add(new String("Titulo: Musical"));
            dade.add(new String("Descripción: de cine"));
        break;
    }   
    dades.add(dade);
}

You modify the items object in the adapter and you get the elements in the bind:

...
private ArrayList<ArrayList<String>> items;

...

public Adaptador(Context context, ArrayList<ArrayList<String>> items) {
    this.context = context;
    this.items= items;
}
...
public void onBindViewHolder(ElMeuViewHolder viewHolder, int position) {

   ArrayList<String> elemento = new ArrayList<String>();
   elemento = items.get(position);

   viewHolder.vTitle.setText(elemento(0));
   viewHolder.vDescripcio.setText(elemento(1));
}
...
    
answered by 11.10.2018 / 13:12
source