0

我想实现我的onItemSelected,这样当用户单击复选框时,它将被标记为“已选中” ,然后更新列DBContract.EntryColumns.IS_CHECKED以进行检查(将其设置为1 )。一切工作正常,但问题是我不知道如何在实现onItemSelected方法 。请有人告诉我如何解决这个问题。复选框如何实现 - android recyclerview

这里是我的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

您可以复选框映射到的ID,并将其存储到地图<复选框,整数>。然后在复选框上设置监听器。 (假设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);