I am using Retrofit to load data from my server in a RecyclerView
. I have successfully implement the Get
, and Post
Retrofit methods but I am facing some problems for the Put
and Delete
ones. Since ReclyclerView
requires an Adapter
to load the data, and I need to get the position of the row that I am clicking, I had to implement the onKeyPressed
(because I am using and EditText) inside my Adapter
.
The problem is that the method that calls my Interceptor
, the Retrofit call and Everything is in my Activity
.
So i decided to call the method from this Activity inside my Adapter to do the Put and Delete of a singular item. But I am getting a
java.lang.ClassCastException saying that the method cannot be casted to the Adapter.
This is the onKeyPressed method, ViewCategoryActivity si my Activity and SendNetworkRequest is my method:
holder.nameCategory.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
System.out.println("Entrou no adapter!!");
Category2 category = new Category2();
category.setName(holder.nameCategory.getText().toString());
((ViewCategoriesActivity)mContext).SendNetworkRequest(categoryList.get(position).getProjectId(), 2, categoryList.get(position).get_id(), new SendCategory(holder.nameCategory.getText().toString()));
//Variable to check if the category inserted is equal to a previous one
int bool = 0;
//The for goes through the list and tries to find a category with the name equal to the one inserted
for (int i = 0; i < categoryList.size(); i++){
//if it finds equal names bool = 1
if (categoryList.get(i).getName().equals(holder.nameCategory.getText().toString())){
bool = 1;
Toast.makeText(mContext, mContext.getResources().getString(R.string.category_different_name),
Toast.LENGTH_LONG).show();
}
}
//There's no category with the same name so it' OK so insert a new one
if (bool == 0){
if(mContext instanceof ViewCategoriesActivity){
((ViewCategoriesActivity)mContext).SendNetworkRequest(categoryList.get(position).getProjectId(), 2, categoryList.get(position).get_id(), new SendCategory(holder.nameCategory.getText().toString()));
}
//categoryList.add(category);
} else {
}
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(holder.nameCategory.getWindowToken(), 0);
return true;
}
return false;
}
});