我被困了好几个小时。我有一个带有recyclerview的活动。要添加到列表中,用户转到另一个活动,在那里他从一个字符串列表中进行选择。第二个活动将字符串返回给第一个活动,并将字符串添加到recyclerview的末尾。
如果用户点击了Recyclerview中项目的长按,则会出现一个对话框,他可以在该对话框中删除该项目。但是,一旦项目被删除,事情就会非常错误。有时列表会更改,有时当您从第二个活动添加另一个项目时,旧项目会返回。它如此随意,很难准确解释。在日志中,我看到正确的字符串已从第二个活动返回,但回收器视图无法正确显示。怎么回事?
即使列表“搞砸了”,当我重新进入活动时,我也会从列表中调用我的SharedPreferences,这一切都显示正常。所以ArrayList是正确的,只是在删除某些东西后它才能在recyclerview中正确显示。我希望它清楚。
以下是我在活动中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_off_limits);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
offLimitsItemsList = new ArrayList<>();
offLimitsItemsList = SharedPreferencesManager.getOffLimitsItemsList(this);
getSupportActionBar().setTitle("Off limits (This week)");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
offLimitsRecyclerView = findViewById(R.id.off_limits_recycler_view);
offLimitsAdapter = new OffLimitsAdapter(offLimitsItemsList);
offLimitsAdapter.setOnItemClickListener(new OffLimitsAdapter.ClickListener() {
@Override
public void onItemLongClick(final int position, View v) {
Log.d(TAG, "onItemClick pos = " + position);
AlertDialog.Builder builder = new AlertDialog.Builder(OffLimitsActivity.this);
builder.setMessage(R.string.dialog_delete)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, offLimitsItemsList.get(position) + " removed");
offLimitsItemsList.remove(position);
offLimitsAdapter.notifyItemRemoved(position);
SharedPreferencesManager.setOffLimitsItemsList(OffLimitsActivity.this, offLimitsItemsList);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create();
builder.show();
}
});
offLimitsRecyclerView.setAdapter(offLimitsAdapter);
offLimitsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK){
Log.d(TAG, "onActivityResult");
String itemEaten= data.getStringExtra(OFF_LIMITS_ITEM_EATEN);
offLimitsItemsList.add(itemEaten);
Log.d(TAG, "Item eaten: " + itemEaten);
offLimitsAdapter.notifyItemInserted(offLimitsItemsList.size()-1);
SharedPreferencesManager.setOffLimitsItemsList(OffLimitsActivity.this, offLimitsItemsList);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.off_limits_menu_add_item, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_off_limits:
Log.d(TAG, "Add clicked");
Intent intent = new Intent(OffLimitsActivity.this, SearchOffLimitsItemActivity.class);
startActivityForResult(intent, SEARCH_OFF_LIMIT_RESULT_CODE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是适配器:
public class OffLimitsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static ClickListener clickListener;
public TextView offLimitsTextViewItem;
ArrayList<String> offLimitsItemsList = new ArrayList<>();
public OffLimitsAdapter(ArrayList<String> offLimitsItemsList) {
this.offLimitsItemsList = offLimitsItemsList;
}
public interface ClickListener {
void onItemLongClick(int position, View v);
}
class OffLimitsViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener{
public OffLimitsViewHolder(View view) {
super(view);
view.setOnLongClickListener(this);
offLimitsTextViewItem = (TextView)view.findViewById(R.id.off_limits_item_text_view);
}
@Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public void setOnItemClickListener(ClickListener clickListener) {
OffLimitsAdapter.clickListener = clickListener;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return offLimitsItemsList.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.off_limts_item_row, parent, false);
return new OffLimitsViewHolder(itemView);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
offLimitsTextViewItem.setText("" + holder.getAdapterPosition() + ". " + offLimitsItemsList.get(position));
}
}