我是android编程的新手,我想在我目前面临的问题上得到帮助:基本上我有一个包含4张卡的回收站视图,可以通过滑动删除。
我还有一个浮动操作按钮,目的是重新添加最终删除的卡片。
它显示一个警告对话框,列出了arraylist的项目,这些项目引用了已被删除的卡片。
第一次点击按钮时按钮完全正常。
Screenshot。在这里,我删除了显卡和键盘卡,它实际上在警告对话框中显示了这两个项目。
但问题出现在以下点击中。
Screenshot_2。这些项目只是在对话框列表中添加(即使据我所知,运行指令arraylist.add给出相同的索引应始终覆盖该项目。)
如果有人能帮我解决这个问题,我真的很感激。
或者甚至是一种全新的方法,如果我的根本无法修复。
如果您还需要了解其他信息,请告诉我。
基本上我只需要一种方法来重新添加最终被删除的recyclerview卡。谢谢。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
ArrayList<String> title = new ArrayList<>();
private String[] button = {"choose os", "choose resolution", "choose type", "choose type"};
int initialSizeRows;
RelativeLayout relativeLayout;
private ArrayList<String> title2 = new ArrayList<>();
boolean os=false, monitor=false, keyboard=false, mouse=false, first_time=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new RecyclerAdapter(title,button);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
title.addAll(Arrays.asList("OS", "Monitor", "Keyboard", "Mouse"));
relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
//adapter for arraylist item2 to be set in the alert dialog
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, title2);
//fab
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
//fab onclicklistener
public void onClick(View view) {
if (title.size() == 4) {
//if no card has been deleted
Snackbar.make(relativeLayout, "Nothing to add!", Snackbar.LENGTH_SHORT).show();
} else {
//check which card/s have been deleted and add them to the arraylist "title2"
//the if(s) purposes is to always add the values in the correct indexes
if (!title.contains("OS")) {
title2.add(0, " OS");
os = true;
}
if (!title.contains("Monitor")) {
if (os) title2.add(1, " Monitor");
else title2.add(0, " Monitor");
monitor = true;
}
if (!title.contains("Keyboard")) {
if (os && monitor) title2.add(2, " Keyboard");
else if (!os && !monitor) title2.add(0, " Keyboard");
else if (!os ^ !monitor) title2.add(1, " Keyboard");
keyboard = true;
}
if (!title.contains("Mouse")) {
if (os && monitor && keyboard) title2.add(3, " Mouse");
else if (!os && !monitor && !keyboard) title2.add(0, " Mouse");
else if (!os && (monitor ^ keyboard)) title2.add(1, " Mouse");
else if (!os && (monitor && keyboard)) title2.add(2, " Mouse");
else if (os && (!monitor && !keyboard)) title2.add(1, " Mouse");
else if (os && (monitor ^ keyboard)) title2.add(2, " Mouse");
}
//create alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//set adapter with arraylist just populated above
builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//to be done
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
});
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
title.remove(viewHolder.getAdapterPosition());
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>{
ArrayList<String> title;
String[] button;
public RecyclerAdapter(ArrayList<String> title, String[] button){
this.title = title;
this.button = button;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.titleTextView.setText(title.get(position));
holder.buttonTextView.setText(button[position]);
}
@Override
public int getItemCount() {
return title.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView titleTextView, buttonTextView;
public RecyclerViewHolder(View itemView) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.title);
buttonTextView = (TextView) itemView.findViewById(R.id.button);
}
}}
答案 0 :(得分:0)
如果要更换项目,则需要使用title2.set(0, " OS");
。添加将在列表中每次添加项目。 Set用于替换List中的项目。