【问题标题】:switch state is not stable in listview when scrolling滚动时列表视图中的开关状态不稳定
【发布时间】:2019-05-12 20:46:23
【问题描述】:

我是 android 新手,我在列表视图中使用示例。因此,我无法更改代码。我的 Listview 的每个项目中都有一个开关按钮。当我滚动 Listview 开关时随机更改状态。如何使开关状态稳定?

我的列表视图类适配器:

public class MyCustomCursorAdapter extends CursorAdapter {
LIGHTS calling_activity; 
private DatabaseHelper dbHelper;

public MyCustomCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
    this.calling_activity = (LIGHTS) context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    return view;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}

@Override
public void bindView(final View view, Context context, Cursor cursor) {
    ((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
    ((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.TITLE)));
    ((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.DESC)));

    ImageView option = view.findViewById(R.id.itemoption);

    Switch thisswitch = view.findViewById(R.id.onoff);
    thisswitch.setTag(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
    thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    calling_activity.myOnCheckedChangedHandler((String)buttonView.getTag(),isChecked);
        }
    });
    option.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView itemID = (TextView) view.findViewById(R.id.id);
            TextView itemTitle = (TextView) view.findViewById(R.id.KEYCODE);
            TextView itemDesc = (TextView) view.findViewById(R.id.NAME);

            String myId = itemID.getText().toString();
            String myTitle = itemTitle.getText().toString();
            String myDesc = itemDesc.getText().toString();

            Intent intent = new Intent(calling_activity, ModifyActivity.class);
            intent.putExtra("Id", myId);
            intent.putExtra("Title", myTitle);
            intent.putExtra("Desc", myDesc);
            calling_activity.startActivity(intent);
        }
    });
}
}

在我的 Lights 活动中:

@Override
public void myOnCheckedChangedHandler(String id, boolean check_status) {
    Toast.makeText(
            this,
            "You changed the status for the row with an id of " + id +
                    " the status is now " + new Boolean(check_status).toString(),
            Toast.LENGTH_SHORT).show();

    String name = cursor.getString(cursor.getColumnIndex(dbHelper.DESC));

    if(new Boolean(check_status).toString().equals("true")){
        Toast.makeText(this,name +" is ON", Toast.LENGTH_SHORT).show();
    }

}

【问题讨论】:

    标签: java android listview adapter


    【解决方案1】:

    请通过适配器的bindView中的if/else case进行管理,您需要检查如下开关条件:

    // set the code into bind
    if(switchCondition1 == 1)
    {
     //set the code as per condition wise 
    }else{
    //
    }
    

    更改状态后,您还需要将状态更改为列表项,并使用 notifydatasetChanged() 方法刷新列表项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多