I have 2 records inserted in a SQLite table and I load them into an array to show them in a recyclerview with an Adapter, but in the recyclerview it shows me the second record twice, here I leave the code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
listCreditos=new ArrayList<>();
arrayList = new ArrayList<>();
_inicio= new Inicio();
idU=getArguments().getString("idU");
if(GetListCreditos())//llenar array desde sqlite
arrayList=GetArrayListInicioItem();
adapter = new InicioRecyclerAdapter(context, (ArrayList) arrayList, idU);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return rootView;
}
public boolean GetListCreditos() {
con=new conexion(context);
con.open();
bd=con.getReadableDatabase();
inicio=null;
String query="Select * from " + Inicio.TInicio +" where IdU= '"+idU+"'";
Cursor cursor=bd.rawQuery(query, null);
if(cursor !=null && cursor.moveToFirst())
{
do {
id = cursor.getInt(cursor.getColumnIndex(_inicio.id));
estatus=cursor.getInt(cursor.getColumnIndex(_inicio.Estatus));
Nombre=cursor.getString(cursor.getColumnIndex(_inicio.Nombre));
inicio = new Inicio(id,estatus,Nombre);
listCreditos.add(inicio);//aqui se cargan los dos registros que hay
} while (cursor.moveToNext());
}
else{
Toast.makeText(context, "No se encontraron datos", Toast.LENGTH_SHORT).show();
}
cursor.close();
return true;
}
I made this method to pass the array of the query to another array so that it maps the data in another class and can show them: The problem is here, because it is where I duplicate the data, nose, but I think that is when I send the variables status and name, apparently "take" the last added value that is why the last data is always duplicated. I hope you have given me to understand.
public ArrayList<InicioItem> GetArrayListInicioItem(){
if(listCreditos != null) {
for (int i = 0; i < listCreditos.size(); i++) {
inicio = listCreditos.get(i);
arrayList.add(new InicioItem(_inicio, context, estatus,Nombre));
}
}
return arrayList;
}
Adapter
Adapter
public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecycler iewHolder> implements Filterable {
private ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
private Context context;
String idU;
public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList, String idU)
{
this.context = context;
inflater = LayoutInflater.from(context);
this.arrayList = arrayList;
this.idU=idU;
}
@Override
public void onBindViewHolder(InicioRecyclerViewHolder holder, int position) {
InicioItem inicioItem = arrayList.get(position);
holder.imgvInicioImgBackground.setImageResource(inicioItem.imgBackground());
holder.imgvInicioImg.setImageResource(inicioItem.img());
holder.txtvInicioNombre.setText(inicioItem.nombre());
}
}
Class to map the query data HomeItem
public class InicioItem {
int img, imgbackground, estatus;
String Nombre;
private Inicio _inicio;
public InicioItem(Inicio inicio, Context context, String Nombre, int estatus) {
this.estatus=estatus;
this.Nombre=rNombre;
this.context=context;
this._inicio=inicio;
_inicio=new Inicio();
}
public Inicio inicio(){
return _inicio;
}
public int imgBackground(){
imgbackground = R.drawable.background_item_red;
if(_inicio != null){
switch (estatus){
case 0:
imgbackground = R.drawable.background_item_red; break;
case 1:
imgbackground = R.drawable.background_item_teal; break;
case 2:
imgbackground = R.drawable.background_item_blue; break;
}
}
return imgbackground;
}
public int img(){
img = R.drawable.ic_item_no_asignado;
if(_inicio != null){
switch (estatus){
case 0:
img = R.drawable.ic_item_no_asignado;
break;
case 1:
img = R.drawable.ic_item_activo;
break;
case 2:
img = R.drawable.ic_item_convenio_promesa_pago;
break;
}
}
return img;
}
public String nombre(){
String nombre = "Nombre: ";
if(_inicio != null){
if(Nombre != "")
return nombre +Nombre;
}
return "";
}
}