How to take Sqlite Database Date on Android and format in dd-mm-yyyy using viewHolder?

Asked

Viewed 259 times

1

public class ContatoArrayAdapter extends ArrayAdapter<Contato> {

    private int resource = 0;
    private LayoutInflater inflater;
    private Context context;

    public ContatoArrayAdapter(Context context, int resource) {
        super(context, resource);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.resource = resource;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;
        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();

            view = inflater.inflate(resource, parent, false);

            viewHolder.txtCor = (ImageView) view.findViewById(R.id.txtCor);
            viewHolder.txtNome = (TextView) view.findViewById(R.id.txtNome);
            viewHolder.txtTelefone = (TextView) view.findViewById(R.id.txtTelefone);

            view.setTag(viewHolder);

            convertView = view;

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            view = convertView;
        }

        Contato contato = getItem(position);

//        if (contato.getNome().toUpperCase().startsWith("A"))
//            viewHolder.txtCor.setBackgroundColor( context.getResources().getColor(R.color.azul) );
//        else
//            if (contato.getNome().toUpperCase().startsWith("B"))
//                viewHolder.txtCor.setBackgroundColor(context.getResources().getColor(R.color.vermelho));
//            else
//                viewHolder.txtCor.setBackgroundColor(context.getResources().getColor(R.color.color1));
        viewHolder.txtNome.setText(contato.getNome());
            viewHolder.txtTelefone.setText(contato.getDatasEspeciais().toString());

        return view;

    }

    static class ViewHolder {

        ImageView txtCor;
        TextView txtNome;
        TextView txtTelefone;
    }
}

inserir a descrição da imagem aqui

2 answers

0

You can use SimpleDateFormat. Take an example:

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
try {
    Date date = formatter.parse("Friday, Jun 7, 2013 12:10:56 PM");
    Log.wtf("TAG", new SimpleDateFormat("dd-MM-yyyy").format(date));

} catch (ParseException e) {
    e.printStackTrace();
}

To adapt to your case, simply replace the "Friday, Jun 7, 2013 12:10:56 PM" for contato.getDatasEspeciais().toString(), however it is necessary to check exactly what is the format of your date that is not fully visible. See some examples below that I found in this answer:

"yyyy.MM.dd G 'at' HH:mm:ss z"  2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"  Wed, Jul 4, '01
"h:mm a"    12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"  02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"    Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"   2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"   2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u"  2001-W27-3

0

Browser other questions tagged

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