java – Set data inside an ArrayList using Cursor

Question:

In a Finance App the cursor has no getData or getCalendar .

What to use instead?

    public List<Despesa> getLista() {
    Cursor c = getWritableDatabase().query(TABELA, COLUNAS, null, null, null, null, null);
    List<Despesa> lista = new ArrayList<Despesa>();

    while (c.moveToNext()) {
        Despesa despesa = new Despesa();
        despesa.setId(c.getInt(0));
        despesa.setValor(c.getString(1));
        despesa.setData(c.getString(2));
        despesa.setDescricao(c.getString(3));
        despesa.setPago(c.getString(4).equalsIgnoreCase("TRUE"));
        despesa.setIdGrupo(c.getInt(5));
        despesa.setIdSubgrupo(c.getInt(6));
        despesa.setIncluidoEm(c.getString(7));
        despesa.setSincronizado(c.getString(8).equalsIgnoreCase("TRUE"));

        lista.add(despesa);
    }
    c.close();

    return lista;
}

Adapter

public class DespesaAdapter extends BaseAdapter {
private Context context;
private List<Despesa> lista;
ListView listView;

public DespesaAdapter(Context context, List<Despesa> lista){
    this.context = context;
    this.lista = lista;
}


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

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

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

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;

    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final LinearLayout layout = (LinearLayout)
            inflater.inflate(R.layout.despesas,null);

    TextView tvNome = (TextView)
            layout.findViewById(R.id.tvDesp);
    tvNome.setText(lista.get(position).getDescricao());


    TextView tvValor = (TextView)
            layout.findViewById(R.id.tvVal);
    tvValor.setText(lista.get(position).getValor());

    ImageButton btEditar = (ImageButton)
            layout.findViewById(R.id.btEdit);
    btEditar.setOnClickListener(new ImageButton.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, EditaDespesaActivity.class);
            intent.putExtra("valor", lista.get(auxPosition).getValor());
            intent.putExtra("data", lista.get(auxPosition).getData());
            intent.putExtra("descricao", lista.get(auxPosition).getDescricao());
            context.startActivity(intent);
        }
    });

    ImageButton btDelete = (ImageButton)
            layout.findViewById(R.id.btDelete);
    btDelete.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View view) {
            DespesaDAO despesa = new DespesaDAO(context);
            despesa.deletar(lista.get(auxPosition));

            layout.setVisibility(View.GONE);
        }
    });



    return layout;
}

}

Answer:

The cursor has the method "getColumnIndex"

cursor.getColumnIndex(<nome da coluna>)

Usually what you do is:

cursor.getString(cursor.getColumnIndex(<nome da coluna>) , can also be getInt() , getLong() , etc.

Scroll to Top
AllEscort