我正在从SQLiteDatabase创建列表视图并获取此信息并将其放入列表视图中。数据库工作正常,但是当我将数据库信息放入listview组件时,图像资源不会显示。文本显示正常,但图像不是。如果你能提供帮助我会非常感激!
编辑:已解决
这是ChampCursorAdapter.java:
public class ChampCursorAdapter extends CursorAdapter {
private Cursor cursor;
private Context context;
private LayoutInflater inflater;
private final int layoutID = R.layout.champ_list_item_2;
@SuppressWarnings("deprecation")
public ChampCursorAdapter(Context con, Cursor c) {
super(con, c);
context = con;
cursor = c;
inflater = LayoutInflater.from(con);
}
@Override
public void bindView(View view, Context con, Cursor cur) {
// get resources for view objects
TextView name = (TextView) view.findViewById(R.id.name);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView image = (ImageView) view.findViewById(R.id.imageView);
// set name and title
name.setText(cur.getString(cur.getColumnIndex(Champ.COLUMN_CHAMP)));
title.setText(cur.getString(cur.getColumnIndex(Champ.COLUMN_TITLE)));
// set image
String pic = cur.getString(cur.getColumnIndex(Champ.COLUMN_PIC));
BitmapDrawable picture = new BitmapDrawable(con.getResources(),pic);
image.setImageDrawable(picture);
image.setVisibility(ImageView.VISIBLE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(layoutID, parent, false);
return v;
}
}
这是列表项xml:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left" >
</ImageView>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="5dp"
android:textColor="@color/white"
android:gravity="right"
android:fontFamily="sans-serif-light"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:padding="5dp"
android:textColor="@color/darkwhite"
android:gravity="right"
android:fontFamily="sans-serif-light" >
</TextView>
</LinearLayout>
</LinearLayout>
这是MainActivity.java
public class MainActivity extends Activity {
private Champ champList;
private ChampCursorAdapter dataAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
champList = new Champ(MainActivity.this);
champList.open();
champList.removeChamps();
createChamps();
createListView();
champList.close();
}
// other stuff here
// creates the list view object and sets the adapter
private void createListView() {
Cursor cursor = champList.getChamps();
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
dataAdapter = new ChampCursorAdapter(this, cursor);
ListView listview = (ListView) findViewById(R.id.listView);
listview.setAdapter(dataAdapter);
}