我正在尝试使用自定义 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 ......但它不起作用。