从一个SQLite数据库移植ListView不工作

问题描述 投票:0回答:1

我试图填充使用从预加载的SQLite数据库数据一个ListView。我没有得到任何错误,但没有数据要么出现在列表视图和我不能肯定这是为什么。

我老老实实同样的事情读到的一切我所能,10-12各个线程之间的位置,YouTube视频,以教程。我怀疑我是在正确的轨道上,但我不知道到底是什么我已经错过了。

我如何填充ListView控件:

    // Populate the ListView
    ArrayList<String> theList = new ArrayList<>();
    ArrayAdapter listAdapter = new ArrayAdapter<>(this,R.layout.row_item,R.id.bonusListCode,theList);
    Cursor data = appDBHelper.getAppDataListContents();
    if (data.getCount() == 0) {
        Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
    } else {
        for(int i=0;i<data.getCount();i++) {
            data.moveToPosition(i);
            theList.add(data.getString(0));
        }
        listView.setAdapter(listAdapter);
    }

我做了一个适配器,但我不任何地方调用它,我不能完全确定1)如何正确地调用它; 2)如果是我应该做的方式。

appDataDBAdapter:

    public class AppDataDBAdapter extends CursorAdapter {
    public AppDataDBAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Find fields to populate in inflated template
        TextView bonusCode = view.findViewById(R.id.bonusListCode);
        TextView bonusName = view.findViewById(R.id.bonusListName);
        TextView bonusCity = view.findViewById(R.id.bonusListCity);
        TextView bonusState = view.findViewById(R.id.bonusListState);
        TextView bonusCategory = view.findViewById(R.id.bonusListCategory);

        // Extract properties from cursor
        String sCode = cursor.getString(cursor.getColumnIndexOrThrow("sCode"));
        String sName = cursor.getString(cursor.getColumnIndexOrThrow("sName"));
        String sCity = cursor.getString(cursor.getColumnIndexOrThrow("sCity"));
        String sState = cursor.getString(cursor.getColumnIndexOrThrow("sState"));
        String sCategory = cursor.getString(cursor.getColumnIndexOrThrow("sCategory"));

        // Populate fields with extracted properties
        bonusCode.setText(sCode);
        bonusName.setText(sName);
        bonusCity.setText(sCity);
        bonusState.setText(sState);
        bonusCategory.setText(sCategory);
    }
}
android
1个回答
0
投票
//populate an ArrayList<String> from the database and then view it
 ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
    ArrayList<String> theList = new ArrayList<>();
    Cursor data = appDBHelper.getAppDataListContents();
    if (data.getCount() == 0) {
        Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
    } else {
        for(int i=0;i<data.count;i++) {
            data.moveToPosition(i);
            theList.add(data.getString(1));
          }
    listView.setAdapter(listAdapter);
    }
© www.soinside.com 2019 - 2024. All rights reserved.