Set date inside an Arraylist using Cursor

Asked

Viewed 337 times

3

In a App finance the cursor has no type of getData or getCalendar.

What to wear 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;
}

}

  • both fields date and includedIn are from the Calendar class. I will need to change?

  • You said what you want, but it’s not clear what you know and what you have.

  • Where is the Adapter?

  • updated with Adapter @Rafael

  • 2

    Allan, is recording the date as String in the bank? In what format? Inside the getLista, why don’t you use a DateFormat to transform the date of String for Date? You could also record date as Long (milliseconds) and use the Calendar.getInstance().setTimeInMillis(...).

  • I am recording direct without formatting, because I did it to test... How exactly would this conversion from String to Date? Or I change from String to Long and use Calendar to set in milliseconds, could you explain? @Wakim

  • When I get home I can assemble a detailed answer, but at first the column that keeps the date in Sqlite could be a long and you would use the Calendar.getTimeInMillis to take the timestamp (Calendar -> long) and write. To do the reverse just use the Calendar.getInstance().setTimeInMillis (long -> Calendar) for popular your object.

  • I am waiting for your detailed answer, as I am beginner on android I do not understand much what is to do, I thank you from now on. @Wakim

Show 3 more comments

1 answer

1

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.

Browser other questions tagged

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