如何调用从BaseAdapter类Android的片段? [英] How to call a fragment from BaseAdapter Class Android?
问题描述
我想从我的BaseAdapter类叫Fragement。在这个类我有点击,而我想打电话给新片段按钮,但我不能够得到这一点。我必须从按钮到片段点击传递值。
BaseAdapter类
公共类StatusAdapter扩展了BaseAdapter {
私人活动的活动;
私人的ArrayList< HashMap的<字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL;
公共StatusAdapter(活动一,
ArrayList的< HashMap的<字符串,字符串>> D){
活性= A;
数据= D;
充气=(LayoutInflater)活动
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回data.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.approval_selftrip_inner,NULL);
TextView的approved_by =(TextView中)vi.findViewById(R.id.approved_by);
TextView的状态=(TextView中)vi.findViewById(R.id.status);
TextView的行程=(TextView中)vi.findViewById(R.id.trip);
按钮view_log =(按钮)vi.findViewById(R.id.view_log);
HashMap的<字符串,字符串>名单=新的HashMap<字符串,字符串>();
表= data.get(位置);
approved_by.setText(list.get(first_id));
status.setText(list.get(状态));
trip.setText(list.get(旅行));
view_log.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//在这里,我想打电话给我的片段
}
});
返回六;
}
}
Fragement
公共类日志扩展片段{
上下文语境;
查看rootView;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
上下文= getActivity();
rootView = inflater.inflate(R.layout.activity_log,
集装箱,假);
返回rootView;
}
}
我要调用该片段从BaseAdapter类上点击 view_log
的。请帮助我如何才能做到这一点。
在马丁Cazares 我已经这样做了。
在活动
公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
上下文= getActivity();
rootView = inflater.inflate(R.layout.activity_main,
集装箱,假);
mBroadcastReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Toast.makeText(上下文中,Recived,
Toast.LENGTH_LONG).show();
ApprovalLog fragment2 =新ApprovalLog();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content_frame,fragment2);
fragmentTransaction.commit();
}
};
返回rootView;
}
在 AdapterClass
view_approvallog.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
CommonUtils.showAlert(测试适配器,活动);
activity.registerReceiver(mBroadcastReceiver,新的IntentFilter(
start.fragment.action));
}
});
只需使用此。
公共无效的onClick(视图查看){
LogFrag fragment2 =新LogFrag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1,fragment2);
fragmentTransaction.commit();
}
I want to call a Fragement from my BaseAdapter Class. In this class I have button on click of which I want to call the new fragment, but I am not able to get this. I have to pass values from the click of the button to the fragment.
BaseAdapter Class
public class StatusAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public StatusAdapter(Activity a,
ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.approval_selftrip_inner, null);
TextView approved_by = (TextView) vi.findViewById(R.id.approved_by);
TextView status = (TextView) vi.findViewById(R.id.status);
TextView trip = (TextView) vi.findViewById(R.id.trip);
Button view_log = (Button)vi.findViewById(R.id.view_log);
HashMap<String, String> list = new HashMap<String, String>();
list = data.get(position);
approved_by.setText(list.get("first_id"));
status.setText(list.get("status"));
trip.setText(list.get("trip"));
view_log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Here i want to call my fragment
}
});
return vi;
}
}
Fragement
public class Log extends Fragment {
Context context ;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.activity_log,
container, false);
return rootView;
}
}
I want to call this Fragment from the BaseAdapter Class on click of view_log
.Please help me how can we do this
After Martin Cazares I have done this
In Activity
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.activity_main,
container, false);
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Recived",
Toast.LENGTH_LONG).show();
ApprovalLog fragment2 = new ApprovalLog();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment2);
fragmentTransaction.commit();
}
};
return rootView;
}
In The AdapterClass
view_approvallog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CommonUtils.showAlert("Test in Adapter", activity);
activity.registerReceiver(mBroadcastReceiver, new IntentFilter(
"start.fragment.action"));
}
});
Simply use this.
public void onClick(View view) {
LogFrag fragment2 = new LogFrag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
这篇关于如何调用从BaseAdapter类Android的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!