【问题标题】:my ListView is not showing my image我的 ListView 没有显示我的图像
【发布时间】:2019-01-13 21:34:03
【问题描述】:

我必须从数据库中创建一个列表视图,并且我想在我的列表中放置一个可点击的图像。我使用 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 中的图像,我可以点击它。

【问题讨论】:

  • 您没有调用shopButton.setImageResource(R.drawable.image)(也可以直接在 XML 上完成),否则发布您的 XML 以查看它是否匹配出现在屏幕上的约束。
  • 你能更详细地说明你想要什么,因为这个问题对我来说是模棱两可的。
  • 我知道图像不是显示,而是您想要显示的图像而不是存储在数据库中的图像。或存储在 xml 中的图像
  • 我更新了我的消息@crammeur
  • 成功了!!!谢谢! @crammeur

标签: java android listview imageview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多