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?
– Allan Chrystian
You said what you want, but it’s not clear what you know and what you have.
– Lollipop
Where is the Adapter?
– Lollipop
updated with Adapter @Rafael
– Allan Chrystian
Allan, is recording the date as
Stringin the bank? In what format? Inside thegetLista, why don’t you use aDateFormatto transform the date ofStringforDate? You could also record date asLong(milliseconds) and use theCalendar.getInstance().setTimeInMillis(...).– Wakim
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
– Allan Chrystian
When I get home I can assemble a detailed answer, but at first the column that keeps the date in Sqlite could be a
longand you would use theCalendar.getTimeInMillisto take the timestamp (Calendar -> long) and write. To do the reverse just use theCalendar.getInstance().setTimeInMillis(long -> Calendar) for popular your object.– Wakim
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
– Allan Chrystian