如何在许多交换机的ListView中打开一个开关?

时间:2016-02-10 23:07:08

标签: android oncheckedchanged

我正在构建一个Android应用程序,我希望在ListView中随时只有一个处于开启模式的开关。我正在使用onCheckedChanged方法。我怎么能这样做?

public ListAdapter2(Context context, ArrayList<String> resource, Boolean[] checkedStatus) {
    super(context,R.layout.toggle_button_row,resource);
    // TODO Auto-generated constructor stub
    this.context = context;
    goalList=new ArrayList<String>();
    this.goalList =resource;
    this.checkedStatus=checkedStatus;

}

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    convertView = inflater.inflate(R.layout.toggle_button_row, parent, false);
    v1=convertView;
    TextView name = (TextView) v1.findViewById(R.id.textview1);
    name.setText(goalList.get(position).toString());
    sw= (Switch) v1.findViewById(R.id.switch1);
    sw.setTag(position);
    sw.setOnCheckedChangeListener(ListAdapter2.this);
    sw.setChecked(checkedStatus[position]);

    return convertView;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Integer index = (Integer) buttonView.getTag();
    checkedStatus[index] = isChecked;
    String key = index.toString();

    //save the data for the status
    SharedPreferences sharedPreferences = getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, isChecked);
    editor.putBoolean("MyGoal", isChecked);
    editor.putString("MyGoal1", goalList.get(index).toString().trim());
    editor.apply();
}

我添加了更多代码。我想要做的就是这个,在这个特定的ListView中,当我打开例如第一个我想要保存它的状态,我已经通过使用sharedPreferences完成了这个,但我不希望有多个开关。因此,当我激活第二个时,我希望第一个关闭。这同样适用于所有这些,当一个活跃时,其他人都关闭。

public void display(View v) {
    //The database is open!
    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();
    //initialize
    goalList = new ArrayList<String>();
    //put cursor
    Cursor cursor = database.rawQuery("select * from tbl_WG", null);
    cursor.moveToFirst();
    if(!cursor.isAfterLast()) {
        do {
            String name=cursor.getString(cursor.getColumnIndex("name"));
            Integer steps=Integer.parseInt(cursor.getString(1));

            goalList.add("Name: "+name+" || Steps: "+steps);


        } while (cursor.moveToNext());
    }
    cursor.close();
    //size of the status list;
    status= new boolean[goalList.size()];

    //check state
    SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
    checkedStatus = new Boolean[goalList.size()];
    for ( int index = 0; index < checkedStatus.length; index++)
        checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);


    //create an ArrayAdaptar from the String Array
    ListAdapter2 adapter = new ListAdapter2(this, goalList, checkedStatus);
    // Assign adapter to ListView
    mainListView.setAdapter(adapter);

}

1 个答案:

答案 0 :(得分:0)

我想你差不多了。

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        // First turn off the switch of any other item that may be on
        Arrays.fill(checkedStatus, false);

        Integer index = (Integer) buttonView.getTag();
        checkedStatus[index] = isChecked;
        notifyDataSetChanged();  // don't forget to tell ListView that data is updated

        //save the data for the status
        String key = index.toString();
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        /*
         *  I don't think this code:
         *
         *      editor.putBoolean(key, isChecked);
         *
         *  is a really good idea, because you have to make sure
         *  you "uncheck" any keys that were previously checked.
         *
         *  This might be better:
         */
        if (isChecked) {
            editor.putInt("last_index", index);
        } else {
            editor.remove("last_index"); // nothing's checked, so erase pref
        }

        editor.putBoolean("MyGoal", isChecked);

        // not sure this makes sense if isChecked == false, you may want to rethink the preferences
        editor.putString("MyGoal1", goalList.get(index).toString().trim());
        editor.apply();
    }

此外,在您的getView方法中,您应该执行以下操作:

        sw.setOnCheckedChangeListener(null);
        sw.setChecked(checkedStatus[position]);
        sw.setOnCheckedChangeListener(ListAdapter2.this);

当您设置切换的已检查状态时,这将阻止任何回收的侦听器触发。

使用上面的首选项保存代码,以下是恢复已检查状态的方法:

    //check state
    SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
    checkedStatus = new Boolean[goalList.size()];
    int index = sharedPreferences.getInt("last_index", -1);
    if (index != -1) {
        checkedStatus[index] = true;
    }