Android Studio save data from arraylist in sqlite repeats the first field

Dope :

The data seems to save with no problems but at the momento to show it with a gridView the first field "Nombre" repeats itself and the other fileds doesnt show the correspondig data

i put some Log.d to see what is saving the list.add(new Negocios....

and it shows the correct data

This is what the activity shows

"Negocios" class

public Negocios(int id, String negocio, String categoria, String descripcion, byte[] image) {
    this.id = id;
    this.negocio = negocio;
    this.categoria = categoria;
    this.descripcion = descripcion;
    this.image = image;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNegocio() {
    return negocio;
}

public void setNegocio(String negocio) {
    this.negocio = negocio;
}

public String getCategoria() {
    return categoria;
}

public void setCategoria(String categoria) {
    this.categoria = categoria;
}

public String getDescripcion() {
    return descripcion;
}

public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}

public byte[] getImage() {
    return image;
}

public void setImage(byte[] image) {
    this.image = image;
}

Class of the Adapter

public class NegociosListAdapter extends BaseAdapter {

private Context context;
private int layout;
private ArrayList<Negocios> listaNegocios;

public NegociosListAdapter(Context context, int layout, ArrayList<Negocios> listaNegocios) {
    this.context = context;
    this.layout = layout;
    this.listaNegocios = listaNegocios;
}

@Override
public int getCount() {
    return listaNegocios.size();
}

@Override
public Object getItem(int position) {
    return listaNegocios.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

private class ViewHolder{
     ImageView imageView;
     TextView Negocio,Categoria,Descripcion;
}

@Override
public View getView(int position, View view, ViewGroup parent) {

    View row = view;
    ViewHolder holder = new ViewHolder();

    if (row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout,null);

        holder.Negocio = row.findViewById(R.id.txtNegocio);
        holder.Categoria =  row.findViewById(R.id.txtCategoria);
        holder.Descripcion =  row.findViewById(R.id.txtDescripcion);
        holder.imageView =  row.findViewById(R.id.imgVnego);

        row.setTag(holder);

    }else{
        holder = (ViewHolder) row.getTag();
    }

    Negocios negocios = listaNegocios.get(position);

    holder.Negocio.setText(negocios.getNegocio());
    holder.Categoria.setText(negocios.getNegocio());
    holder.Descripcion.setText(negocios.getNegocio());

    byte[] negocioImage = negocios.getImage();
    Bitmap bitmap = BitmapFactory.decodeByteArray(negocioImage,0, negocioImage.length);
    holder.imageView.setImageBitmap(bitmap);

    return row;
}

And the class that i use to show the data and add items to a list.add(new Negocios...

public class Negocios_list extends AppCompatActivity {

GridView gridView;
ArrayList<Negocios> list;
NegociosListAdapter adapter = null;

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

    gridView = findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new NegociosListAdapter(this, R.layout.negocios_items, list);
    gridView.setAdapter(adapter);

    //get data
    Cursor cursor = RegistroNegocios.sqLiteHelper.getData("SELECT * FROM NEGOCIOS");

    list.clear();

    while (cursor.moveToNext()){
        int id = cursor.getInt(0);
        String negocio = cursor.getString(1);
        String categoria = cursor.getString(2);
        String descripcion = cursor.getString(3);
        byte[] image = cursor.getBlob(4);

        Log.d("Neg", negocio);
        Log.d("Cat", categoria);
        Log.d("Des", descripcion);

        list.add(new Negocios(id, negocio, categoria, descripcion, image));



    }



    adapter.notifyDataSetChanged();

}
Usman Zafer :

You are dealing with data in Recyclerview incorrectly.

Replace

holder.Negocio.setText(negocios.getNegocio());
holder.Categoria.setText(negocios.getNegocio());
holder.Descripcion.setText(negocios.getNegocio());

With

holder.Negocio.setText(negocios.getNegocio());
holder.Categoria.setText(negocios.getcategoria());
holder.Descripcion.setText(negocios.getdescription());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=385213&siteId=1