簡體   English   中英

從適配器的活動中調用方法

[英]Calling a method from Activity in Adapter

我正在使用Retrofit在RecyclerView從服務器加載數據。 我已經成功實現了GetPost Retrofit方法,但對於PutDelete方法卻遇到了一些問題。 由於ReclyclerView需要Adapter來加載數據,並且我需要獲取要單擊的行的位置,因此必須在Adapter內部實現onKeyPressed (因為我正在使用和EditText)。

問題在於,調用我的Interceptor ,改造調用和所有內容的方法都在我的Activity 因此,我決定從我的適配器內部的Activity中調用該方法,以執行單個項目的放置和刪除操作。 但是我得到了

java.lang.ClassCastException表示無法將方法強制轉換為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;
            }
        });

您應該將監聽器從您的活動傳遞到適配器。 然后,您應該使用該偵聽器在您的活動中調用該函數。 即,如果您像以下那樣調用適配器,

adapter = new DataAdapter(data);

數據可以是arraylist(假設),然后像這樣傳遞上下文

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

使用適配器中的構造函數接受此偵聽器,然后調用諸如listener.yourFunction()之類的活動函數

您可以使用偵聽器輕松地做到這一點,請盡量不要使您的代碼那么復雜。 據我所知,您的問題在這里有很好的答案,希望它能解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM