我试图将选定的行值传递给下一个Fragment,但onClick在列表项上却出现错误java.lang.ArrayIndexOutOfBoundsException:length = 10; index = -1“
请在错误的地方帮助我。
这是我的适配器类文件,仅供参考,我无法找到解决该问题的地方。
public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
final Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View addressList = inflater.inflate(R.layout.soc_search_list, parent, false);
final ViewHolder viewHolder = new ViewHolder(addressList);
try {
viewHolder.searchList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.clear();
Fragment fragment = new UserReg();
final int position = viewHolder.getLayoutPosition();
address = socAddressList.get(position).getAddress();
socName = socAddressList.get(position).getSocName();
Toast.makeText(parent.getContext(),socName, Toast.LENGTH_LONG).show();
args.putString("socAddress", address);
args.putString("socName", socName);
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment.setArguments(args);
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
});
} catch (Exception e) {
}
return new ViewHolder(addressList);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Society_Address society_address = socAddressList.get(position);
String mtvSocName, mtvLandmark, mtvArea, mtvTown, mtvDistrict, mtvPincode, mtvState, mtvAddress;
TextView tvSocAddressList;
mtvAddress = society_address.getAddress();
tvSocAddressList = holder.socAddress;
holder.socAddress.setText(socAddressList.get(position).getAddress());
tvSocAddressList.setText(mtvAddress);
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView socAddress;
private LinearLayout searchList;
public ViewHolder(@NonNull View itemView) {
super(itemView);
searchList = itemView.findViewById(R.id.searchlayout);
socAddress = itemView.findViewById(R.id.tv_socName);
}
}
@Override
public int getItemCount() {
return socAddressList.size();
}
此代码将填充列表
sv_address.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(final String newText) {
if (newText.length() == 0) {
displayList();
} // This is used as if user erases the characters in the search field.
else {
list = new ArrayList<>();
final List<String> docList = new ArrayList<>();
CollectionReference mdb = db.collection("AllSocAddress");
mdb.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
list = new ArrayList<>();
List<String> docList = new ArrayList<>();
List<Society_Address> myList = task.getResult().toObjects(Society_Address.class);
myList.clear();
try{
if(task.isSuccessful()){
for (QueryDocumentSnapshot document : task.getResult()) {
docList.add(document.getId());
Society_Address society_address= document.toObject(Society_Address.class);
society_address.address = document.getString("Address");
String socName = document.getString("Society_Name");
text = newText.toUpperCase();
society_address.socName = text.replace(" ","").toUpperCase();
if(socName.contains(text)){
myList.add(society_address);
}
}
Society_Address_Adapter adapter = new Society_Address_Adapter(getActivity(),myList);
rv_address.setAdapter(adapter);
}
}
catch (Exception ex){
Toast.makeText(getActivity(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
if(adapter!= null){
adapter.notifyDataSetChanged();
}
return true;
}
});
答案 0 :(得分:1)
viewHolder.getAdapterPosition()
或ViewHolder
内部getAdapterPosition()
答案 1 :(得分:0)
请尝试使用viewHolder.getAdapterPosition()
并检查它是否大于0或相等,因为它可以在幕后情况下返回INVALID_POSITION。 viewHolder.getLayoutPosition()
与布局中的位置有关,有时与viewHolder.getAdapterPosition()
不对应。
答案 2 :(得分:0)
使用此
int position = viewHolder.getAdapterPosition();