【问题标题】:Cannot resolve getSupportFragmentManager() in adapter无法解析适配器中的 getSupportFragmentManager()
【发布时间】:2018-01-08 12:29:09
【问题描述】:

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

无法解析方法“getSupportFragmentManager()”

Fragment 是一个 DialogFragment 并且该类扩展了 AppCompatDialogFragment 我可以用其他方式开始这个片段吗?或者有什么解决办法?

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

【问题讨论】:

    标签: android fragment adapter android-dialogfragment


    【解决方案1】:

    getSupportFragmentManager() 是 AppCompatActivity 不是 ArrayAdapter 的方法。
    如果你想使用它,你可以通过活动上下文来使用它。适合你的情况。

      ((AppCompatActivity)context).getSupportFragmentManager();
    

    【讨论】:

      【解决方案2】:

      这是因为getSupportFragmentManager(); 不是ArrayAdapter 的方法。您只能从 Activity 或 Fragment 调用它。我不建议直接从适配器交换片段。这个逻辑应该在别处完成。

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

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

      【讨论】:

        猜你喜欢
        • 2021-08-28
        • 1970-01-01
        • 2023-03-13
        • 2012-08-23
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多