【问题标题】:CursorAdapter with separators - how to get proper cursor?带分隔符的 CursorAdapter - 如何获得正确的光标?
【发布时间】:2013-02-13 14:45:56
【问题描述】:

我正在尝试使用 CursorAdapter 在视图之间添加分隔符。

我知道如何自己添加分隔符,但我不确定如何获得合适的Cursor for 这样的任务。存储在CotentProvider 中的所有数据都由 SQLite 数据库支持。

分隔符存储为:_id, name

项目存储为:_id, name, ... , separator_id

我目前看到的解决方案

  1. 获取所有分隔符。对于每个分隔符独立地检索项目。我认为会有很大的开销......还是我错了?
  2. item.separator_id = separator._id 上加入表格并按分隔符的某些字段排序结果。然后,在CursorAdapter 中查找separator_id 的更改并插入分隔符视图。我认为相当混乱。

有没有更好的方法来解决这个问题?

【问题讨论】:

    标签: android android-sqlite android-cursoradapter


    【解决方案1】:

    最好使用分隔符枚举。但在你的情况下

    public Cursor getItemList(){
         return db.rawQuery("SELECT * FROM Items NATURAL JOIN Separators ON Item.separator_id = Separators._id");
        }
    

    创建您的自定义 CursorAdapter 并以您的方式在 bindView 方法中对其进行管理。 带有视图分隔符的布局应包含 ImageView 作为分隔符。

    public class MyCursorAdapter extends CursorAdapter {
          LayoutInflater inflater;
          public MyCursorAdapter(Context context, Cursor c) {
            super(context, c);
            inflater = LayoutInflater.from(context);
          }
    

    @Override
          public void bindView(View view, Context context, Cursor cursor) {
            //cursor is already setted to requared position, just get your column
            TextView tv1 = (TextView)view.findViewById(R.id.textView1);
            TextView tv2 = (TextView)view.findViewById(R.id.textView2);
            tv1.setText(cursor.getString(1));
            tv2.setText(cursor.getString(2));
            ImageView myImageView = (ImageView)fimdViewById(R.id.my_image_view);
            if(cursor.getString(5) == "line"){
              myImageView = getResources().getIdentifier("yourpackagename:drawable/line.png");
            }else if(cursor.getString(5) == "wave"){
              myImageView = getResources().getIdentifier("yourpackagename:drawable/wave.png");
            ...
          }
    

      @Override
              public View newView(Context context, Cursor cursor, ViewGroup parent) {
                //here is view for each raws
                return inflater.inflate(R.layout.my_raw_view, parent, false);
              }
            }
    

    【讨论】:

      猜你喜欢
      • 2013-08-14
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      相关资源
      最近更新 更多