ListActivity与自定义布局不叫getview [英] ListActivity with custom layout does not call getview

查看:163
本文介绍了ListActivity与自定义布局不叫getview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义行布局的ListActivity。该行布局由一个复选框,一个TextView和的ImageButton的。

这背后是包含一个简单的字符串和布尔自定义对象的ArrayList。我想复选框以反映布尔,TextView的反映斯汀,并在将揭开序幕诉讼的权利的按钮。

我用一个ArrayAdapter页面上列出的最后一个构造函数来尝试这个绑定在一起。 ArrayAdapter(上下文的背景下,INT资源,诠释textViewResourceId,列表< T>对象)

由于某些原因,TextView的是的ToString(对象)填充,而不是由getView。因此该复选框永远不会布尔价值可言。任何想法?

 公共类ListSelection扩展ListActivity {
    私人的ArrayList<&列表项GT;清单;
    私人的ArrayList<串GT;删除;    静态私有最终字符串TAG =亲切;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d(TAGListSelection.onCreate);        名单=新的ArrayList<&列表项GT;();
        删除=新的ArrayList<串GT;();        意向TEMP = getIntent();
        的String [] = listNames temp.getStringArrayExtra(listNames);
        布尔[] listStatus = temp.getBooleanArrayExtra(listStatus);
        的for(int i = 0; I< listNames.length;我++){
            list.add(新的ListItem(listNames [I],listStatus [I]));
        }        ** setListAdapter(新ArrayAdapter<&列表项GT;(这一点,R.layout.selectionlistitem,R.id.text,列表)); **    }    私有类列表项{
        串LISTNAME;
        布尔listStatus;
        公共列表项(字符串名称,布尔状态){
            LISTNAME =名称;
            listStatus =状态;
        }
        @覆盖
        公共字符串的toString(){
            返回LISTNAME;
        }
    }    公共类ListItemArrayAdapter扩展ArrayAdapter<&列表项GT; {
        INT资源;
        LayoutInflater六;
        私人列表<&列表项GT;项目;        公共ListItemArrayAdapter(上下文的背景下,INT _resource,列表与LT;列表项> listItems中){
            超(背景下,_resource,listItems中);
            Log.d(TAGlistItemArrayAdapter);
            VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
            资源= _resource;
            this.items = listItems中;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            Log.d(TAGlistItemArrayAdapter.getView);
            的LinearLayout NewView的;            列表项项=的getItem(位置);            如果(convertView == NULL){
                NewView的=新的LinearLayout(的getContext());
                vi.inflate(资源,NewView的);
            }其他{
                NewView的=(的LinearLayout)convertView;
            }            TextView中的TextView =(TextView中)newView.findViewById(R.id.text);
            Log.d(TAG,item.listName);
            textview.setText(item.listName);            复选框复选框=(复选框)newView.findViewById(R.id.checkbox);
            checkbox.setChecked(item.listStatus);
            返回NewView的;
        }
    }}


解决方案

在我看来,你应该取代这一行:

  setListAdapter(新ArrayAdapter<&列表项GT;(这一点,R.layout.selectionlistitem,R.id.text,列表));

本:

  setListAdapter(新ListItemArrayAdapter<&列表项GT;(这一点,R.layout.selectionlistitem,列表));

既然这样,你正在使用 ArrayAdapter 的默认视图生成逻辑,这是饲料的toString()对于每个数组元素到的TextView

I have a ListActivity with a custom row layout. The row layout consists of a checkbox, a textview and an imagebutton.

Behind this is an ArrayList of a custom object containing simply a String and a boolean. I want to checkbox to reflect the boolean, the textview to reflect the Sting, and the button on the right will kick off an action.

I use the last constructor listed on the ArrayAdapter page to try to bind this all together. ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

For some reason the textView is populated by the objects toString(), and not by getView. Therefore the checkbox never gets the boolean value at all. Any ideas?

public class ListSelection extends ListActivity {
    private ArrayList<ListItem> list;
    private ArrayList<String> deleted;  

    static private final String TAG = "kinder";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "ListSelection.onCreate");   

        list = new ArrayList<ListItem>();
        deleted = new ArrayList<String>();

        Intent temp = getIntent();
        String[] listNames = temp.getStringArrayExtra("listNames");
        boolean[] listStatus = temp.getBooleanArrayExtra("listStatus");
        for (int i = 0; i < listNames.length; i++) {
            list.add(new ListItem(listNames[i], listStatus[i]));
        }

        **setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));**

    }

    private class ListItem {
        String listName;
        Boolean listStatus;     
        public ListItem(String name, boolean status) {
            listName = name;
            listStatus = status;
        }
        @Override
        public String toString() {
            return listName;
        }
    }

    public class ListItemArrayAdapter extends ArrayAdapter<ListItem> {
        int resource;
        LayoutInflater vi;
        private List<ListItem> items;

        public ListItemArrayAdapter(Context context, int _resource, List<ListItem> listitems) {
            super(context, _resource, listitems);
            Log.d(TAG, "listItemArrayAdapter");
            vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            resource = _resource;
            this.items = listitems;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d(TAG, "listItemArrayAdapter.getView");
            LinearLayout newView;

            ListItem item = getItem(position);

            if (convertView == null) {
                newView = new LinearLayout(getContext());
                vi.inflate(resource, newView);
            } else {
                newView = (LinearLayout)convertView;
            }

            TextView textview = (TextView)newView.findViewById(R.id.text);
            Log.d(TAG, item.listName);
            textview.setText(item.listName);

            CheckBox checkbox = (CheckBox)newView.findViewById(R.id.checkbox);
            checkbox.setChecked(item.listStatus);
            return newView;
        }
    }

}

解决方案

It seems to me that you should replace this line:

setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));

with this:

setListAdapter(new ListItemArrayAdapter<ListItem>(this, R.layout.selectionlistitem, list));

As it stands, you are using the default view generation logic of ArrayAdapter, which is to feed toString() for each array element to a TextView.

这篇关于ListActivity与自定义布局不叫getview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆