listview Adapter repeats values saved in id

Asked

Viewed 96 times

0

I am trying to bring the values of the database and click on a listview, I already bring from the database an object with the data (a list of "modules"), and I tried to pass the id of the corresponding line in the database via Intent to the other Activity to load the values corresponding to the id that was passed, the problem is that Adapter loads the database data by placing each entry right in the listview, but id it only passes to another Activity like Intent with value 1, regardless of which is the true id.

follows the code passage:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_adapter,null);
        holder = new ViewHolder();
        holder.tituloView = (TextView)convertView.findViewById(R.id.titulo2);
        holder.progressBar =
(ProgressBar)convertView.findViewById(R.id.progress);
        holder.button = (Button)convertView.findViewById(R.id.buttonModulo);

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

    modulos = new Modulos();
    modulos = modulosList.get(position);

    Log.i("idModulo:"," "+modulos.getId());
    String titulo = modulos.getTitulo();
    holder.tituloView.setText(titulo);

    baixarImagem(convertView,modulos,context,holder);

    holder.progressBar.setMax(8);



    holder.progressBar.setProgress(4);

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Bundle bundle = new Bundle();
            bundle.putString("id",modulos.getId());
            Intent intent = new Intent(context, ListaAulasActivity.class);
            intent.putExtras(bundle);
            intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            Log.i("idModuloEnviado: ",""+modulos.getId());
            context.startActivity(intent);
        }
    });

    convertView.setTag(holder);
    return convertView;
}

1 answer

1


To call the attributes of each item in the list, use Onitemclicklistener in Listview.

seuListView.setOnItemClickListener(onCliqueItemLista());

Event:

private AdapterView.OnItemClickListener onCliqueItemLista() {
    return new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            idModulo = modulosList.get(position).getId();
            Bundle bundle = new Bundle();
            bundle.putString("id",modulos.getId());
            Intent intent = new Intent(context, ListaAulasActivity.class);
            intent.putExtras(bundle);
            intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            Log.i("idModuloEnviado: ",""+modulos.getId());
            context.startActivity(intent);
        }
    };
}
  • It worked!!! You’re a genius man! I’ve been stuck in this for a long time...?

  • I think the problem is in your Viewholder, because it is used with Recyclerview. It looks like you have created an Adapter for your Listview, which is another way to do it, but in that case you do not need Viewholder, remove it and take your convertView components. The button click event should be handled within the class of your custom Adapter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.