2012-03-11 125 views
2

我有一个应用程序,从数据库加载图像到一个滚动视图。我想将其更改为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的适配器?

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

嘿家伙......我见过很多教程,但其中大多数都是简单的类,只是一个简单的列表。

任何人都可以帮助我吗?

任何帮助被赞赏!

+0

看到这个[链接](http://mobile.dzone.com/news/listview-data-sqlitedatabase)希望充分,这将帮助你 – Deepak 2012-03-11 17:42:50

回答

1

您应该从CursorAdapterSimpleCursorAdapter继承并重写getView(或newView和bindView)方法。 这里有一个列表视图和适配器一个很好的教程 - http://www.vogella.de/articles/AndroidListView/article.html

你也应该看罗曼盖伊的(ListView的作者)的讲话在谷歌IO称为The world of ListView

+0

弗拉基米尔你好,感谢你回答。任何示例代码?我是一个小白菜! =/ – 2012-03-11 17:54:50

1

你必须实现自己的适配器,可以延长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; 
    } 
} 

然后在你的活动后,您将获取数据使用该适配器为您的列表:

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); 
+0

你好Slukian,你的代码似乎非常接近我所需要的。非常感谢你。但是,现在,当我运行它时,出现另一个错误:java.lang.IllegalArgumentException:列'id'不存在。我用你的建议更新了上面的代码。你能帮我说我做错了什么吗?非常感谢你! = ^) – 2012-03-12 01:29:10

+0

@CarlosPereira基于'Cursor'的适配器需要在数据库中有一个特殊的列,像这样'_id INTEGER PRIMARY KEY AUTOINCREMENT'。当你查询数据库时,除了你需要的其他列以外,把这个列添加到查询中(尽管你不需要它必须存在的数据或者你得到了这个异常)。同样,当你创建适配器时,第四个参数应该是一个包含数据库中列名称的字符串数组(现在你从光标而不是列名获得字符串) – Luksprog 2012-03-12 07:49:07