我有这个适配器,但是存在这样的问题:
无法解决方法' 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");
}
});
答案 0 :(得分:1)
getSupportFragmentManager()
是AppCompatActivity
而非ArrayAdapter
的方法
如果要使用它,可以按活动上下文使用它。为你的情况。
((AppCompatActivity)context).getSupportFragmentManager();
答案 1 :(得分:0)
这是因为getSupportFragmentManager();
不是ArrayAdapter
的方法。您只能从活动或片段中调用它。我不建议直接从适配器交换片段。这种逻辑应该在其他地方完成。
E.g。回到你的片段并在那里做工作:
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");
}