Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

android - Problem with indexing in RecyclerView Adapter. How to do it in a good way?

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);
}

}

Image which can help understand it

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this lines To removeItem function in MainActivity.java

public void removeItem(int position){
        if(mainListAdapter.getItemCount()!=0){
            removeData(arrayList.get(position));
            arrayList.remove(position);
            mainListAdapter.notifyItemRemoved(position);
            arrayList.remove(position); // add this line
            mainListAdapter.update(arrayList); // add this line
        }
    }

And add update function to your MainListAdapter.java like this

public void update(ArrayList<String> mDataset){
     this.mDataset = mDataset
     notifyDataSetChanged();
}

For show animation try this function (I suggest you to set all function about adapter in adapter)

public void update(ArrayList<String> mDataset,int position){
         this.mDataset = mDataset
         notifyItemRangeChanged(position,mDataset.size);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...