![](/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.