java - 我的 ListView 没有显示我的图像

标签 java android listview imageview

我必须从数据库创建一个 ListView ,我想在我的列表中放置一个可点击的图像。我为此使用了 CursorAdapter,但我的图像没有显示在我的列表中。这是我的 BooksCursorAdapter

public class BooksCursorAdapter  extends CursorAdapter {


    public BooksCursorAdapter(Context context, Cursor c) {
        super(context, c, 0 );
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.books_list_item, parent, false);

    }


    @Override
    public void bindView(View view,final Context context, Cursor cursor) {
        TextView productText = view.findViewById(R.id.productText);
        TextView priceText = view.findViewById(R.id.priceText);
        TextView quantityText = view.findViewById(R.id.quantityText);
        ImageView shopButton = view.findViewById(R.id.shop_button);
        int productColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT);
        int priceColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE);
        final int quantityColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY);
        String bookProduct = cursor.getString(productColumnIndex);
        String bookPrice = cursor.getString(priceColumnIndex);
        final int bookQuantity = cursor.getInt(quantityColumnIndex);
        productText.setText(bookProduct);
        priceText.setText(bookPrice);
        quantityText.setText(Integer.toString(bookQuantity));
        int idColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry._ID);
        final int booksId = cursor.getInt(idColumnIndex);

        shopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri currentUri = ContentUris.withAppendedId(BooksContract.BooksEntry.CONTENT_URI, booksId);
                makeSale(context, bookQuantity, currentUri);
            }
        });
    }

    private void makeSale(Context context,  int bookQuantity, Uri uri) {
        if (bookQuantity == 0) {
            Toast.makeText(context, R.string.no_books, Toast.LENGTH_SHORT).show();
        } else {
            int newQuantity = bookQuantity - 1;
            ContentValues values = new ContentValues();
            values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, newQuantity);
            context.getContentResolver().update(uri, values, null, null);
        }
    }
}

一切正常,只是我的图像没有显示应该如何。

我的 book_list_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="333dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/activity_margin">

        <TextView
            android:id="@+id/productText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#2B3D4D" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/priceText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="#AEB6BD"
                android:padding="5dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/currencyTextList" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/quantityTextList"
                android:padding="5dp"/>

            <TextView
                android:id="@+id/quantityText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="#AEB6BD" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="52dp"
        android:layout_height="match_parent"
        android:src="@drawable/shop_button"
        android:id="@+id/shop_button"/>
</LinearLayout>

图像出现在我的预览中,但不出现在我的手机上。我想显示存储在 xml 中的图像,我可以单击它。

最佳答案

我做了@crammeur 所说的,在第二个 LinearLayout 中,我用 android:layout_width="0dp"替换了 android:layout_width="333dp"并添加了 android:layout_weight="1"

关于java - 我的 ListView 没有显示我的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51715850/

相关文章:

java - Java Vector2d 类中的旋转

Java - 字符串可以用正则表达式分割,但正则表达式匹配返回 false

java - 尝试使用 Kotlin 编写高效的代码来更新背景颜色

javascript - 如何在 Nativescript 上设置 Android 状态栏的样式?

Android Espresso - 网络浏览器

Android:如何在应用过滤器后找出适配器中项目的*原始*位置

android - Android Studio 中带有自定义适配器的自定义 ListView

android - 使用 Firebase 数据库首先显示新项目

java - Android DataPickerDialog onDateChanged 未触发

Java >> 运算符查找字符是否唯一