Java A类创建2个B类实例。如何从B类内部访问B类(1)中的项?
我有一个分为两个部分的回收视图。它们在回收器视图中保存购物列表项目,每个项目都有一个复选框。我将此库用于以下部分: 我试图为复选框设置onCheckedChangeListener,因此如果第1节中的某个项被选中,它将转到第2节,当选中->未选中时,反之亦然。我首先尝试在选中时从第1节中删除一项 ShoppingList类(类A)创建HeaderRecyServiceViewSection(类B)的2个实例 购物清单类:Java A类创建2个B类实例。如何从B类内部访问B类(1)中的项?,java,android,sectionedrecyclerviewadapter,Java,Android,Sectionedrecyclerviewadapter,我有一个分为两个部分的回收视图。它们在回收器视图中保存购物列表项目,每个项目都有一个复选框。我将此库用于以下部分: 我试图为复选框设置onCheckedChangeListener,因此如果第1节中的某个项被选中,它将转到第2节,当选中->未选中时,反之亦然。我首先尝试在选中时从第1节中删除一项 ShoppingList类(类A)创建HeaderRecyServiceViewSection(类B)的2个实例 购物清单类: public class ShoppingList extends Ap
public class ShoppingList extends AppCompatActivity {
HeaderRecyclerViewSection firstSection = new HeaderRecyclerViewSection("Unchecked", getDataSource());
HeaderRecyclerViewSection secondSection = new HeaderRecyclerViewSection("Checked", getDataSource());
protected void onCreate(Bundle savedInstanceState) {
// shopping list with sections
sectionHeader = (RecyclerView) findViewById(R.id.shopping_listView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShoppingList.this);
sectionHeader.setLayoutManager(linearLayoutManager);
sectionHeader.setHasFixedSize(true);
sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(firstSection);
sectionAdapter.addSection(secondSection);
sectionHeader.setAdapter(sectionAdapter);
}
public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView itemContent;
public CheckBox checkBox;
public Button deleteButton;
public ItemViewHolder(View itemView) {
super(itemView);
itemContent = (TextView)itemView.findViewById(R.id.textView1);
checkBox = (CheckBox)itemView.findViewById(R.id.checkBox);
deleteButton = (Button)itemView.findViewById(R.id.deleteItem);
}
节课:
public class ShoppingList extends AppCompatActivity {
HeaderRecyclerViewSection firstSection = new HeaderRecyclerViewSection("Unchecked", getDataSource());
HeaderRecyclerViewSection secondSection = new HeaderRecyclerViewSection("Checked", getDataSource());
protected void onCreate(Bundle savedInstanceState) {
// shopping list with sections
sectionHeader = (RecyclerView) findViewById(R.id.shopping_listView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShoppingList.this);
sectionHeader.setLayoutManager(linearLayoutManager);
sectionHeader.setHasFixedSize(true);
sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(firstSection);
sectionAdapter.addSection(secondSection);
sectionHeader.setAdapter(sectionAdapter);
}
public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView itemContent;
public CheckBox checkBox;
public Button deleteButton;
public ItemViewHolder(View itemView) {
super(itemView);
itemContent = (TextView)itemView.findViewById(R.id.textView1);
checkBox = (CheckBox)itemView.findViewById(R.id.checkBox);
deleteButton = (Button)itemView.findViewById(R.id.deleteItem);
}
我需要onCheckedChangeListener中的某些内容来获取单击的项目并将其从第一节中删除,然后通知购物列表中的适配器
public class HeaderRecyclerViewSection extends StatelessSection{
private static final String TAG = HeaderRecyclerViewSection.class.getSimpleName();
private String title;
public List<ShoppingItem> list;
public HeaderRecyclerViewSection(String title, List<ShoppingItem> list) {
super(SectionParameters.builder()
.itemResourceId(R.layout.shopping_item)
.headerResourceId(R.layout.section_header)
.build());
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size();
}
public boolean addItem(ShoppingItem shoppingItem) {
return list.add(shoppingItem);
}
public boolean removeItem(ShoppingItem shoppingItem) {
return list.remove(shoppingItem);
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
final ItemViewHolder iHolder = (ItemViewHolder)holder;
final ShoppingItem currentItem = list.get(position);
iHolder.itemContent.setText(currentItem.getItemName());
iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
removeItem(currentItem);
}
});
}
}那么您需要从ShoppingList中删除HeaderRecyclServiceViewSection实例吗?我需要从其中一个HeaderRecyclViewSection中删除一个项目要“删除”一个对象,您只需将其设置为null或使其超出范围即可。垃圾收集器(System.gc()
)将处理它。如何访问onCheckedChange中的其他列表?我必须删除它,然后将其添加到其他列表中,或者将其移动到某个可以完成的位置…发布您的适配器类。。。