Я пытаюсь передать значение выбранной строки следующему фрагменту, но при нажатии на элемент списка получаю сообщение об ошибке 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;
}
});