Как создать пользовательский адаптер курсора для ListView для использования с изображениями и текстом?

StackOverflow https://stackoverflow.com/questions/5300787

Вопрос

Привет, хочу создать пользовательский адаптер курсора, чтобы я мог отобразить изображение с 2 строками текста. У меня были некоторые проблемы с пониманием пользовательских адаптеров курсора, но я не понимаю, как добавить изображение, чтобы быть заполненным из пути в моей базе данных.

Это было полезно?

Решение

Будут,

Я на самом деле реализовал что -то невероятно похожее на то, что вы ищете. Вот моя реализация.

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ItemAdapter extends CursorAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public ItemAdapter(Context context, Cursor c) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context); 
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.items_row, parent, false);
        return v;
    }

    /**
     * @author will
     * 
     * @param   v
     *          The view in which the elements we set up here will be displayed.
     * 
     * @param   context
     *          The running context where this ListView adapter will be active.
     * 
     * @param   c
     *          The Cursor containing the query results we will display.
     */

    @Override
    public void bindView(View v, Context context, Cursor c) {
        String title = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_TITLE));
        String date = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DATE));
        String imagePath = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_IMG));
        int deletion = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DELETION));
        int priority = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_PRIORITY));

        /**
         * Next set the title of the entry.
         */

        TextView title_text = (TextView) v.findViewById(R.id.item_text);
        if (title_text != null) {
            title_text.setText(title);
        }

        /**
         * Set Date
         */

        TextView date_text = (TextView) v.findViewById(R.id.item_date);
        if (date_text != null) {
            date_text.setText(date);
        }       

        /**
         * Decide if we should display the paper clip icon denoting image attachment
         */

        ImageView item_image = (ImageView) v.findViewById(R.id.item_attachment);
        item_image.setVisibility(ImageView.INVISIBLE);
        if (imagePath != null && imagePath.length() != 0 && item_image != null) {
            item_image.setVisibility(ImageView.VISIBLE);
        }

        /**
         * Decide if we should display the deletion indicator
         */
        ImageView del_image = (ImageView) v.findViewById(R.id.item_deletion);
        del_image.setVisibility(ImageView.INVISIBLE);
        if (deletion == 1) {
            del_image.setVisibility(ImageView.VISIBLE);
        }
    }
}

XML просто взволнован ...

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/list_bg">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/item_text"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:lines="1"
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:paddingLeft="2sp"
            android:paddingTop="2sp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:shadowColor="#90909090"
            android:shadowDx="1.0"
            android:shadowDy="1.0"
            android:shadowRadius="1.0"/>

        <TextView android:id="@+id/item_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#FF808080"
            android:paddingLeft="2sp"
            android:paddingTop="2sp"/>
    </LinearLayout>

    <ImageView android:id="@+id/item_deletion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/deletion"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:paddingRight="5sp"/>

    <ImageView android:id="@+id/item_attachment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/attachment"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/item_deletion"/>

</RelativeLayout>

Это отображает два ряда текста и до 2 изображений справа от текста в зависимости от определенных условий.

Я надеюсь, что это может дать вам основу для работы!

Удачи :

Другие советы

Проверьте, что я сделал: Кстати: CheckpointsView.getImageResId() Возвращает действительную ссылку на притяжение

public class CheckpointCursorAdapter extends ResourceCursorAdapter {

    private int layoutType = 0;
    public static final int BLOTTER = 1;
    public static final int DAY = 2;
    public static final int MONTH = 3;
    public static final int OVERVIEW = 4;
    public static final int LAYOUT_ID = R.layout.checkpoint_row;

    public CheckpointCursorAdapter(Context context, int layoutType, Cursor cursor) {
        super(context, LAYOUT_ID, cursor);
        this.layoutType = layoutType;
    }

    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) {
        LayoutInflater li = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return li.inflate(LAYOUT_ID, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView tvListText = (TextView) view.findViewById(R.id.txtCheckPoint);
        Date dt = new Date(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.KEY_CHECKPOINT)));
        switch (layoutType) {
            case BLOTTER:
                tvListText.setText(dt.toLocaleString());
                ((ImageView) view.findViewById(R.id.imgCheckpointInOut))
                        .setImageResource(CheckpointsView.getImageResId(cursor.getCount()
                                - cursor.getPosition()));
                break;
            case DAY:
                tvListText.setText(new SimpleDateFormat("HH:mm:ss").format(dt));
                ((ImageView) view.findViewById(R.id.imgCheckpointInOut))
                        .setImageResource(CheckpointsView.getImageResId(cursor.getPosition() + 1));
                break;
            case MONTH:
                tvListText.setText(new SimpleDateFormat("MMM dd HH:mm:ss").format(dt));
                ((ImageView) view.findViewById(R.id.imgCheckpointInOut))
                        .setImageResource(CheckpointsView.getImageResId(cursor.getPosition() + 1));
                break;
            case OVERVIEW:
                break;
        }
    }

}

И XML на всякий случай

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/txtCheckPoint"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textSize="20dp" android:padding="8dp" android:textStyle="bold">
</TextView>
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:gravity="right" android:layout_weight="1"
    android:layout_marginRight="8px">
    <TextView android:id="@+id/txtTotalHours"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textSize="20dp" android:padding="8dp" android:paddingRight="12dp">
</TextView>
    <TextView android:id="@+id/txtHourBalanceRow"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textSize="12dp" android:padding="8dp">
    </TextView>
    <ImageView android:id="@+id/imgCheckpointInOut"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/black_clock" />
</LinearLayout>

Вы можете проверить весь код по адресу:http://code.google.com/p/droidtimesheet/

Добавление изображений и текста не зависит от CursorAdapter; Вы используете ту же технику для любого вида адаптера. Вы можете создать макет для своего ряда и надуть его внутри своего getView метод А Апидемос Образец проекта, содержащийся в Android SDK android-sdk-PLATFORM/samples/android-XY/ApiDemos Папка, делает то, что вы хотите. Вы можете найти определения строки в list_item_icon_text.xml. Анкет Вот копия содержимого файла (без лицензии).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/icon"
        android:layout_width="48dip"
        android:layout_height="48dip" />

    <TextView android:id="@+id/text"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content" />

</LinearLayout>

Вот отличный руководство что я использовал раньше, чтобы помочь с моими пользовательскими адаптерами списка. Похоже, он делает то же самое, что вы хотите сделать. Учебное пособие показывает, как создать пользовательский адаптер списка с изображением и двумя строками текста.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top