我想实现我的onItemSelected,以便当用户单击该复选框时,它将标记为“已选中” 然后更新列DBContract.EntryColumns.IS_CHECKED以检查(将其设置为1)。一切都很好,但问题是我不知道如何 实现onItemSelected方法。请有人告诉我如何解决这个问题。
这是我的Activity和list_item_book.xml,其中定义了CheckBox。
public class MainActivity extends AppCompatActivity implements
BookAdapter.OnItemClickListener,
View.OnClickListener {
private BookAdapter mAdapter;
private CheckBox mCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new BookAdapter(null);
mAdapter.setOnItemClickListener(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
/* This method will be called when I Click on checkboxes */
@Override
public void onItemSelected(boolean active, int position) {
//what should I write here?
// DBContract.EntryColumns.IS_CHECKED
//
}
}
// list_item_book.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
答案 0 :(得分:0)
您可以将复选框映射到ID并将其存储到Map&lt; Checkbox,Integer&gt;。然后只需在复选框上设置监听器。 (假设checkbox
变量具有Checkbox
类型)
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Get the ID here
int id = yourMapCheckboxToId.get(buttonView);
// ... do what you are going to do having ID
}
}
};
checkbox.setOnCheckedChangeListener(listener);