2

I have to make a listview from database and i want to put a image that is clickable in my list.I use CursorAdapter for it,but my image does not show on my list.Here is my 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);
        }
    }
}

Everything works fine and how it should be only that my image is not showing.

My 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>

The image appears in my preview but not on my phone.I want to display an image stored on xml, that I can click on.

Frandall
  • 378
  • 1
  • 13
Prox
  • 51
  • 8

1 Answers1

0

I did what @crammeur said, in second LinearLayout i replaced android:layout_width="333dp" with android:layout_width="0dp" and add android:layout_weight="1"

Prox
  • 51
  • 8