我正在使用Retrofit从RecyclerView
的服务器中加载数据。我已成功实施了Get
和Post
改进方法,但我在Put
和Delete
中遇到了一些问题。由于ReclyclerView
需要一个Adapter
来加载数据,我需要得到我点击的行的位置,我必须在我的Adapter
内实现onKeyPressed
(因为我使用和EditText)。从适配器中的活动调用方法
问题是,调用我的Interceptor
,改造呼叫和Everything的方法在我的Activity
中。 所以我决定从我的适配器中的这个活动调用方法来执行单个项目的放置和删除。但我得到一个
java.lang.ClassCastException说方法不能被铸造 到适配器。
这是onKeyPressed方法,ViewCategoryActivity对我的活动和SendNetworkRequest是我的方法:
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;
}
});
不这样做。使用回调代替 –
哪种方法无法投射? – Vyacheslav
发布您的调用堆栈 – Vyacheslav