简体   繁体   中英

Calling a method from Activity in Adapter

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;
            }
        });

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

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

Accept this listener using a constructor in your adapter and then call your activity function like 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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