将自定义适配器从自身传递给方法

Pass customadapter from itself to method

我得到了一个扩展 BaseAdapter 的 customAdapter。在这个 customAdapter 中,我从另一个 class 调用了一个方法,它恰好需要这个 customAdapter 作为参数。所以基本上我想实现这个:

public class customAdapter extends BaseAdapter {

    public View getView(final int i, View view, ViewGroup viewGroup) {

        className.someMethod(arg1, arg2, this customAdapter);

    }

}

这背后的原因是,在方法内部更新了包含此 customAdapter 数据的数组,更新后我想调用

notifiyDataSetChanged();

在适配器上。

我也试过return上述方法中的数组,然后调用notifiyDataSetChanged();在 customAdapter class 中,但数组有点乱。

在这个 activity 中是我要填充数据的 listView:

public class activity_1_5_friendslist extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    @Override protected void onCreate(Bundle savedInstanceState) {

       customFriendsListAdapter= new customAdapter_CustomFriendsListAdapter(activity_1_5_friendslist.this,friendsList);
       friendsListListView = findViewById(R.id.friendsListListView);
       friendsListListView.setAdapter(customFriendsListAdapter);

       // API-Call to get data
       API.getFriends(activity_1_5_friendslist.this, preFriendsList, friendsList, friendsListProgressBar, friendsListTextView, customFriendsListAdapter);

   }
}

listView 的每一行都有按钮,例如"declineFriends"。在我的 customAdapter 中,我得到以下内容:

public class customAdapter_CustomFriendsListAdapter extends BaseAdapter {

     customAdapter_CustomFriendsListAdapter adapter;

     public customAdapter_CustomFriendsListAdapter(Context c, ArrayList<Object> friendsList){

     this.friendsList=friendsList;
     friendsListInflater= (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     context=c;

     }

    @Override public View getView(final int i, View view, ViewGroup viewGroup) {

        holder.declineFriendImageButton.setOnClickListener(new View.OnClickListener() {

        @Override public void onClick(View view) {

            // make call that friend reqeust was declined, delete friendObject from arrayList and refresh adapter
            API.declineFriend(context, pubKey, friendsList, i, adapter);

        }
    }
}

这是拒绝好友的方法:

public void declineFriend(Context context, String pubKey, final ArrayList<Object> friendsList, final int i, final customAdapter_CustomFriendsListAdapter adapter){

    requestQueue= Volley.newRequestQueue(context);

    String userID = getUserId();

    url = "someURL";

    Log.d(TAG, "declineFriend: "+i);

    StringRequest dr = new StringRequest(Request.Method.DELETE, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {

                    friendsList.remove(i);
                    adapter.notifyDataSetChanged();
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error.

                }
            }
    );
    requestQueue.add(dr);
}

在适配器的方法中使用这个 this 而不是 adapter 不起作用,因为我在这个 onClickListener 中,这给了我 listener 而不是adapter.

which needs exactly this customAdapter as an argument.

Using this this instead of the adapter in the method in the adapter doesnt work, since Im in this onClickListener and this gives me the listener instead of the adapter

你刚刚通过this。更具体地说 CustomAdapter.this

或者去掉那里的参数,传给className的构造函数


但是,如果你想做这样的事情,你应该使用回调接口而不是将你的适配器耦合到外部 class。

大多数 Android 操作已经在使用回调,即使您没有意识到。单击按钮和 Volley 响应只是您已经在使用的两个示例。

要创建自己的 ID,您需要定义一个接口,该接口接受已删除的 ID 作为参数。然后,当您调用外部方法时,它会通知适配器删除该元素。然后适配器知道它需要更新自己。

public class CustomAdapter<E> extends ArrayAdapter<E> {

    public static class OnUpdateListener {
       void onRemove(String id);
    }

    public View getView(final int i, View view, ViewGroup viewGroup) {

        className.someMethod(arg1, arg2, new CustomAdapter.OnUpdateListener() {
            @Override public void onRemove(String id) {
                remove(id);
            }
        });

    }

}

而在另一个class

void someMethod(Object arg1, Object arg2, final CustomAdapter.OnUpdateListener callback) {
    // Do some things
    // Get some data
    String userID = getUserId();
    // notify back to the adapter
    callback.onRemove(userID);
}