gpt4 book ai didi

android - 坚持使用自定义 Android Listview

转载 作者:行者123 更新时间:2023-11-29 00:40:32 25 4
gpt4 key购买 nike

我有一个应用程序可以将数据库中的图像加载到 ScrollView 中。我想将其更改为 ListView。

这是我现在拥有的:

ListView 中的每一行都应该从这个 xml 文件中扩充:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#FFFFFF">

<ImageButton
android:background="@drawable/roundcorners"
android:id="@+id/figura"
android:src="@drawable/rice"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="84dip"
android:layout_width="98dip"
android:gravity="left">
</ImageButton>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Rice"
android:id="@+id/txtNome"
android:gravity="center"
android:textColor="#000000"
android:textSize="30dip"
android:textStyle="bold"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="rice, food"
android:id="@+id/txtTags"
android:gravity="left"
android:textColor="#000000"
android:textSize="20dip"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>

它基本上是一个图像、它的名称和它的标签。

我刚刚更新了下面的代码以反射(reflect) SLUKIAN 的建议:

public class AnotherCursorAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
public AnotherCursorAdapter(Context context,
int layout,
Cursor c,
String[] from,
int[] to) {
super(context, layout, c, from, to);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// get the views from the row
TextView name = (TextView) view.findViewById(R.id.txtNome);
TextView tags = (TextView) view.findViewById(R.id.txtTags);
ImageView img = (ImageView) view.findViewById(R.id.figura);
//asign the values
name.setText(cursor.getString(4)); // the number will be the index of the column containing the name
tags.setText(cursor.getString(3)); // the same thing
// set the image resource, i don't know how you stored it in the database
}

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

在我的 Activity 中(从数据库)获取游标内的信息后,我正在这样做:

                    AnotherCursorAdapter adapter = new AnotherCursorAdapter(CadItemActivity.this, 
R.layout.imgsinternas,
cursorImagens,
new String[] {cursorImagens.getString(4), cursorImagens.getString(3)/*your columns from the cursor*/},
new int[] { R.id.txtNome, R.id.txtTags });
telaScroll.setAdapter(adapter);

当我运行建议的代码时,出现以下错误:

java.lang.IllegalArgumentException: column 'id' 不存在

Slukian,我做错了什么吗?你能帮帮我吗?

我很难通过上面的代码使用 ListView。我有点迷路了。

如何将光标 (cursorImagens) 设置为 ListView 的适配器?

如何设置图像、名称和标签(这是来 self 的数据库的三个信息)?

大家好...我看过很多教程,但大多数都是简单的,只是一个简单的列表。

有人能帮帮我吗?

感谢任何帮助!

最佳答案

你必须实现你自己的适配器,你可以扩展 SimpleCursorAdapter 因为你有一个带有数据的游标:

public class AnotherCursorAdapter extends SimpleCursorAdapter {

private LayoutInflater inflater;

public AnotherCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// get the views from the row
TextView name = (TextView) view.findViewById(R.id.txtNome);
TextView tags = (TextView) view.findViewById(R.id.txtTags);
ImageView img = (ImageView) view.findViewById(R.id.figura);
//asign the values
name.setText(cursor.getString(0)); // the number will be the index of the column containing the name
tags.setText(cursor.getString(1)); // the same thing
// set the image resource, i don't know how you stored it in the database
}

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

然后在您获取数据后的 Activity 中,将此适配器用于您的列表:

AnotherCursorAdapter adapter = new AnotherCursorAdapter(this, 
R.layout.test_row, cursor, new String[] {/*your columns from the cursor*/},
new int[] { R.id.txtNome, R.id.txtTags });
listView.setAdapter(adapter);

关于android - 坚持使用自定义 Android Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657144/

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