![](/img/trans.png)
[英]I need to remove the line where a button was clicked from a listadapter. Preferably from within the method getView. As I do this?
[英]ListAdapter. getView return bad link of item from View(convertView)
我在ListAdapter(ArrayAdapter)中遇到了一个奇怪的错误。 我解决了这个问题,但我想,任何人都可以遇到一些问题,我决定写下我的解决方案。
我的任务:ListView,它具有来自每个项目的onclick事件。 最后选择的项目(其_id val)保存到SharedPreferences中。 重新打开活动后,需要加载最后选择的项目位置(在SharedPreferences中使用已保存的_id)。
问题:组件加载后,我有更多次调用getView和其他视图的值(null或not null)和position(位置值的几倍为0)。 但主要问题是从所选项目获取链接。
我的代码:
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 =持有人; 有错误的链接,虽然代码是正确的,条件(i == MainActivity.idProgram)只适用于一个选项。 但是在我点击其他项目并运行代码行之后,在Henry.frontView.setBackgroundResource(R.color.colorAccent)之后; 项目颜色未更改。
花了几天后,我找到了解决问题的方法。 我知道错误是在Activity布局中。 我将参数layout_height="wrap_content"
设置为ListView
,因为这个getView运行了很多次(我添加了相同的组件几次,因为它在添加过程中改变了它的大小)。
首先,我添加了一行
ProgramActivity.adapter.notifyDataSetChanged();
(在代码中,它被注释掉了。)并删除了beforeHolder = holder;
进入条件(i == MainActivity.idProgram)
,但第一次点击时此解决方案的延迟约为1秒。
最喜欢的解决方案是将layout_height
更改为fill_parent
。 总之,我想说这是某种bug,即使使用wrap_content
,beforeHolder变量也应该有一个有效的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.