我试了好几个小时但我不能让它工作。请帮帮我。
尝试:
当从导航抽屉切换到第2部分时,方法" removeStuff"被调用,应该检查并删除每个" mRecyclerView"项目从列表到" removedItems"数组,所以可以回调。将来我会设置一个if语句,以便"匹配"数组被删除。
问题:
非常奇怪。当我在" myData"中的每个数组中有7个元素时,当" removeStuff"被调用,视图中仍有4个项目。当有8个元素时,删除后仍然有5个元素。然而,当逐个删除项目而不使用循环时,一切都很完美,除了我无法逐个删除所有内容
请引导我解决我在代码中犯的错误:(如果需要的话,我几乎附上了整个代码)
//I added the entire script which is relevant to RecyclerView.
//Scroll down to go to the part I have error with
private static RecyclerView.LayoutManager mLayoutManager;
private static RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private static ArrayList < Integer > removedItems;
private static ArrayList < MatchData > matches;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyData.UpdateTiming();
myOnClickListener = new MyOnClickListener(this);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
matches = new ArrayList < > ();
for (int i = 0; i < MyData.matchArray.length; i++) {
matches.add(new MatchData(
MyData.matchArray[i],
MyData.team1Array[i],
MyData.team2Array[i],
MyData.id_[i],
MyData.venueArray[i],
MyData.timingArray[i],
MyData.winnerArray[i],
MyData.scoreArray[i]
));
}
removedItems = new ArrayList < > ();
mAdapter = new MyAdapter(matches);
mRecyclerView.setAdapter(mAdapter);
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
removeStuff();
break;
}
}
//ERROR FROM HERE:
public void removeStuff() {
int length = MyData.dates.length;
for (int i = 0; i < length; i++) {
View child = mRecyclerView.getChildAt(i);
if (child != null) {
RecyclerView.ViewHolder mViewHolder = mRecyclerView.findViewHolderForPosition(i);
TextView textViewName = (TextView) mViewHolder.itemView.findViewById(R.id.textViewMatch);
String selectedName = (String) textViewName.getText();
int selectedItemId = -1;
for (int j = 0; j < MyData.matchArray.length; j++)
if (selectedName.equals(MyData.matchArray[j])) {
selectedItemId = MyData.id_[j];
}
removedItems.add(selectedItemId);
matches.remove(i);
mAdapter.notifyItemRemoved(i);
}
}
}
&#13;
虽然我有警告,但我没有收到错误。
提前致谢。
编辑:我选择了什么:
static class MyOnClickListener implements View.OnClickListener {
private final Context context;
private MyOnClickListener(Context context) {
this.context = context;
}
public void onClick(View v) {
removeItem(v);
}
private static void removeItem(View v) {
int selectedItemPosition = mRecyclerView.getChildPosition(v);
RecyclerView.ViewHolder mViewHolder = mRecyclerView.findViewHolderForPosition(selectedItemPosition);
TextView textViewName = (TextView) mViewHolder.itemView.findViewById(R.id.textViewMatch);
String selectedName = (String) textViewName.getText();
int selectedItemId = -1;
for (int i = 0; i < MyData.matchArray.length; i++) {
if (selectedName.equals(MyData.matchArray[i])) {
selectedItemId = MyData.id_[i];
}
}
removedItems.add(selectedItemId);
matches.remove(selectedItemPosition);
mAdapter.notifyItemRemoved(selectedItemPosition);
}
}
&#13;