我有一个带有row.xml的回收站视图,其中包含两个textview和一些按钮。我想要完成的是让用户在calendarView上选择一个日期并将其更改为String。之后它还应该显示用户输入的Item的名称。我的问题是,而不是将项目的日期和名称放在一行中。它显示了两行在每个textView中具有相同值。这样的例子。第1行= textview(Hello),textView(Hello) 第2行= TextView(march14),Textview(3月14日)。
这是我的RecyclerViewAdapter
public class ItemRecyclerViewAdapter extends RecyclerView.Adapter<ItemRecyclerViewAdapter.ViewHolder>{
private ArrayList<String> mDataset;
public ItemRecyclerViewAdapter(ArrayList<String> mDataset){
this.mDataset = mDataset;
}
@Override
public ItemRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_row_inventory, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ItemRecyclerViewAdapter.ViewHolder holder, int position) {
holder.mItemName.setText(mDataset.get(position));
holder.mItemDate.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mItemName;
public TextView mItemDate;
public ViewHolder(View itemView) {
super(itemView);
mItemName = itemView.findViewById(R.id.ItemNameTxtView);
mItemDate = itemView.findViewById(R.id.expirationDateTxtView);
}
}
这是我的活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_freezer);
mDataset = new ArrayList<>();
mRecyclerView = findViewById(R.id.FreezerInventoryRecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ItemRecyclerViewAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
AddFreezerItems = findViewById(R.id.addItemsFreezer);
SharedPreferences prefs = getSharedPreferences("itemInfo1", Context.MODE_PRIVATE);
String name = prefs.getString("FoodName1", null);
String date = prefs.getString("ExpirationDate1", null);
mDataset.add(name);
mDataset.add(date);
mAdapter.notifyDataSetChanged();
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.apply();
SharedPreferences preferences = getSharedPreferences("itemName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = preferences.edit();
editor1.clear();
editor1.apply();
}
答案 0 :(得分:0)
问题是您的数据类型 - 目前是ArrayList<String>
创建一个POJO来保存您的ROW信息,一个项目应包含填充一行的所有数据。
POJO:
public class Event {
private final String name;
private final String date;
public Event(String name, String date) {
this.name = name;
this.date = date;
}
public String getName() {
return name;
}
public String getDate() {
return date;
}
}
创建项目:
mDataset = new ArrayList<Event>(); // change your current instance variable from String to Event type
String name = prefs.getString("FoodName1", null);
String date = prefs.getString("ExpirationDate1", null);
mDataset.add(new Event(name, date));
您的数据集例如是:
public ItemRecyclerViewAdapter(ArrayList<Event> mDataset){
this.mDataset = mDataset;
}
绑定数据时:
@Override
public void onBindViewHolder(ItemRecyclerViewAdapter.ViewHolder holder, int position) {
holder.mItemName.setText(mDataset.get(position).getName());
holder.mItemDate.setText(mDataset.get(position).getDate());
}