gpt4 book ai didi

java - 从一个构造函数访问数组列表到另一个构造函数android

转载 作者:行者123 更新时间:2023-11-29 18:28:36 25 4
gpt4 key购买 nike

我试图从一个构造函数中获取一个数组列表。我尝试了很多方法但没有成功。请在下面查看我的代码。如果这不可能,请向我建议实现此方法的任何替代方法。此代码在我的适配器中

ArrayList<CuisineCategory> mydataList = new ArrayList<CuisineCategory>();

ArrayList<CuisineCategory> dataList;

public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
this.context = context;
this.mydataList = mydataList;

}

public CuisineAdapter(Context context, CuisineListToggleListener listToggleListener) {
this.context = context;
this.listToggleListener = listToggleListener;
this.dataList = new ArrayList<>();

this.dataList = mydataList;<- this is not working

}

mydataList 包含数组列表,我想在第二个构造函数中使用它

请查看我的适配器完整代码

public class CuisineAdapter extends RecyclerView.Adapter<CuisineAdapter.MyViewHolder> {
private Context context;
ArrayList<CuisineCategory> mydataList = new ArrayList<>();

private ArrayList<CuisineCategory> dataList;

private CuisineListToggleListener listToggleListener;

public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
this.context = context;
//this.dataList = new ArrayList<>();
this.mydataList = mydataList;

// for(CuisineCategory dt: mydataList){
//
// //Log.e("data:", String.valueOf(dt.getTitle()));
// }
}

public CuisineAdapter(Context context, CuisineListToggleListener listToggleListener) {
this.context = context;
this.listToggleListener = listToggleListener;

this.dataList = new ArrayList<>();

this.dataList.add(new CuisineCategory("Popular"));
this.dataList.add(new CuisineCategory("Main course"));
this.dataList.add(new CuisineCategory("Lunch"));
this.dataList.add(new CuisineCategory("Breakfast"));
this.dataList.add(new CuisineCategory("Dinner"));
this.dataList.add(new CuisineCategory("Appetizer"));

}




@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_cuisine_cat, parent, false));
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.setData(dataList.get(position));
}

@Override
public int getItemCount() {
return dataList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout rootLayout;
private TextView cuisineCategory;
private ImageView cuisineItemToggle;
private RecyclerView cuisineItemList;

public MyViewHolder(View itemView) {
super(itemView);
cuisineItemList = itemView.findViewById(R.id.cuisineItemList);
cuisineCategory = itemView.findViewById(R.id.cuisineCategory);
cuisineItemToggle = itemView.findViewById(R.id.cuisineItemToggle);
rootLayout = itemView.findViewById(R.id.rootLayout);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
CuisineCategory category = dataList.get(pos);
category.setSelected(!category.isSelected());
Collections.swap(dataList, 0, pos);
notifyItemMoved(category.isSelected() ? pos : 0, category.isSelected() ? 0 : pos);
listToggleListener.OnListExpanded(category.isSelected());
notifyItemChanged(0);
}
});
}

public void setData(CuisineCategory category) {
cuisineCategory.setText(category.getTitle());
cuisineItemToggle.setImageDrawable(ContextCompat.getDrawable(context, category.isSelected() ? R.drawable.ic_keyboard_arrow_up_accent_24dp : R.drawable.ic_keyboard_arrow_down_accent_24dp));

cuisineItemList.setLayoutManager(new LinearLayoutManager(context));
cuisineItemList.setAdapter(new RestaurantMenuAdapter(context));
cuisineItemList.setVisibility(category.isSelected() ? View.VISIBLE : View.GONE);

ViewGroup.LayoutParams layoutParams = rootLayout.getLayoutParams();
layoutParams.height = category.isSelected() ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT;
rootLayout.setLayoutParams(layoutParams);
}
}

public interface CuisineListToggleListener {
void OnListExpanded(boolean selected);
}
}

在我的第二个适配器 dataList 数组中,我想动态加载它,所以使用改造我使用下面的代码将一些数据提取到 ArrayList

cuisineAdapter = new CuisineAdapter(RestaurantDetailActivity.this,dataList);

像这样设置适配器

private void setupCuisineRecycler() {
linearLayoutManager = new MyLinearLayoutManager(getContext());
cuisineRecycler.setLayoutManager(linearLayoutManager);

cuisineRecycler.setAdapter(new CuisineAdapter(getContext(), new CuisineAdapter.CuisineListToggleListener() {
@Override
public void OnListExpanded(final boolean selected) {
if (selected) {
cuisineRecycler.scrollToPosition(0);
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
linearLayoutManager.setScrollEnabled(!selected);
}
}, 500);
}
}));
}

现在我想将数据绑定(bind)到datalist。

最佳答案

不能这样做。

this.dataList = mydataList;<- this is not working

在您的第二个构造函数中 mydataList 不存在

你可以这样做:

  public class CuisineAdapter{

private ArrayList<CuisineCategory> mydataList;

public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
this.context = context;
this.mydataList = mydataList;
}

public CuisineAdapter(Context context,
ArrayList<CuisineCategory> mydataList,
CuisineListToggleListener listToggleListener) {
this(context, mydataList);
this.listToggleListener = listToggleListener;
}

public CuisineAdapter(Context context,
CuisineListToggleListener listToggleListener) {
this.context = context;
this.listToggleListener = listToggleListener;
}

public ArrayList<CuisineCategory> getMydataList(){
return mydataList;
}

public void setMydataList(ArrayList<CuisineCategory> mydataList){
this.mydataList= mydataList;
}

}

然后将其用于:

CuisineAdapter adapter = new CuisineAdapter(context, mydataList); 

或:

CuisineAdapter adapter = new CuisineAdapter(context, listToggleListener);
adapter.setMydataList(mydataList);

关于java - 从一个构造函数访问数组列表到另一个构造函数android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57561313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com