custom listview repeating last inserted

Asked

Viewed 175 times

0

to learn, I’m trying to create a custom listview, taking values from the bank, created this Metasacvivity where I call the list method

public void listarMetas() {


    ArrayList<Meta> values = new ArrayList<>();

    Cursor cursor = meuBanco.query("metas", null,null,null,null,null,null);

    while (cursor.moveToNext()){
        int idMetas = cursor.getInt(cursor.getColumnIndex("id"));
        String nomeMeta = cursor.getString(cursor.getColumnIndex("nome"));
        String tempo = cursor.getString(cursor.getColumnIndex("hora"));

        Meta item = new Meta(idMetas,nomeMeta,tempo);
        values.add(new Meta(idMetas,nomeMeta,tempo));
        System.out.println("MOVE------:");
        System.out.println("ArrayList :   " + item.getId() + "======" + item.getNome() + "----" + item.getTempo());


        System.out.println("ITEM+++++++++++");

        lv_metas = (ListView)findViewById(R.id.lv_metas);

        adapter1 = new MetasAdapter(MetasActivity.this,values);

        lv_metas.setAdapter(adapter1);
    }

And I created Metasadapter:

Meta item = this.getItem(position);
    MetasHolder holder = null;

    if(convertView == null){
        convertView = LayoutInflater.from(this.context).inflate(R.layout.item_list, null);

        holder = new MetasHolder();
        holder.nomeMeta = (TextView)convertView.findViewById(R.id.tv_nomeCategoria);
        holder.tempo = (TextView)convertView.findViewById(R.id.tv_tempo);

        convertView.setTag(holder);
    } else {
        holder = (MetasHolder)convertView.getTag();
    }

    Meta metas = data.get(position);
    holder.nomeMeta.setText(item.getNome());
    System.out.println("metas.getNome()======" + item.getNome());
    holder.tempo.setText(item.getTempo());
    System.out.println("metas.getTempo()======" + item.getTempo());

    return convertView;

The problem is that the custom listview is only filled with the last record. It creates lines in the amount and every time I save a value, all the lines in the listview are changed, where I’m missing?

1 answer

1

I haven’t figured out the problem yet, but I believe I have solved the problem by temporarily changing Metasadapter with the following code and making the Goals attributes public.

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    Meta meta = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list, parent, false);
    }

    TextView tv_id = (TextView)convertView.findViewById(R.id.tv_id);
    TextView tv_nomeCategoria = (TextView)convertView.findViewById(R.id.tv_nomeCategoria);
    TextView tv_tempo = (TextView)convertView.findViewById(R.id.tv_tempo);
    tv_id.setText(String.valueOf(meta.id));
    tv_nomeCategoria.setText(meta.nome);
    tv_tempo.setText(meta.tempo);
}

Browser other questions tagged

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