custom listview repeating last insert inserted

0

To learn, I'm trying to create a custom listview, getting values from the database, I 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 populated with only the last record. It creates rows in the amount and every time I save a value, all rows in the listview are changed, where am I wrong?

    
asked by anonymous 20.09.2015 / 03:13

1 answer

1

I have not yet figured out the problem, but I believe you have solved the problem by temporarily altering the MetasAdapter with the following code and making the Attributes of the Goals 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);
}
    
21.09.2015 / 23:11