【问题标题】:How to call getSupportFragmentManager() in ArrayAdapter如何在 ArrayAdapter 中调用 getSupportFragmentManager()
【发布时间】:2017-07-06 20:01:45
【问题描述】:

无法在此代码中从 CustomAdapter 和 setArguments(bundle) 调用 getSupportFragmentManager() 并在另一个片段中使用它。我无法在我的代码中解决这两种方法。我如何调用这些方法。请有人尝试帮助我

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class CustomGridViewAdapter extends ArrayAdapter<CategoryPost> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<CategoryPost> data = new ArrayList<CategoryPost>();
    private Fragment getquoteFrag;

    public CustomGridViewAdapter(Context context, int resource, ArrayList<CategoryPost> objects) {
        super(context, resource, objects);
        this.layoutResourceId = resource;
        this.context = context;
        this.data = objects;
    }

    static class ViewHolder {
        TextView success, message, id, name;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(layoutResourceId, parent, false);
            holder.id = (TextView)convertView.findViewById(R.id.tv_data_id);
            holder.name = (TextView)convertView.findViewById(R.id.tv_name_id);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CategoryPost post = data.get(position);
        holder.name.setText(post.getName());
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                getquoteFrag = new GetQuoteFragment();
                FragmentManager quote_fm = getSupportFragmentManager().beginTransaction();;
                FragmentTransaction quote_ft = quote_fm.beginTransaction();
                bundle.putString("pos", String.valueOf(post.getId()));
                quote_fm.setArguments(bundle);
            }
        });
        return convertView;
    }
}

【问题讨论】:

  • getSupportFragmentManagerFragmentActivity 的成员 ...所以很明显,如果你想调用它,你需要引用此类的实例
  • @Selvin 我仍然无法解决它。
  • 为什么在适配器中需要它?

标签: android fragment adapter


【解决方案1】:

试试下面的代码:

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomGridViewAdapter extends ArrayAdapter<CategoryPost> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<CategoryPost> data = new ArrayList<CategoryPost>();
    private Fragment getquoteFrag;
    public CustomGridViewAdapter(Context context, int resource, ArrayList<CategoryPost> objects) {
        super(context, resource, objects);
        this.layoutResourceId = resource;
        this.context = context;
        this.data = objects;
    }
    static class ViewHolder
    {
        TextView success, message, id, name;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(layoutResourceId, parent, false);
            holder.id = (TextView)convertView.findViewById(R.id.tv_data_id);
            holder.name = (TextView)convertView.findViewById(R.id.tv_name_id);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CategoryPost post = data.get(position);
        holder.name.setText(post.getName());
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                getquoteFrag = new GetQuoteFragment();
                FragmentManager quote_fm = ((MainActivity)context).getSupportFragmentManager().beginTransaction();;
                FragmentTransaction quote_ft = quote_fm.beginTransaction();
                bundle.putString("pos", String.valueOf(post.getId()));
                getquoteFrag.setArguments(bundle);
                quote_ft.replace(R.id.container,getquoteFrag)
                quote_ft.commit();

            }
        });
        return convertView;
    }
}

【讨论】:

  • 不兼容的类型FragmentManager quote_fm = ((MainActivity)context).getSupportFragmentManager().beginTransaction(); 并且无法解析方法quote_fm.setArguments(bundle);
  • @MukeshPrajapati 你需要使用片段对象 getquoteFrag.setArguments(bundle);
【解决方案2】:

这样称呼

FragmentManager quote_fm = context.getSupportFragmentManager().beginTransaction();

FragmentManager quote_fm = ((MainActivity)context).getSupportFragmentManager().beginTransaction();

开始片段所需的代码是

            Bundle bundle = new Bundle();
            getquoteFrag = new GetQuoteFragment();
            FragmentManager quote_fm = ((MainActivity)context).getSupportFragmentManager().beginTransaction();;
            FragmentTransaction quote_ft = quote_fm.beginTransaction();
            bundle.putString("pos", String.valueOf(post.getId()));  // you can also send this value as an integer type
            getquoteFrag.setArguments(bundle);
            quote_ft.replace(R.id.your_main_activity_container, getquoteFrag)
            quote_ft.commit();

对于下面的问题,在调用它时获取另一个片段中的值。

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        String post_id= getArguments().getString("pos");

       // you can also pass values in int type.
       int pos = getArguments().getInt("pos"); 
    }
}

【讨论】:

  • 我无法得到它。实际上,我想将post.getId() 值存储在bundle 对象中,并在GetQuoteFragment 片段中获取该值。
  • 这是不同的问题?
  • 否,但添加FragmentManager quote_fm = ((GetQuoteFragment)context).getSupportFragmentManager().beginTransaction(); 行后我仍然无法解决它。然后我想在这个方法中获取捆绑值quote_ft.setArguments(bundle);.
  • @MukeshPrajapati 您可以检查如何在片段传递中获取值
  • @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { String post_id= getArguments().getString("pos"); // you can also pass values in int type. int pos = getArguments().getInt("pos"); } } 它正在工作。非常感谢您宝贵的时间。
猜你喜欢
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 2013-03-16
  • 2022-11-24
  • 2012-11-06
  • 2013-12-12
  • 2018-03-01
相关资源
最近更新 更多