我有一个ArrayAdapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)LayoutInflater.from(context);
v = vi.inflate(R.layout.consumerlist_row, null);
}
SharedClass.GenericItem item = SharedClass.consumerList.get(position);
if (item != null) {
final GenericItem fitem = item;
v.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = ID = ((ExtEditText)v.findViewById(R.id.dayreadout_entry)).getIdentifyingNumber();
if(prevID!=ID){
if(!((ExtEditText)v.findViewById(R.id.dayreadout_entry)).isSelected()
&&!((ExtEditText)v.findViewById(R.id.nightreadout_entry)).isSelected()){
if(fitem.GetIdAddress()==ID){
//Set unique identificator of
((ExtEditText)v.findViewById(R.id.dayreadout_entry)).setChosen(true);
TransitionDrawable drawable = (TransitionDrawable) v.getBackground();
drawable.startTransition(animSpeed);
}
if(prevID!=-1){
CloseEditMode();
LeaveRow(prevRow, prevID);
}
prevRow = v;
prevID=ID;
}
}
}
});
SetColorToRow( v, item);
return v;
}
LeaveRow:
private void LeaveRow(View row, int id){
int ids = ((ExtEditText)row.findViewById(R.id.dayreadout_entry)).getIdentifyingNumber();
if(id==ids){
SharedClass.GenericItem item = GetItemByID(id);
((ExtEditText)row.findViewById(R.id.dayreadout_entry)).setChosen(false);
TransitionDrawable tDrawable = (TransitionDrawable)row.getBackground();
tDrawable.reverseTransition(animSpeed);
}
}
SetColorToRow:
private void SetColorToRow(View v, GenericItem item){
boolean ids = ((ExtEditText)v.findViewById(R.id.dayreadout_entry)).isChosen();
String day = String.valueOf(item.GetDay());
String night = String.valueOf(item.GetNight());
String skipped = String.valueOf(item.GetSkipped());
if(day.equals("-9999999")&&night.equals("-9999999")&&!skipped.equals("0")){
if(ids){
TransitionDrawable td = (TransitionDrawable) v.getBackground();
td.startTransition(animSpeed);
}
else
v.setBackgroundResource(R.drawable.listviewitem_yellow_blue);
}
else if(day.equals("-9999999")&&night.equals("-9999999")&&skipped.equals("0")){
if(ids){
TransitionDrawable td = (TransitionDrawable) v.getBackground();
td.startTransition(animSpeed);
}
else
v.setBackgroundResource(R.drawable.listviewitem_white_blue);
}
else {
if(ids) {
TransitionDrawable td = (TransitionDrawable) v.getBackground();
td.startTransition(animSpeed);
}
else
v.setBackgroundResource(R.drawable.listviewitem_green_blue);
}
}
问题是当我选择一行(该行变成蓝色)并向下移动列表时(当行消失时)它选择其他行。似乎它复制了行的状态并将其放在下面的列表中。
--------REAL SELECTED ROW--------
---------------------------------
| |
| |
| |
| LIST VIEW |
| |
| |
| |
|-----WRONG SELECTED ROW--------|
| |
---------------------------------
我做错了什么?求你帮帮我。
答案 0 :(得分:0)
我发现在适配器中添加了一个字段来实现这个目的:
private int selectedIndex;
public void setSelectedIndex(int index){
this.selectedIndex = index;
}
//Your activity or controller can access this index at any time.
public int getSelectedIndex(){return selectedIndex;}
在公共视图中查看getView(int position,View convertView,ViewGroup parent)
if(selectedIndex == position){
convertView.setBackground(R.id.some_resource_selectable);
}else{
//set background null
convertView.setBackground(null);
}
然后在我的listview上点击项目点击监听器
onItemClick (AdapterView<?> parent, View view, int position, long id){
adapter.setSelectedIndex(position);
adapter.notifyDataSetChanged();
}
希望这有帮助