滚动时列出适配器取消选中我的切换按钮。从这里尝试了多种解决方案,但没有运气

时间:2015-03-04 18:33:19

标签: android android-listview android-viewholder android-togglebutton

我的适配器(其风格已经为我的应用程序的其余部分工作)现在给了我一些问题。这是第一个具有可切换按钮的列表。每个listItem都有一个toggleButton,当我切换,向下滚动然后再向上时,它会自动取消选中。在过去的两天里,我一直在尝试使用SO的不成功解决方案,从禁用回收到添加视图持有者,这两种方法都不起作用。

我知道代码可能看起来有点混乱,但希望它仍然可以理解。变量已经改变了,我很确定我得到了每个实例,但我主要需要逻辑/语法方面的帮助。我没有展示一些我拥有的ViewHolder代码,主要是因为这会让它更加混乱。

我在每个字符串中发送了相同数量的字符串值的String []。

public class MyListAdapter extends BaseAdapter
{

Activity activity;

/**
 * String[] of sender Data
 */

String[] Id, Name, Alert, Instructions, RedText, dawcb; //Alert, Name, and a toggleButton are to be displayed in list

TextView status, date, prescription;

ImageView AlertIcon;

ToggleButton tb;

/**
 * Index of Selected Item
 */
private int _index = -1;

/**
 * Inflate Layout in Application Context
 */
private static LayoutInflater inflater = null;

public MultiRenewAdapter(Activity a, String[] Id, String[] Name, String[] Alert, String[] Instructions, String[] RedText, String[] dawcb)
{
    activity = a;
    this.Id = Id;
    this.Name = Name;
    this.Alert = Alert;
    this.Instructions = Instructions;
    this.RedText = RedText;
    this.dawcb = dawcb;

    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


//some code to help save selected items

static class ViewHolder {
protected ImageView alertIcon;
    protected TextView text;
    protected ToggleButton tb;
}

public int getCount()
{
    return Name.length;
}

public Object getItem(int position)
{
    return position;
}

public long getItemId(int position)
{
    return position;
}

//    I thought I could try turning off recycling (this is from another post here):
//    @Override
//    public int getViewTypeCount() {
//        //Count=Size of ArrayList.
//        return Name.length;
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//
//        return position;
//    }



/**
 * Override Get View for custom format
 */
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View vi = convertView;//vi and convertView are null

    ViewHolder viewHolder = null;

    vi = inflater.inflate(R.layout.row_layout, null);

    viewHolder = new ViewHolder();

    positionInt = position;

//doesn't work since viewHolder is null, and is causing a null pointer exception
    //viewHolder.tb = (ToggleButton) vi.findViewById(R.id.toggleButton);
    //viewHolder.prescription = (TextView)    //vi.findViewById(R.id.prescription);
    //viewHolder.AlertIcon = (ImageView) vi.findViewById(R.id.AlertIcon);


    if (position == _index)
    {
vi.setBackgroundColor(activity.getResources().getColor(R.color.DeepSkyBlue));
    }
    else
    {
            vi.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
        if (position % 2 == 0)
        {
            vi.setBackgroundColor(Color.TRANSPARENT);
        }
        else
        {
            vi.setBackgroundColor(Color.LTGRAY);
        }
    }

    if(convertView==null)
    {

        if (position % 2 == 0)
        {
            vi.setBackgroundColor(Color.TRANSPARENT);
        }
        else
        {
            vi.setBackgroundColor(Color.LTGRAY);
        }
    }

    tb = (ToggleButton) vi.findViewById(R.id.toggleButton);
    prescription = (TextView) vi.findViewById(R.id.prescription);
    AlertIcon = (ImageView) vi.findViewById(R.id.AlertIcon);

    if(Alert[position].equals("4")){
        AlertIcon.setBackgroundResource(R.drawable.checktemp);
    }else if(Alert[position].equals("3")){
        AlertIcon.setBackgroundResource(R.drawable.orange_diamond_temp);
    }else if(Alert[position].equals("2")){
        AlertIcon.setBackgroundResource(R.drawable.exclamation_red_temp);
    }else if(Alert[position].equals("1")){
        AlertIcon.setBackgroundResource(R.drawable.red_x_temp);
    } else {
        AlertIcon.setVisibility(View.INVISIBLE);
    }

    prescription.setText(Name[position]);

    return vi;
}
public void setClicked(int index)
{
    _index = index;
    this.notifyDataSetChanged();
}
} 

我期待着所有人的帮助。

谢谢!

0 个答案:

没有答案