在我的申请中,我有2 recyclerview
个活动。
我希望何时在一个recyclerview', add this item into other
recyclerview`中添加项目。
在recyclerViews
用于将数据发送到活动的情况下,我使用了Interface
。
但是当点击recyclerview
中的项目时,两个显示 Logcat 中的错误。
活动代码:
public class SuggestFilmActivity extends AppCompatActivity implements SuggestedListener, SuggestedDeleteListener {
@BindView(R.id.toolbarTitleTxt)
TextView toolbarTitleTxt;
@BindView(R.id.suggestFilm_searchEditText)
EditText suggestFilm_searchEditText;
@BindView(R.id.suggestFilm_searchBtn)
ImageView suggestFilm_searchBtn;
@BindView(R.id.suggestFilm_clearBtn)
ImageView suggestFilm_clearBtn;
@BindView(R.id.suggestFilm_recyclerView)
RecyclerView suggestFilm_recyclerView;
@BindView(R.id.suggestFilm_recyclerViewProgress)
ProgressBar suggestFilm_recyclerViewProgress;
@BindView(R.id.suggestFilm_noDataText)
TextView suggestFilm_noDataText;
@BindView(R.id.newsPageLoadLay)
RelativeLayout newsPageLoadLay;
@BindView(R.id.suggestFilm_recyclerViewSendUser)
RecyclerView suggestFilm_recyclerViewSendUser;
@BindView(R.id.suggestFilm_buttonsSendCard)
CardView suggestFilm_buttonsSendCard;
private Context context;
private SuggestFilmAdapter suggestFilmAdapter;
private List<Result> model = new ArrayList<>();
private ArrayList<SuggestFilmAddUser> suggestFilmAddUserList = new ArrayList<>();
private SuggestFilmUserAdapter suggestFilmUserAdapter;
private InterfaceApi api;
private SharedPrefrencesHandler prefrencesHandler;
private String token;
private GridLayoutManager gridLayoutManager;
private LinearLayoutManager linearLayoutManager;
private List<Integer> usersListSend = new ArrayList<>();
private SuggestFilmAddUser suggestFilmAddUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suggest_film);
//Initialize
ButterKnife.bind(this);
context = this;
prefrencesHandler = new SharedPrefrencesHandler(context);
api = ApiClient.getClient().create(InterfaceApi.class);
suggestFilmAdapter = new SuggestFilmAdapter(context, model, this);
gridLayoutManager = new GridLayoutManager(context, 3);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
//Get token
token = prefrencesHandler.getFromShared(SharedPrefrencesKeys.TOKEN.name());
//Set toolbar title
toolbarTitleTxt.setText(context.getResources().getString(R.string.SuggestToFollowers));
//Init followers recyclerView
suggestFilm_recyclerView.setLayoutManager(gridLayoutManager);
suggestFilm_recyclerView.setHasFixedSize(true);
//Init send user recyclerView
suggestFilm_recyclerViewSendUser.setLayoutManager(linearLayoutManager);
suggestFilm_recyclerViewSendUser.setHasFixedSize(true);
//Init Send user adapter
suggestFilmUserAdapter = new SuggestFilmUserAdapter(context, suggestFilmAddUserList, this);
suggestFilm_recyclerViewSendUser.setAdapter(suggestFilmUserAdapter);
//Load more
newsPageLoadLay.setVisibility(View.GONE);
suggestFilm_recyclerView.setOnScrollListener(new EndlessRecyclerGridPage1(gridLayoutManager) {
@Override
public void onLoadMore(int current_page) {
newsPageLoadLay.setVisibility(View.VISIBLE);
Call<SeriesWhoWatchedResponse> call = api.getSuggestFilmUsers(token, filmSendData("", 10, current_page));
call.enqueue(new Callback<SeriesWhoWatchedResponse>() {
@Override
public void onResponse(Call<SeriesWhoWatchedResponse> call, Response<SeriesWhoWatchedResponse> response) {
if (response.body().getData() != null && response.body().getStatusCode() != 401
&& response.body().getStatusCode() != 402) {
if (response.body().getData().getResult().size() > 0) {
suggestFilmAdapter.addNewItem(response.body().getData().getResult());
//Gone no explore
newsPageLoadLay.setVisibility(View.GONE);
}
} else {
prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
startActivity(new Intent(context, LoginActivity.class));
}
newsPageLoadLay.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<SeriesWhoWatchedResponse> call, Throwable t) {
newsPageLoadLay.setVisibility(View.GONE);
}
});
}
});
//Get user data
getUserData("", 1);
}
private void getUserData(String search, int page) {
suggestFilm_recyclerViewProgress.setVisibility(View.VISIBLE);
Call<SeriesWhoWatchedResponse> call = api.getSuggestFilmUsers(token, filmSendData(search, 10, page));
call.enqueue(new Callback<SeriesWhoWatchedResponse>() {
@Override
public void onResponse(Call<SeriesWhoWatchedResponse> call, Response<SeriesWhoWatchedResponse> response) {
if (response.body().getData() != null) {
if (response.body().getData().getResult().size() > 0) {
model.clear();
model.addAll(response.body().getData().getResult());
suggestFilmAdapter.notifyDataSetChanged();
suggestFilm_recyclerView.setAdapter(suggestFilmAdapter);
suggestFilm_noDataText.setText("");
} else {
model.clear();
suggestFilmAdapter.notifyDataSetChanged();
suggestFilm_recyclerView.setAdapter(suggestFilmAdapter);
suggestFilm_noDataText.setText(context.getResources().getString(R.string.noFollowersYet));
}
}
suggestFilm_recyclerViewProgress.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<SeriesWhoWatchedResponse> call, Throwable t) {
suggestFilm_recyclerViewProgress.setVisibility(View.GONE);
}
});
}
private SuggestFilmSendData filmSendData(String search, int size, int page) {
SuggestFilmSendData sendData = new SuggestFilmSendData();
sendData.setKeyword(search);
sendData.setPageIndex(page);
sendData.setPageSize(size);
return sendData;
}
@Override
public void onSend(int Id, String name, String image) {
suggestFilmAddUser = new SuggestFilmAddUser();
suggestFilmAddUser.setId(Id);
suggestFilmAddUser.setName(name);
suggestFilmAddUser.setImage(image);
//Add to list only once item
for (SuggestFilmAddUser idList : suggestFilmAddUserList) {
if (idList.getId() == suggestFilmAddUser.getId()) {
return;
}
}
suggestFilmAddUserList.add(suggestFilmAddUser);
if (suggestFilmAddUserList.size() > 0) {
suggestFilm_buttonsSendCard.setCardBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
suggestFilm_buttonsSendCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSuggestList();
}
});
}
suggestFilmUserAdapter.notifyItemInserted(suggestFilmAddUserList.size() - 1);
suggestFilm_recyclerViewSendUser.setVisibility(View.VISIBLE);
for (SuggestFilmAddUser suggest : suggestFilmAddUserList) {
if (suggest.getId() == suggestFilmAddUser.getId()) {
Log.e("SuggestLog", suggest.getId() + "");
usersListSend.add(suggest.getId());
return;
}
}
}
@Override
public void onDelete(int Id) {
if (usersListSend.size() > 0) {
usersListSend.remove(Id);
}
Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show();
}
private void getSuggestList() {
for (SuggestFilmAddUser suggest : suggestFilmAddUserList) {
if (suggest.getId() == suggestFilmAddUser.getId()) {
Toast.makeText(context, "" + usersListSend, Toast.LENGTH_SHORT).show();
return;
}
}
}
添加用户适配器:
public class SuggestFilmAdapter extends RecyclerView.Adapter<SuggestFilmAdapter.ViewHolder> {
private Context context;
private List<Result> model;
private SuggestedListener suggestedListener;
public SuggestFilmAdapter(Context context, List<Result> model, SuggestedListener suggestedListener) {
this.context = context;
this.model = model;
this.suggestedListener = suggestedListener;
}
@Override
public SuggestFilmAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_suggest_film_users_followers, parent, false);
return new SuggestFilmAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final SuggestFilmAdapter.ViewHolder holder, final int position) {
//Name
holder.row_suggestFilmProfileName.setText(model.get(position).getName());
//Image
Glide.with(context)
.load(model.get(position).getImageUrl())
.asBitmap()
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.override(300, 300)
.into(new BitmapImageViewTarget(holder.row_suggestFilmProfileImage) {
@Override
protected void setResource(Bitmap resource) {
if (context == null) return;
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
holder.row_suggestFilmProfileImage.setImageDrawable(circularBitmapDrawable);
}
});
//Is Mutual
if (model.get(position).getIsMutual()) {
holder.row_suggestFilmIsOk.setVisibility(View.VISIBLE);
} else {
holder.row_suggestFilmIsOk.setVisibility(View.GONE);
}
holder.row_suggestedLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
suggestedListener.onSend(model.get(position).getUserId(),
model.get(position).getName(),
model.get(position).getImageUrl());
}
});
}
@Override
public int getItemCount() {
return model.size();
}
public void addNewItem(List<Result> newContent) {
int start = this.model.size();
int end = newContent.size();
model.addAll(newContent);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView row_suggestFilmProfileImage, row_suggestFilmIsOk;
private TextView row_suggestFilmProfileName;
private RelativeLayout row_suggestedLay;
public ViewHolder(View itemView) {
super(itemView);
row_suggestFilmProfileImage = (ImageView) itemView.findViewById(R.id.row_suggestFilmProfileImage);
row_suggestFilmIsOk = (ImageView) itemView.findViewById(R.id.row_suggestFilmIsOk);
row_suggestFilmProfileName = (TextView) itemView.findViewById(R.id.row_suggestFilmProfileName);
row_suggestedLay = (RelativeLayout) itemView.findViewById(R.id.row_suggestedLay);
}
}
}
删除用户适配器:
public class SuggestFilmUserAdapter extends RecyclerView.Adapter<SuggestFilmUserAdapter.ViewHolder> {
private Context context;
private ArrayList<SuggestFilmAddUser> model;
private SuggestedDeleteListener deleteListener;
public SuggestFilmUserAdapter(Context context, ArrayList<SuggestFilmAddUser> model, SuggestedDeleteListener deleteListener) {
this.context = context;
this.model = model;
this.deleteListener = deleteListener;
}
@Override
public SuggestFilmUserAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_suggest_film_users_add, parent, false);
return new SuggestFilmUserAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final SuggestFilmUserAdapter.ViewHolder holder, final int position) {
//Name
holder.rowSuggestAddUser_name.setText(model.get(position).getName());
//Image
Glide.with(context)
.load(model.get(position).getImage())
.asBitmap()
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.override(300, 300)
.into(new BitmapImageViewTarget(holder.rowSuggestAddUser_image) {
@Override
protected void setResource(Bitmap resource) {
if (context == null) return;
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
holder.rowSuggestAddUser_image.setImageDrawable(circularBitmapDrawable);
}
});
//Cancel btn
holder.rowSuggestAddUser_cancelImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteItem(position);
deleteListener.onDelete(model.get(position).getId());
}
});
}
private void deleteItem(int position) {
model.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, model.size());
}
@Override
public int getItemCount() {
return model.size();
}
public void addNewItem(ArrayList<SuggestFilmAddUser> newContent) {
int start = this.model.size();
int end = newContent.size();
model.addAll(newContent);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView rowSuggestAddUser_image, rowSuggestAddUser_cancelImage;
private TextView rowSuggestAddUser_name;
private RelativeLayout rowSuggestAddUser_lay;
public ViewHolder(View itemView) {
super(itemView);
rowSuggestAddUser_image = (ImageView) itemView.findViewById(R.id.rowSuggestAddUser_image);
rowSuggestAddUser_cancelImage = (ImageView) itemView.findViewById(R.id.rowSuggestAddUser_cancelImage);
rowSuggestAddUser_name = (TextView) itemView.findViewById(R.id.rowSuggestAddUser_name);
rowSuggestAddUser_lay = (RelativeLayout) itemView.findViewById(R.id.rowSuggestAddUser_lay);
}
}
}
LogCat错误:
FATAL EXCEPTION: main
Process: com.app.test, PID: 28516
java.lang.IndexOutOfBoundsException: Invalid index 18, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.remove(ArrayList.java:403)
at com.app.test.Activities.SuggestFilmActivity.onDelete(SuggestFilmActivity.java:222)
at com.app.test.Adapters.SuggestFilm.SuggestFilmUserAdapter$2.onClick(SuggestFilmUserAdapter.java:76)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
在onDelete中显示此行的错误:
usersListSend.remove(Id);
请帮助我,我很业余,真的需要这个帮助。请 。谢谢,全部&lt; 3
答案 0 :(得分:1)
根据异常消息:
java.lang.IndexOutOfBoundsException:索引18无效,大小为2 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.remove(ArrayList.java:403) 在com.app.test.Activities.SuggestFilmActivity.onDelete(SuggestFilmActivity.java:222)
您正在删除ArrayList中索引为18且大小为2的元素。此异常发生在第222行的SuggestFilmActivity类中的onDelete方法中。所以,根据你的代码我们在那里:
public void onDelete(int Id) {
if (usersListSend.size() > 0) {
usersListSend.remove(Id);
}
Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show();
}
要解决这个问题,我建议做的是:
public void onDelete(int id) {
if (id > -1 && id < usersListSend.size()) {
usersListSend.remove(id);
}
Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
}
id值必须大于-1且小于usersListSend.size()
但问题的原因来自这里(来自异常消息):
在 com.app.test.Adapters.SuggestFilm.SuggestFilmUserAdapter $ 2.onClick(SuggestFilmUserAdapter.java:76)
转到 SuggestFilmUserAdapter 类,找到第76行,看看那里发生了什么,以彻底解决此问题。
在SuggestFilmUserAdapter类中更改此代码:
//Cancel btn
holder.rowSuggestAddUser_cancelImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteItem(position);
deleteListener.onDelete(model.get(position).getId());
}
});
对此:
//Cancel btn
holder.rowSuggestAddUser_cancelImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = model.get(position).getId();
deleteItem(position);
deleteListener.onDelete(id);
}
});
并修改此方法:
private void deleteItem(int position) {
model.remove(position);
notifyItemRemoved(position);
//notifyItemRangeChanged(position, model.size()); // you notified already and no need to do it again I think
}
通过方法void onDelete(int Id)中的SuggestFilmActivity类中的ID删除SuggestFilmAddUser:
@Override
public void onDelete(int Id) {
if (usersListSend.size() > 0) usersListSend.remove(Integer.valueOf(Id)); // removing by Integer value not index!
Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
最简单的解决方案是将Id
String
存储在ArrayList<String>
中,但我建议您详细了解ArrayList和Collections
答案 2 :(得分:0)
用整数替换int
public void onDelete(Integer Id) {
if (usersListSend.size() > 0) {
usersListSend.remove(Id);
}
Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show(); }