gpt4 book ai didi

java - 如何为 ListView 创建自定义光标适配器以用于图像和文本?

转载 作者:IT老高 更新时间:2023-10-28 23:30:09 25 4
gpt4 key购买 nike

您好,我想创建一个自定义光标适配器,这样我就可以显示带有 2 行文本的图像。我在理解自定义光标适配器时遇到了一些麻烦,但我不明白如何添加要从数据库中的路径填充的 ImageView 。

最佳答案

会,

我实际上已经实现了一些与您正在寻找的非常相似的东西。这是我的实现。

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 个图像。

我希望这能给你一个工作的基础!

祝你好运:]

关于java - 如何为 ListView 创建自定义光标适配器以用于图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com