0

を使用してビュー間にセパレーターを追加しようとしていますCursorAdapter

Cursorセパレータ自体を追加する方法は知っていますが、そのようなタスクに適したものを取得する方法がわかりません。内部に保存されているすべてのデータCotentProviderは、SQLite データベースに裏打ちされています。

セパレータは次のように保存されます:_id, name

アイテムは次のように保管されます:_id, name, ... , separator_id

これまでに見た解決策

  1. すべての区切りを取得します。セパレーターごとに独立してアイテムを取得します。大きなオーバーヘッドが発生すると思います...それとも間違っていますか?
  2. テーブルを結合しitem.separator_id = separator._id、セパレーターのフィールドで結果を並べ替えます。次に、セパレータービューの変更を内部でCursorAdapter探して挿入します。separator_idかなり面倒だと思います。

この問題を解決するより良い方法はありますか?

4

1 に答える 1

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-02-25T08:25:50.220 に答える