如何将数据从片段发送到适配器类,用于button.setOnclickListner

时间:2017-08-29 04:35:09

标签: android

我想从companyName班级到fragment班级Adapter

这是我的适配器类

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    Log.d("test",""+deliveryListBeans.size());
    LayoutInflater inflater=(LayoutInflater)ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v=inflater.inflate(R.layout.list_view_for_delivery_list,null);
    Button delivered=(Button)v.findViewById(R.id.do_delivered);
delivered.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent deliveryUpdate=new Intent(ct, MyOwn.class);
            deliveryUpdate.putExtra("invoiceId",dlb.getInvoiceNo());

            ct.startActivity(deliveryUpdate);

        }
    });
    return v;
}
  

setOnClickListener我需要来自companyName类的fragment值,以便我可以在帮助我的网址中使用它。

这是我的片段类

public class DeliveryList extends Fragment {

private List<DeliveryListBean> dlblistbean= new ArrayList<DeliveryListBean>();
ListView listview;
Button delivered;
String companyName;}
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootView = inflater.inflate(R.layout.fragment_delivery_list,container,false);
    listview=(ListView) rootView.findViewById(R.id.deliveryListView);
    companyName=this.getArguments().getString("companyName");
  

现在,我需要在此companyName课程中使用此fragment来访问Adapter   有人可以告诉我该怎么做。

**This Question is complete edit**

1 个答案:

答案 0 :(得分:3)

在将数据发送到片段之前,您已经替换了片段。您的代码序列应为

String toDeliveryList=getIntent().getStringExtra("userName");
Bundle bundle=new Bundle();
bundle.putString("userID",toDeliveryList);
DeliveryList dl=new DeliveryList();
dl.setArguments(bundle);

fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, d1);
fragmentTransaction.commit();

是的,不要忘记将new DeliveryList()替换为d1中的fragmentTransaction.replace()