简体   繁体   English

从适配器的活动中调用方法

[英]Calling a method from Activity in Adapter

I am using Retrofit to load data from my server in a RecyclerView . 我正在使用Retrofit在RecyclerView从服务器加载数据。 I have successfully implement the Get , and Post Retrofit methods but I am facing some problems for the Put and Delete ones. 我已经成功实现了GetPost Retrofit方法,但对于PutDelete方法却遇到了一些问题。 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 . 由于ReclyclerView需要Adapter来加载数据,并且我需要获取要单击的行的位置,因此必须在Adapter内部实现onKeyPressed (因为我正在使用和EditText)。

The problem is that the method that calls my Interceptor , the Retrofit call and Everything is in my Activity . 问题在于,调用我的Interceptor ,改造调用和所有内容的方法都在我的Activity So i decided to call the method from this Activity inside my Adapter to do the Put and Delete of a singular item. 因此,我决定从我的适配器内部的Activity中调用该方法,以执行单个项目的放置和删除操作。 But I am getting a 但是我得到了

java.lang.ClassCastException saying that the method cannot be casted to the Adapter. java.lang.ClassCastException表示无法将方法强制转换为Adapter。

This is the onKeyPressed method, ViewCategoryActivity si my Activity and SendNetworkRequest is my method: 这是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;
            }
        });

You should be passing a listener from your activity to the adapter. 您应该将监听器从您的活动传递到适配器。 You should be then using that listener to call the function in your activity. 然后,您应该使用该侦听器在您的活动中调用该函数。 ie, if you are calling your adapter like the following, 即,如果您像以下那样调用适配器,

adapter = new DataAdapter(data);

where data can be an arraylist(suppose), then pass the context like this 数据可以是arraylist(假设),然后像这样传递上下文

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

Accept this listener using a constructor in your adapter and then call your activity function like listener.yourFunction() 使用适配器中的构造函数接受此侦听器,然后调用诸如listener.yourFunction()之类的活动函数

You can do it using the listener easily, try not to make your code that complex. 您可以使用侦听器轻松地做到这一点,请尽量不要使您的代码那么复杂。 As per i know your problem has good answer here , hope it will work. 据我所知,您的问题在这里有很好的答案,希望它能解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM