无法在适配器中解析getSupportFragmentManager()

android fragment adapter android-dialogfragment

2013 观看

2回复

20 作者的声誉

我有这个适配器,但是有这样的问题:

无法解析方法'getSupportFragmentManager()'

Fragment是一个DialogFragment,类扩展AppCompatDialogFragment我可以用其他方式启动这个Fragment吗?或者有解决方案吗?

public class PostAdapterProfile extends ArrayAdapter<Post> {
private int resource;
private LayoutInflater inflater;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReferences;
SharedPreferences prefs;
private String uuid;
private boolean verifyStar;
private User currentUserPost;

public PostAdapterProfile(@NonNull Context context, int resourceId, ArrayList<Post> objects) {
    super(context, resourceId, objects);
    resource = resourceId;
    inflater = LayoutInflater.from(context);
}

public View getView(int position, View v, ViewGroup parent) {
    if (v == null) {
        Log.d("DEBUG", "Inflating view");
        v = inflater.inflate(R.layout.list_element, null);
    }

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDatabaseReferences = mFirebaseDatabase.getReference();

    prefs = PreferenceManager.getDefaultSharedPreferences(v.getContext());
    uuid = prefs.getString("RECORDMAN1", "");
    CurrentUser c = (CurrentUser)getContext().getApplicationContext();
    currentUserPost = c.getUserPost();

    final Post p = getItem(position);

    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("postId", p.getId());

    Log.d("DEBUG", "contact c=" + p);
    final Button nameButton;
    TextView testoView, dataView;
    ImageView profile_pic;
    final ImageView imagePost;
    final Button likeButtonGrey, likeButtonOk, numberLike, commentaButton;// redHeart, blackHeart;
    ImageButton xButton;

    xButton = (ImageButton) v.findViewById(R.id.deleteButton);
    xButton.setVisibility(View.VISIBLE);
    xButton.setBackgroundResource(R.mipmap.x);
    xButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            FragmentEliminaPost fragmentEliminaPost = new FragmentEliminaPost();
            fragmentEliminaPost.show(manager, "ahahahaha");
        }
    });
作者: a.sarto 的来源 发布者: 2018 年 1 月 8 日

回应 2


0

5273 作者的声誉

这是因为getSupportFragmentManager();不是一种方法ArrayAdapter。您只能从活动或片段中调用它。我不建议直接从适配器交换片段。这种逻辑应该在其他地方完成。

例如,回调你的片段并在那里完成工作:

  public void onClick(View view) {
        ((MyActivityOrFragment)context).myMethod()
    }

...然后在片段/活动中

public void myMethod(){

 android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        FragmentEliminaPost fragmentEliminaPost = new FragmentEliminaPost();
        fragmentEliminaPost.show(manager, "ahahahaha");

}
作者: Speed 发布者: 2018 年 1 月 8 日

1

10014 作者的声誉

getSupportFragmentManager()是的方法AppCompatActivity不是ArrayAdapter
如果要使用它,可以按活动上下文使用它。为你的情况。

  ((AppCompatActivity)context).getSupportFragmentManager();
作者: ADM 发布者: 2018 年 1 月 8 日
32x32