【问题标题】:CursorAdapter bindView and newView not firingCursorAdapter bindView 和 newView 未触发
【发布时间】:2014-01-30 17:46:31
【问题描述】:

所以我试图从数据库中获取我的信息并将其返回到我已经创建的自定义格式的 ListView 中。

从我的 MainActivity.java 我正在启动我的 history.java 文件,该文件应该显示所有数据!

history.java如下所示

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Light.ttf");

        ListView listContent = (ListView) findViewById(android.R.id.list);
        TextView history = (TextView) findViewById(R.id.history);
        history.setTypeface(tf);

        MySQLiteHelper db = new MySQLiteHelper(this);

        Cursor cursor = db.getAllLogs();
        Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
        GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);
        Log.d("history.java", "GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);");
        listContent.setAdapter(adapter);
        Log.d("history.java", "setAdapter(adapter);");
        cursor.close();
    }

}

(在日志中我看到这里的一切都着火了——很好!)

这是我用来获取所有数据的 MySQLiteHelper.java

public Cursor getAllLogs() {
        List<String> List = new ArrayList<String>();

        String selectQuery = "SELECT * FROM " + TABLE_GASLOG;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        Log.d("MySQLiteHelper.java", "through getAllLogs()");
        return cursor;
        }

(我也看到了那里的日志 - 很好!)

这是我的 GasCursorAdapter.java

public class GasCursorAdapter extends CursorAdapter {


    //private LayoutInflater mInflater = null;

    public GasCursorAdapter(Context context, Cursor c, int flags){
        super(context, c, flags);
        //mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("inGasCursorAdapter", "GasCursorAdapter");
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.card_background, parent, false);
        Log.d("inGasCursorAdapter", "newView");
        return retView;
        //return mInflater.inflate(R.layout.card_background,  parent, false);
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor){
        TextView cardDate = (TextView) v.findViewById(R.id.cardDate);
        int date = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_DATE);
        cardDate.setText(date);
        TextView cardGallons = (TextView) v.findViewById(R.id.cardGallons);
        cardGallons.setText((int) cursor.getDouble(cursor.getColumnIndex(cursor.getColumnName(3))));
        Log.d("inGasCursorAdapter", "through bindView");
    }

}

我看到它通过了第一种方法,但是 newView 和 bindView 方法永远不会触发。我想正因为如此,我的 history.java 页面只是空白,没有数据显示:(

关于我可能有什么问题或遗漏的任何想法?

提前致谢!

【问题讨论】:

  • 您检查过您的Cursor 中有多少行吗?如果为空,则不会显示任何内容。
  • 我为游标做了一个日志,我得到以下信息:01-30 12:51:29.533: D/cursor(21492): android.database.sqlite.SQLiteCursor@41f450d0 不确定是否是检查与否的正确方法?
  • 不,cursor.getCount() 为您提供行数。
  • 你也在打开你的数据库。你声明它但从不打电话给db.open()。运行 cursor.getCount() 将显示为零,因为它没有写入数据库。

标签: java android android-cursoradapter


【解决方案1】:

不要将包含游标的数据传递给GasCursorAdapter 构造函数,而是在将适配器附加到ListView 后尝试使用适配器的swapCursor 方法。无论如何,更好的方法是使用 CursorLoader 类,因为它提供了从 UI 线程外部的数据源加载。是的,您应该为此实现自己的ContentProvider,但您可以扩展AsyncTaskLoader 类并使用MySQLiteHelper 实现直接检索数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多