When extending an ArrayAdapter and overriding getView, is passing the resource/textViewResourceId arguments completely redundant?

痞子三分冷 提交于 2019-12-13 20:07:13

问题


I've been messing around with custom ArrayAdapters for ListViews a bit, and when extending them I've always simply passed -1 (A non-existent resource id) as the resource argument to super. Are there any potential benefits (at all) to passing anything else in it's place when you also override getView?


回答1:


super(context, R.layout.list_item_layout, R.id.item_text_inside_layout, items);

the 3rd parameter is usefull when you want to use custom layout for ListView/Gridview.

ArrayAdapter use Object.toString() to get the value of each item in Listview. It must have a TextView to display. So you have three options here

  1. Use default layout for text item. like android.R.layout.simple_list_item_1

  2. Use custom layout for text item and provide textViewId to place your data. like super(context, R.layout.list_item_layout, R.id.item_text_inside_layout, items);

  3. Use custom Adapter, not ArrayAdapter. You can extend BaseAdapter and inflate what view you want

Hope it help !




回答2:


Are there any potential benefits (at all) to passing anything else in it's place when you also override getView?

Sure. If you pass your actual layout and TextView Resource ID, you can let the super.getView() method handle the View inflation and assigning the text on a single TextView. Then your getView() override would just need to "fill in the blanks".

For example, say we have the following simple list item class:

public class Item {
    String text;
    int imageResId;

    public Item(String text, int imageResId) {
        this.text = text;
        this.imageResId = imageResId;
    }

    @Override
    public String toString() {
        return text;
    }
}

And a simple item layout like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <ImageView android:id="@+id/item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Then our ArrayAdapter subclass could be simply this:

public class MyAdapter extends ArrayAdapter<Item> {
    public MyAdapter(Context context, List<Item> items) {
        super(context, R.layout.list_item, R.id.item_text, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);

        ((ImageView) v.findViewById(R.id.item_image))
            .setImageResource(getItem(position).imageResId);

        return v;
    }
}

Note that we implement a toString() override in our Item class to provide the correct String to ArrayAdapter.



来源:https://stackoverflow.com/questions/37824293/when-extending-an-arrayadapter-and-overriding-getview-is-passing-the-resource-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!