1

我正在从 SQLiteDatabase 创建一个列表视图,并将此信息放入列表视图中。数据库工作正常,但是当我将数据库信息放入列表视图组件时,图像资源没有显示。文字显示正常,但图像不是。如果您能提供帮助,我将不胜感激!

编辑:已解决

这是 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);   
}
4

0 回答 0