首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ListAdapter。getView从视图返回错误的项目链接(ConvertView)

ListAdapter。getView从视图返回错误的项目链接(ConvertView)
EN

Stack Overflow用户
提问于 2019-05-23 03:41:08
回答 1查看 21关注 0票数 0

我在使用ListAdapter(ArrayAdapter)时出现了一个奇怪的错误。我解决了这个问题,但我认为,任何人都可以有一些问题,我决定写下我的解决方案。

我的任务: ListView,它有来自每个项目的onclick事件。最后选择的项目(其_id值)保存到SharedPreferences中。重新打开活动后,需要加载最后选择的项目位置(使用SharedPreferences中保存的_id )。

问题:加载组件后,我多次使用其他视图的值(null或not null)和位置( position值为0)调用getView。但主要的问题是从所选项目获取链接。

我的代码:

代码语言:javascript
复制
public class ListAdapter extends ArrayAdapter<String[]> {
private final LayoutInflater mInflater;
private final ViewBinderHelper binderHelper;
public ViewHolder beforeHolder = null;

public ListAdapter(Context context, List<String[]> objects) {
    super(context, R.layout.lv_item, objects);
    mInflater = LayoutInflater.from(context);
    binderHelper = new ViewBinderHelper();
}

public  int a = 0;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    int i = Integer.valueOf(ProgramActivity.dirNames.get(position));
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.lv_item, parent, false);

        holder = new ViewHolder();
        holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_layout);
        holder.frontView = convertView.findViewById(R.id.front_layout);
        holder.textView = (TextView) convertView.findViewById(R.id.text);


        holder.frontView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backStack.size() > 0) {
                    Toast.makeText(getContext(), "У вас уже выбрана последовательность программ", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (beforeHolder != null) {
                    beforeHolder.frontView.setBackgroundResource(R.color.colorAccent);
                }
                else {
                    //ProgramActivity.adapter.notifyDataSetChanged();
                }

                view.setBackgroundResource(R.color.holo_green_light);

                SharedPreferences.Editor ed = MainActivity.sPref.edit();
                ed.putInt("idProgram",holder.idProgram);
                ed.commit();

                MainActivity.idProgram = holder.idProgram;

                beforeHolder = holder;
            }
        });


        convertView.setTag(holder);

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

    final String[] item = getItem(position);
    if (item != null) {
        holder.idProgram = i;
        if (i == MainActivity.idProgram) {
            holder.frontView.setBackgroundResource(R.color.holo_green_light);
            beforeHolder = holder;
        } else {
            holder.frontView.setBackgroundResource(R.color.colorAccent);
        }

        binderHelper.bind(holder.swipeLayout, item[1]);

        holder.textView.setText(item[1]);
    }

    return convertView;
}

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onSaveInstanceState(Bundle)}
 */
public void saveStates(Bundle outState) {
    binderHelper.saveStates(outState);
}

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onRestoreInstanceState(Bundle)}
 */
public void restoreStates(Bundle inState) {
    binderHelper.restoreStates(inState);
}

private class ViewHolder {
    SwipeRevealLayout swipeLayout;
    View frontView;
    TextView textView;
    int idProgram;
}
}

beforeHolder = holder;有错误的链接,尽管代码是正确的,并且条件(i == MainActivity.idProgram)仅适用于一个选项。但是在我单击other item之后,在运行代码行beforeHolder.frontView.setBackgroundResource(R.color.colorAccent);之后,item的颜色没有改变。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56263865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档