首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我的ListView没有显示我的图像

我的ListView没有显示我的图像
EN

Stack Overflow用户
提问于 2018-08-07 05:25:41
回答 1查看 55关注 0票数 2

我必须从数据库创建一个列表视图,并且我想将一个可单击的图像放入我的列表中。我使用CursorAdapter进行此操作,但我的图像不显示在我的list.Here上是我的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上的图像,我可以单击该图像。

EN

回答 1

Stack Overflow用户

发布于 2018-08-07 06:42:16

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51715850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档