“getView” - the constant change of position of Items and I have to click twice of “Item”

送分小仙女□ 提交于 2020-01-05 12:13:56

问题


I have a realization of ArrayAdapter (extends ArrayAdapter), the method getView constantly redraw the screen,constantly changing position item of the ListView. But this is not the only problem. When I want to make text changes - are re-sorted. As well as to activate the line by setOnClickListener I have to click twice

My source

public View getView(int position, View convertView, ViewGroup parent) {


        UrlItem urlItem = (UrlItem) this.getItem(position);
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.database_table_item, null);
            viewHolder = new ViewHolder();

            viewHolder.text = (TextView) convertView
                    .findViewById(R.id.editText1);

            viewHolder.text.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {                   


                    int getPosition = (Integer) v.getTag();
                }
            });



            convertView.setTag(viewHolder);

            convertView.setTag(R.id.editText1, viewHolder.text);
            viewHolder.text.setTag(position);
            viewHolder.text.setText(urlItem.getUrl());
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        return convertView;

    }

Helped me to this link.


回答1:


You're not doing anything with your views when they are recycled (i.e. when convertView != null). You need to do any position-specific stuff after obtaining the viewHolder. Try something more along these lines:

if (convertView == null) {

    convertView = inflater.inflate(R.layout.database_table_item, null);
    viewHolder = new ViewHolder();

    viewHolder.text = (TextView) convertView
            .findViewById(R.id.editText1);

    viewHolder.text.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {                   


            int getPosition = (Integer) v.getTag();
        }
    });

    convertView.setTag(viewHolder);

} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.text.setTag(position);
viewHolder.text.setText(urlItem.getUrl());

return convertView;


来源:https://stackoverflow.com/questions/11953875/getview-the-constant-change-of-position-of-items-and-i-have-to-click-twice-o

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