1

我有一个 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;
}

离开行:

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--------|
 |                               |
 ---------------------------------

我究竟做错了什么?请帮我。

4

1 回答 1

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);
    }

然后在我的列表视图上的项目单击侦听器

     onItemClick (AdapterView<?> parent, View view, int position, long id){
            adapter.setSelectedIndex(position);
            adapter.notifyDataSetChanged();
      }

希望这可以帮助

于 2013-02-12T22:49:51.737 回答