如何在 ArrayAdapter 中调用 getSupportFragmentManager()
Posted
技术标签:
【中文标题】如何在 ArrayAdapter 中调用 getSupportFragmentManager()【英文标题】:How to call getSupportFragmentManager() in ArrayAdapter 【发布时间】: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;
【问题讨论】:
getSupportFragmentManager
是 FragmentActivity
的成员 ...所以很明显,如果你想调用它,你需要引用此类的实例
@Selvin 我仍然无法解决它。
为什么在适配器中需要它?
【参考方案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");
它正在工作。非常感谢您宝贵的时间。以上是关于如何在 ArrayAdapter 中调用 getSupportFragmentManager()的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android ArrayAdapter 中使用对象数组?
Android - ArrayAdapter<LinearLayout> :指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
如何使用 ArrayAdapter 在 ArrayList 中的 ListView 中显示项目(图像的路径)?