0

我正在android中开发一个简单的应用程序。我有一个类别列表(现在),我想将其显示为列表,然后(但这很远)单击,开始另一个具有某些功能的活动......好吧,情况:

我的 single_list_item.xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <!-- Name Label -->
  <TextView android:id="@+id/category_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"           
            android:textStyle="bold"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:textColor="#43bd00"/>
  <!-- Description Label -->

</LinearLayout>

在主要活动中,我在这里调用适配器和布局:

List<categorie> values = datasource.getAllCategorie();

        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this,
            R.layout.single_list_item, values);
        setListAdapter(adapter);

在数据源中,我在这里声明 getAllCategorie:

public List<categorie> getAllCategorie() {
            List<categorie> categorie = new ArrayList<categorie>();

            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, null, null, null, null, null);

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              categorie categoria = cursorToCategorie(cursor);
              categorie.add(categoria);
              cursor.moveToNext();
            }
            // Make sure to close the cursor
            cursor.close();
            return categorie;
          }

最后是 categorie.class :

public class categorie {
      private long id;
      private String nome;
      private long preferita;

      public long getId() {
        return id;
      }

      public void setId(long id) {
        this.id = id;
      }

      public String getNome() {
        return nome;
      }

      public void setNome(String nome) {
        this.nome = nome;
      }



      public long getPreferita() {
        return preferita;
    }

    public void setPreferita(long preferita) {
        this.preferita = preferita;
    }

    // Will be used by the ArrayAdapter in the ListView
      @Override
      public String toString() {
        return nome;
      }
    } 

目前,如果我运行该应用程序,它会在启动列表视图时冻结,使其完全为空。我想指定每个类别元素的放置位置,例如名称、id 或者是否“喜欢”(在 categorie.class 中获取并设置 Preferita)。

当我开始时,适配器部分是:

ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this,
            android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

一切都神奇地好……为什么使用默认布局好?再次,我在哪里指定什么去哪里?

提前致谢。

4

1 回答 1

1

When you want to implement a simple list, there is no problem using a stock ArrayAdapter. Android looks at the objects you supply to the adapter, calls toString() on those objects and populates a TextView with id=@android:id/text1 with that String. It worked "magically" when you used android.R.layout.simple_list_item_1 because that layout includes a TextView with id=@android:id/text1

If you want more control over the layout of each row in your ListView, you can use the SimpleAdapter which lets you map values to certain Views in your row layout. Alternatively, you can write your own adapter class by extending ArrayAdapter. I recommend writing your own Adapter, as it is both fun and it gives some insight in how lists are populated. Here's a guide on how to get started with that.

于 2012-12-18T12:31:39.180 回答