【问题标题】:Android design using ListFragment with SQLite database使用带有 SQLite 数据库的 ListFragment 进行 Android 设计
【发布时间】:2012-05-07 21:27:16
【问题描述】:

使用 SQLite 数据库实现 ListFragment 的最佳方法是什么。目前我已经创建了一个 DBAdapter 来促进打开、关闭和将记录提取到 SimpleCursorAdapter 中。我有我的 MainActivity,它实现了一个带有导航选项卡的 ActionBar,我想为每个选项卡显示不同的 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));
    }
}

这是解决这个问题的方法吗?如果您有任何建议,或者您知道任何好的例子,我将不胜感激。

【问题讨论】:

    标签: android android-fragments sqliteopenhelper android-listfragment


    【解决方案1】:

    我能够让它工作,所以我认为我在正确的轨道上。我必须进行此修改才能使其运行,但它似乎运行良好。

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

    我仍然会对改进此方法的任何建议或方法感兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多