滚动Listview时切换更改状态

时间:2015-05-22 06:37:12

标签: android listview switch-statement toggle

我有一个包含两个切换/切换按钮项的列表视图。我将第一个开关项的settext设置为OFF时为“No”,ON时为“Yes”。并将第二项settext保留为默认值文本(例如ON / OFF)。每当我向下滚动我的listview时,switchitem就会改变它的状态。如何将开关状态保持为原始值?请帮忙。我不知道我的代码中的确切问题在哪里,但我认为问题出在getview中。请参阅下面的代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.det_item_layout, null);

        holder = new ViewHolder();
        holder.drawerItemIcon =  (ImageView) convertView.findViewById(R.id.ic1);
        holder.drawerItemName = (TextView) convertView.findViewById(R.id.ic2);
        holder.switchItem = (Switch) convertView.findViewById(R.id.switch);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.drawerItemIcon.setImageResource(drawerItems.get(position).getIcon_Id());
    holder.drawerItemName.setText(drawerItems.get(position).getName());

    holder.switchItems.setTag(drawerItems.get(position).getKey());
    holder.switchItems.setChecked(Boolean.parseBoolean(drawerItems.get(position).getValue()));

    if (drawerItems.get(position).getKey() == Globals.DRAWER_LOCKCARD){

        holder.switchItems.setTextOn("Yes");    
        holder.switchItems.setTextOff("No");

    }


    holder.switchItems.setOnCheckedChangeListener(switchItemOnCheckedChangeListener);

    holder.switchItems.setVisibility(drawerItems.get(position).getTag().equals("SWITCH") ? View.VISIBLE : View.GONE);

    return convertView;
} 

2 个答案:

答案 0 :(得分:0)

设置按钮/开关按钮的状态并进行如下检查。问题解决了,记得添加notifyDataSetChanged();改变按钮状态后。

if (profileActivationStatus.equalsIgnoreCase("Y")) 
        {
            convertView.setBackgroundResource(R.drawable.profile_border);

            activeInactivestateBtn.setBackgroundResource(R.drawable.active1);

        }

答案 1 :(得分:0)

你在Switch的Listener中做了什么?您是否将切换状态更新为 drawerItems ?在Listener of Switch中试试这个:

drawerItems.get(position).setValue("True/False");

顺便说一句,为什么你必须使用String而不是Boolean来保存状态?