【问题标题】:Changing values from Cursor using SimpleCursorAdapter使用 SimpleCursorAdapter 从 Cursor 更改值
【发布时间】:2011-04-06 06:22:30
【问题描述】:

我有一个包含 {Name, Time (UTC format) , Latitude, Longitude} 列的数据库表

我使用带有 SimpleCursorAdapter 的 ListActivity 显示表格。

我希望时间列以人类可读的格式 (13-07-2010 10:40) 而非 UTC 格式 (18190109089) 显示时间。

如何指定时间列中的值需要一些过滤/调整?

可能的解决方案(有问题):

SimpleCursorAdapter 提供方法:

setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);

指定一个类如何将 Cursor 转换为 CharSequence (convertToString(Cursor cursor). 无论如何,我不知道返回 CharSequence 参数应该采用哪种格式!

【问题讨论】:

    标签: android sqlite filtering android-cursor


    【解决方案1】:

    格式化光标值的最简单方法是使用 SimpleCursorAdapter.setViewBinder(..):

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor,
                new String[] { Definition.Item.TITLE, Definition.Item.CREATE_DATE }, new int[] { R.id.title, R.id.createDate});
    
    adapter.setViewBinder(new ViewBinder() {
    
        public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
    
            if (aColumnIndex == 2) {
                    String createDate = aCursor.getString(aColumnIndex);
                    TextView textView = (TextView) aView;
                    textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
                    return true;
             }
    
             return false;
        }
    });
    

    【讨论】:

    • 请记住,aColumnIndexthe column at which the data can be found in the cursor。我用Cursor.getColumnIndex来比较。
    【解决方案2】:

    我也遇到了同样的问题,经过长时间的努力终于找到了答案:)(见下文)

    use setViewText (TextView v, String text)
    

    例如

    SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.somelayout, accountCursor, from, to)
    {
     @Override
     public void setViewText(TextView v, String text) {
     super.setViewText(v, convText(v, text));
    }    
    };
    
    private String convText(TextView v, String text)
    {
    
     switch (v.getId())
     {
     case R.id.date:
                 String formatedText = text;
                 //do format
                return formatedText;
            }
    return text;
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用setViewBinder(),或子类SimpleCursorAdapter 并覆盖bindView()

      【讨论】:

        【解决方案4】:

        您可以在该列上使用SQLite 语法来格式化日期。

        这样就可以了

        SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');
        
        SELECT strftime('%d-%m-%Y %H:%M',timecol,'unixepoch');
        

        【讨论】:

          【解决方案5】:

          浏览这篇旧帖子,注意到我做了类似的事情可能会有所帮助:

          public class FormatCursorAdapter extends SimpleCursorAdapter {
          
          protected int[] mFormats;
          
          public static final int FORMAT_TEXT=0;
          public static final int FORMAT_CURRENCY=1;
          public static final int FORMAT_DATE=2;
          
          public FormatCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int[] formats, int flags) {
              super(context, layout, c, from, to, flags);
              mFormats = formats;
              ViewBinder viewBinder = new ViewBinder() {
                  @Override
                  public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                      int formatType = mFormats[columnIndex-1];
                      switch (formatType) {
                          case FORMAT_CURRENCY:
                              NumberFormat nf = NumberFormat.getCurrencyInstance();
                              nf.setMaximumFractionDigits(2);
                              ((TextView)view).setText(nf.format(cursor.getDouble(columnIndex)));
                              return true;
                          case FORMAT_DATE:
                              DateFormat df = SimpleDateFormat.getDateTimeInstance();
                              ((TextView)view).setText(df.format(new Date(cursor.getLong(columnIndex))));
                              return true;
                      }
                      return false;
                  }
              };
              setViewBinder(viewBinder);
          }
          

          }

          用法:

              // For the cursor adapter, specify which columns go into which views with which format
              String[] fromColumns = {
                      Table.COLUMN_TITLE,
                      Table.COLUMN_AMOUNT,
                      Table.COLUMN_DATE};
              int[] toViews = {
                      R.id.tvTitle,
                      R.id.tvAmount,
                      R.id.tvDate};
              int[] formatViews = {
                      FormatCursorAdapter.FORMAT_TEXT,
                      FormatCursorAdapter.FORMAT_CURRENCY,
                      FormatCursorAdapter.FORMAT_DATE};
          
              mAdapter=new FormatCursorAdapter(getContext(),R.layout.item_operation,cursor,
                      fromOpsColumns,toOpsViews,formatViews,0);
              mListView.setAdapter(mOpsAdapter);
          

          希望这可以帮助任何人!

          【讨论】:

          • 这个问题在 6 年前就已经回答过了……请解释一下你的代码带来了什么新价值?
          猜你喜欢
          • 1970-01-01
          • 2013-04-14
          • 1970-01-01
          • 1970-01-01
          • 2011-08-16
          • 1970-01-01
          • 1970-01-01
          • 2011-12-14
          • 2010-12-31
          相关资源
          最近更新 更多