【问题标题】:imageview listview图像视图列表视图
【发布时间】:2012-10-23 15:35:01
【问题描述】:

我有一个问题。

我创建了一个具有不同like、name、id、type 的数据库(数组)。 我还制作了读取名称的字符串(名称是一个字符串:

public static String[] getAllPokemonsInStrings() {
    ArrayList<String> items = new ArrayList<String>();
    for(Pokemon p : Pokemon.values()){
        if(p.getName() != null){
            items.add(p.getName());
        }
    }
    return items.toArray(new String[]{});
}

这是创建它的位:

public class Test3 extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,      Types.getAllPokemonsInStrings() ));

    }
}

每行的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  
    android:orientation="vertical"
    android:id="@+id/LinearLayout">  

    <ImageView
        android:id="@+id/sprite"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:contentDescription="@string/sprite"
        android:src="@drawable/p002" />

    <LinearLayout
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"  
        android:orientation="vertical">

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/label"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="10dip"
            android:textSize="16dip"
            android:textStyle="bold" />

    </LinearLayout>
</LinearLayout>

有没有办法改变每行的图像资源(它是一个 int),从数组中加载它?

【问题讨论】:

  • pokemon 的复数形式是不带 s 的 pokemon :P
  • 嗯,我将我的列表命名为 pokemons 以使其与当前的 pokemon 对象不同。没有伤害,没有犯规:P

标签: java android arrays image android-listview


【解决方案1】:

是的,这是可能的。不过,您需要创建自己的适配器类并覆盖getView()。我建议您阅读此tutorial,了解如何创建自定义适配器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多