1

I am wondering about using setViewBinder. When I try to this, all columns change same date even it has data. How can I edit each column, which is related date, not every column? I think that I have to modify the setViewBinder part. Could you help me?

protected void onActivityResult(int requestCode, int resultCode,Intent intent) 
{
   super.onActivityResult(requestCode, resultCode, intent);
   fillData();
}

private void fillData()
{
   cursor = dbHelper.fetchAllItems();
   startManagingCursor(cursor);
   String[] from = new String[] {FridgeDbAdapter.KEY_NAME,    
                                FridgeDbAdapter.KEY_EXPIRED_DATE};
   int [] to = new int[] {R.id.fridge_name_txt, R.id.fridge_expired_txt};
   SimpleCursorAdapter data_row = new SimpleCursorAdapter(this, R.layout.fridge_row,
   cursor, from, to);

   //It makes crash the list which is making all same date..
   data_row.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

   //How can I bind the data with only specific date column?
   public boolean setViewValue(View view, Cursor cursor, int column) 
   {
      TextView tv = (TextView) view;
      String date = cursor.getString(cursor.getColumnIndex("expired_date"));
  SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
  Calendar cal = Calendar.getInstance();
  date = df.format(cal.getTimeInMillis());
  tv.setText(date);
  return true;
  }
   });*/
   setListAdapter(data_row);
}

***** I edited setViewBinder.. however, it is shown the date every column, not specific column. And date is shown 01-01-1970, not like 12/11/2011.. How can I do?**

data_row.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

public boolean setViewValue(View view, Cursor cursor, int column) 
{
if(column>=0)
   {
TextView tv = (TextView) view;
    long date=cursor.getLong(cursor.getColumnIndex("expired_date"));
Calendar cal = Calendar.getInstance();
cal.clear();
    cal.setTimeInMillis(date);
    String date_format = String.format("%1$td-%1$tm-%1$tY", cal);
tv.setText(date_format);
return true;
    }
  return false;
  }

});

Original List

Result after setViewBinder

wholee1
  • 1,491
  • 13
  • 34
  • 51

3 Answers3

0

About date types you can find there: Android database date type setViewBinder

Community
  • 1
  • 1
tadvas
  • 131
  • 1
  • 13
0

it is caused by this code

String date = cursor.getString(cursor.getColumnIndex("expired_date"));

you should do smthing like this

String date = cursor.getString(column);

next you have to check if column == cursor.getColumnIndex("expired_date") and do formatting only if it's true in other case you sould setup tv with data from date string

Selvin
  • 6,598
  • 3
  • 37
  • 43
0

The error in your code is that you never are parsing the date you are getting from the database.

Try

    public boolean setViewValue(View view, Cursor cursor, int column) 
   {
      TextView tv = (TextView) view;
      String date = cursor.getString(cursor.getColumnIndex("expired_date"));
      tv.setText(date);
      return true;
  }

If the date from the DB isn't formatted how you want it to be formatted then:

public boolean setViewValue(View view, Cursor cursor, int column) 
   {
      TextView tv = (TextView) view;
      String dbDate = cursor.getString(cursor.getColumnIndex("expired_date"));
      SimpleDateFormat dbDateFormat = new SimpleDateFormat("****db date format***");
      SimpleDateFormat outputFormat = new SimpleDateFormat("****output format***");
      Date date = dbDateFormat.parse(dbDate);
      tv.setText(outputFormat.format(date));
      return true;
  }
Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • When I use your coding, it happened error here, Date date = dbDateFormat.parse(dbDate);. they said use try catch, and date has to initialized. then I put Date date == null, and I used try catch. However when I debugging, tv.setText(outputFormat.format(date)); it is error again.. how can I do? – wholee1 Nov 24 '11 at 10:07