在适配器中从 Activity 调用方法

Calling a method from Activity in Adapter

我正在使用 Retrofit 在 RecyclerView 中从我的服务器加载数据。我已经成功实施了 GetPost 改造方法,但我面临 PutDelete 方法的一些问题。由于 ReclyclerView 需要 Adapter 来加载数据,并且我需要获取我正在单击的行的位置,因此我必须实现 onKeyPressed (因为我正在使用和 EditText ) 在我的 Adapter.

里面

问题是调用我的 Interceptor 的方法、Retrofit 调用和 Everything 在我的 Activity 中。 所以我决定在我的适配器中调用此 Activity 的方法来执行单个项目的放置和删除。但是我收到

java.lang.ClassCastException saying that the method cannot be casted to the Adapter.

这是 onKeyPressed 方法,ViewCategoryActivity 是我的 Activity,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;
            }
        });

您应该将侦听器从 activity 传递到适配器。然后您应该使用该侦听器来调用 activity 中的函数。即,如果您像下面这样调用您的适配器,

adapter = new DataAdapter(data);

其中数据可以是数组列表(假设),然后像这样传递上下文

adapter = new DataAdapter(data,ViewCategoriesActivity.this);

在您的适配器中使用构造函数接受此侦听器,然后调用您的 activity 函数,例如 listener.yourFunction()

你可以很容易地使用监听器来完成,尽量不要让你的代码那么复杂。据我所知,你的问题有很好的答案 here,希望它能奏效。