2

SQLiteデータベースを使用してListFragmentを実装するための最良の方法は何ですか。現在、SimpleCursorAdapterへのレコードのオープン、クローズ、およびフェッチを容易にするためにDBAdapterを作成しました。ナビゲーションタブを備えたActionBarを実装するMainActivityがあり、タブごとに異なるListViewを表示したいと思います。

これが私のListFragmentです:

public class MaterialsListFragment extends ListFragment {

public DBAdapter db;   

@Override
public View onCreateView(LayoutInflater inflater, 
ViewGroup container, Bundle savedInstanceState) { 


    return inflater.inflate(R.layout.portrait_material_view, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // create new DBAdapter
    db = new DBAdapter(getActivity());
    db.open();

    Cursor c = db.getAllRecords();

    String[] from = new String[] { DBAdapter.KEY_IDNO, DBAdapter.KEY_MATERIAL };
    int[] to = new int[] { R.id.idno, R.id.materials };        

    // Now create an array adapter and set it to display using our row
    MyListAdapter materials = new MyListAdapter(this, R.layout.list_cell, c, from, to);
    setListAdapter(materials);
 }

これがMyListAdapterコードで、今のところ独自のクラスファイルにあります。

public class MyListAdapter extends SimpleCursorAdapter {

    public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout , cursor, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        // Create the idno textview with background image
        TextView idno = (TextView) view.findViewById(R.id.idno);
        idno.setText(cursor.getSring(3));

        // create the material textview
        TextView materials = (TextView) view.findViewById(R.id.materials);
        materials.setText(cursor.getString(1));
    }
}

これはこれについて行く方法ですか?私はあなたが持っているかもしれないどんな提案でも、あるいはあなたが何か良い例を知っているなら、感謝します。

4

1 に答える 1

2

これを機能させることができたので、私は正しい方向に進んでいると思います。実行するにはこの変更を加える必要がありましたが、うまく機能しているようです。

      MyListAdapter materials = new MyListAdapter(getActivity(), R.layout.list_cell, c, from, to);

I would still be interested in any suggestions or ways to improve this approach.

于 2012-05-08T17:22:26.503 に答える