我有一个应用程序,从数据库加载图像到一个滚动视图。我想将其更改为ListView。卡住自定义Android列表视图
这是我现在有:
在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>
这基本上是一个图像,它的名字和它的标签。
我刚刚更新下面的代码,以反映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;
}
}
让我在活动光标(从数据库)在我的信息后,我这样做:
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:列'id'不存在
Slukian,我做错了什么?你能帮我吗?
我真的很难用上面的代码使用ListView。我有点失落。
如何将我的光标(cursorImagens)设置为ListView的适配器?
如何设置图像,名称和标签(这些是来自我的数据库的三个信息)?
嘿家伙......我见过很多教程,但其中大多数都是简单的类,只是一个简单的列表。
任何人都可以帮助我吗?
任何帮助被赞赏!
看到这个[链接](http://mobile.dzone.com/news/listview-data-sqlitedatabase)希望充分,这将帮助你 – Deepak 2012-03-11 17:42:50