Тип даты базы данных Android setViewBinder - PullRequest
1 голос
/ 23 ноября 2011

Мне интересно использовать setViewBinder. Когда я пытаюсь это сделать, все столбцы изменяют одну и ту же дату, даже если она содержит данные. Как я могу редактировать каждый столбец, связанный с датой, а не каждый столбец? Я думаю, что я должен изменить часть setViewBinder. Не могли бы вы помочь мне?

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);
}

***** Я отредактировал setViewBinder .. однако там показывается дата каждого столбца, а не конкретного столбца. И дата показывается 01-01-1970, а не как 12/11/2011 .. Как я могу это сделать? **

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

Ответы [ 3 ]

0 голосов
/ 23 ноября 2011

Ошибка в вашем коде заключается в том, что вы никогда не анализируете дату, полученную из базы данных.

Попробуйте

    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;
  }

Если дата из БД не отформатирована, какВы хотите, чтобы это было отформатировано тогда:

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;
  }
0 голосов
/ 25 октября 2013

О типах дат вы можете найти здесь: Тип базы данных Android setViewBinder

0 голосов
/ 23 ноября 2011

вызвано этим кодом

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

ты должен сделать что-то вроде этого

String date = cursor.getString(column);

Далее вы должны проверить, если column == cursor.getColumnIndex ("expired_date") и делать форматирование, только если это правда, в противном случае вы должны настроить телевизор с данными из строки даты

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...