I have some problems with RecyclerView Adapter. I want to do the list with id index: 1,2,3... When an item is removed it should set every next item position to position-1. Like this: (for example delete number 3)
1|2|3|4|5 -> 1|2|3|4
It works perfectly when:
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
But this causes other errors with notifyItemRemoved()
.
When I set:
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
return 0;
}
It has problems with item order after scrolling, with repeatability of numbers etc.
I'm stuck.
MainActivity.class
public class MainActivity extends AppCompatActivity {
private DatabaseHelper databaseHelper;
private EditText editText;
private Button showButton,saveButton;
private RecyclerView mainListRecyclerView;
private ArrayList<String>arrayList;
private MainListAdapter mainListAdapter;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
showButton = (Button) findViewById(R.id.showButton);
saveButton = (Button) findViewById(R.id.saveButton);
mainListRecyclerView = (RecyclerView) findViewById(R.id.listView);
databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>(20);
layoutManager = new LinearLayoutManager(this);
mainListRecyclerView.setLayoutManager(layoutManager);
mainListAdapter = new MainListAdapter(arrayList,this);
mainListAdapter.setHasStableIds(false);
mainListRecyclerView.setAdapter(mainListAdapter);
mainListAdapter.notifyDataSetChanged();
showButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FinalListActivity.class);
intent.putExtra("productList",arrayList);
MainActivity.this.startActivity(intent);
}
});
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE) {
addItem();
}
return true;
}
});
}
public void addData(String newItem){
boolean insertData = databaseHelper.addData(newItem);
if(insertData){
toastMessage("Item successfully inserted!");
}
else {
toastMessage("Something is wrong");
}
}
public void removeData(String itemToRemove){
boolean removeData = databaseHelper.removeData(itemToRemove);
if(removeData){
toastMessage("Item successfully deleted!");
}
else {
toastMessage("Something is wrong");
}
}
public void toastMessage(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
public void addItem(){
String newItem = editText.getText().toString();
if(newItem.length()!=0) {
addData(newItem);
arrayList.add(newItem);
editText.setText("");
editText.requestFocus();
mainListAdapter.notifyItemInserted(arrayList.size()-1);
}
else{
toastMessage("You must put something in the text field");
}
}
public void removeItem(int position){
if(mainListAdapter.getItemCount()!=0){
removeData(arrayList.get(position));
arrayList.remove(position);
mainListAdapter.notifyItemRemoved(position);
}
}
}
Adapter class
public class MainListAdapter extends
RecyclerView.Adapter<MainListAdapter.MyViewHolder> {
private ArrayList<String> mDataset;
private MainActivity mainActivity;
public MainListAdapter(ArrayList<String> mDataset, MainActivity
mainActivity) {
this.mDataset = mDataset;
this.mainActivity = mainActivity;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public void onBindViewHolder(final MyViewHolder holder,final int position) {
holder.listTextView1.setText(holder.getAdapterPosition()+"");
holder.listTextView2.setText(mDataset.get(position));
holder.listButtonViewDel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int currentPosition = holder.getAdapterPosition();
if(currentPosition!=-1)
mainActivity.removeItem(currentPosition);
}
});
}
public class MyViewHolder extends RecyclerView.ViewHolder {
final public TextView listTextView1;
final public TextView listTextView2;
final public Button listButtonViewDel;
public MyViewHolder(View view) {
super(view);
listTextView1 = (TextView) view.findViewById(R.id.listTextView1);
listTextView2 = (TextView) view.findViewById(R.id.listTextView2);
listButtonViewDel = (Button) view.findViewById(R.id.listButtonViewDel);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_list_layout, parent, false);
return new MyViewHolder(view);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…