自定义CursorAdaptor什么都没有显示

时间:2013-05-07 16:36:57

标签: android listview cursor

我正在尝试使用自定义CursorAdaptor从y sqlite db获取数据以向右侧显示图像一些数据,但到目前为止,我得到的只是一个空白屏幕或旋转加载圆圈而没有结果

这是我的片段活动:

public class FragMvp extends SherlockListFragment {

    private DataBaseManager dataBase;
    private MyCursorAdapter mAdapter;

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

        // creates and open the database so we can use it
        dataBase = DataBaseManager.instance();

        String query = "SELECT mo._id,mo.sprite,mo.iName FROM Mobs AS mo WHERE mo.MobType = 2 ORDER BY mo.iName ASC;";

        Cursor cursor = dataBase.select(query);

        if (cursor != null) {
            mAdapter = new MyCursorAdapter(ApplicationContextProvider.getContext(), cursor, false);
            setListAdapter(mAdapter);

            cursor.close();
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data
    }
}

查询在我的数据库管理器上完美运行,如果我使用ArrayAdapter,它可以工作并显示数据。所以我猜这个问题与数据库无关。

然后,这是CursorAdapter:

public class MyCursorAdapter extends CursorAdapter {

    private final LayoutInflater mInflater;

    public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.my_adapter, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        String mobSprite = cursor.getString(cursor.getColumnIndex("sprite"));
        String mobName = cursor.getString(cursor.getColumnIndex("iName"));
        int lvl = cursor.getInt(cursor.getColumnIndex("LVL"));
        String hp = cursor.getString(cursor.getColumnIndex("HP"));

        ImageView imageView = (ImageView) view.findViewById(R.id.mobSprite);
        TextView mobNameView = (TextView) view.findViewById(R.id.mobName);
        TextView levelView = (TextView) view.findViewById(R.id.lvl);
        TextView hpView = (TextView) view.findViewById(R.id.hp);

        int resID = context.getResources().getIdentifier(mobSprite, "drawble", context.getPackageName());
        imageView.setImageResource(resID);
        mobNameView.setText(mobName);
        levelView.setText(lvl);
        hpView.setText(hp);
    }
}

以及以防万一的XML。它尚未完成,但首先我希望看到一些结果并看看它们的样子。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/mobSprite"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp" >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/mobName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginTop="4dp"
            android:text="@+id/mobName"
            android:textSize="20sp" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/lvlString"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:text="@string/lvl"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lvl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/lvlString"
                android:text="@+id/lvl"
                android:textSize="12sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/hpString"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:text="@string/hp"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/hp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/hpString"
                android:text="@+id/hp"
                android:textSize="12sp" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

我不知道问题出在哪里,我已经找到了无数的教程,而且总是一样的。做CursorAdapter,传递光标和上下文并设置listAdapter ......但它不起作用。

1 个答案:

答案 0 :(得分:1)

问题出在这里。

Cursor cursor = dataBase.select(query);
if (cursor != null) {
    mAdapter = new MyCursorAdapter(ApplicationContextProvider.getContext(), cursor, false);
    setListAdapter(mAdapter);

    cursor.close(); <========
}

您正在过早关闭光标。如果你在这里关闭它,适配器将没有结果。 CursorAdapter将在光标完成或将光标更改为新光标时关闭光标。

希望这有帮助。