onItemClickListener之间的两个片段 [英] onItemClickListener between two fragments

查看:83
本文介绍了onItemClickListener之间的两个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新与android.I都试过,但没能发现我错过了什么。我使用的两个片段两个显示列表。现在我想改变列表数据的第二个片段,当用户在第一个列表项的点击。默认情况下零位索引将被选中,显示在第二个列表中的数据。我使用自定义的数组适配器,显示的列表中两个fragments.Please帮我出guys.Thanks审议。 在这里,我贴我的code:

活动类:

 公共类ProductListActivity扩展活动实现ProductInterface {
公共静态字符串的cookie;
公共静态字符串jsonSettingsResponse;
公共静态的String [] TIDS;
公共静态字符串jsonPorductsCategoryListResponseString;
公共JSONArray jsonPorductsCategoryListResponseArray;
公共静态字符串VID;
公共静态字符串publicPath;
公共静态JSONArray productsList;
公众的ArrayList<字符串> listItems中;
公众的String [] listProCategory;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.product_category_and_list);
    意向意图= getIntent();
    饼干= intent.getStringExtra(BConstant.WEB_SERVICES_COOKIES);
    productsList =新JSONArray();

    FetchProductCategories产品=新FetchProductCategories();
    productsList = products.fetchProducts();
    listProCategory = products.onDisplayProductList(productsList).toArray(
            新的String [0]);

    束束=新包();
    bundle.putString(BConstant.WEB_SERVICES_COOKIES,饼干);
    bundle.putString(BConstant.PUBLIC_PATH,publicPath);
    bundle.putStringArray(BConstant.TAXONOMY_TID,TIDS);
    bundle.putStringArray(BConstant.PRODUCT_C​​ATEGORY_NAMES,
            listProCategory);
    ProductCategoryFragment.newInstance(包);
    捆绑bundleForProductList =新包();
    bundleForProductList.putStringArray(BConstant.TAXONOMY_TID,TIDS);
    bundleForProductList.putString(BConstant.WEB_SERVICES_COOKIES,饼干);
    ProductListFragment.newInstance(bundleForProductList);
}


私有类FetchProductCategories {
    保护JSONArray fetchProducts(字符串... PARAMS){
        jsonSettingsResponse = WebserviceBUtil
                .callWebServicesGetVocabularyList(饼干);
        VID = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.TAXONOMY_VID);
        publicPath = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.PUBLIC_PATH);
        jsonPorductsCategoryListResponseString = WebserviceBUtil
                .callWebServicesGetProductsCategoryList(饼干,VID);
        TIDS = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponseString,
                BConstant.TAXONOMY_TID);
        尝试 {
            jsonPorductsCategoryListResponseArray =新JSONArray(
                    jsonPorductsCategoryListResponseString);
        }赶上(JSONException E){
            e.printStackTrace();
        }
        返回jsonPorductsCategoryListResponseArray;
    }


    受保护的ArrayList<字符串> onDisplayProductList(JSONArray结果){
        listItems中的=新的ArrayList<字符串>();
        的for(int i = 0; I< result.length();我++){
            尝试 {
                listItems中
                        。新增(result.getJSONObject(I)
                                .getString(BConstant.NAME_CONSTANT)
                                的ToString());
            }赶上(例外前){
                ex.printStackTrace();
            }
        }
        返回时listItems;
    }
}


@覆盖
公众的HashMap<整数,位图> DownloadImages(
        HashMap的<整数,字符串> productCategoryImagePath){
    HashMap的<整数,位图> imgBitmap =新的HashMap<整数,位图>();
    对于(INT POS = 0; POS&L​​T; productCategoryImagePath.size(); POS ++){
        位图BVAL = ImageDownloader
                .getBitmapFromURL(productCategoryImagePath.get(POS));
        imgBitmap.put(POS,BVAL);
    }
    返回imgBitmap;

}

@覆盖
公共无效clickedProductCategory(字符串TID){
    FragmentManager productListFragmentManager = getFragmentManager();
    ProductListFragment productListFragment =(ProductListFragment)productListFragmentManager
            .findFragmentById(R.id.proListfragment);
    productListFragment.clickedProductCategoryIdByProductCategoryFragment(TID);

}
 

}

第一块碎片:

 公共类ProductCategoryFragment扩展片段实现OnItemClickListener {
ProductInterface productInterface;
私有静态的String [] TIDS;
私有静态HashMap的<整数,字符串> productCategoryImagePath;
私有静态字符串jsonPorductsDetailsImagePathResponse;
私有静态字符串publicPath;
私有静态字符串的cookie;
公共静态的String [] listProCategory;
公众的ListView listOfProductsCategory;
私有静态HashMap的<整数,位图> imgBitmapUrls;
DisplayProductCategoryListArrayAdapter适配器;


@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
     查看fragProcatView = inflater.inflate(R.layout.product_category_list,集装箱,
                假);

listOfProductsCategory =(ListView控件)fragProcatView .findViewById(R.id.productCategorylistView);
    返回fragProcatView;

}

@覆盖
公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    productInterface =(ProductInterface)getActivity();
    productCategoryImagePath =新的HashMap<整数,字符串>();
    的for(int i = 0; I< tids.length;我++){
        jsonPorductsDetailsImagePathResponse = WebserviceBUtil
                .callWebServicesGetProductsDetails(饼干,TIDS [I]);
        字符串文件名= ProductCategoryIds
                .parseJSONResponseToGetVidOfProductsFromVocabulary(
                        jsonPorductsDetailsImagePathResponse,
                        BConstant.FILE_NAME);
        字符串completeUrl = publicPath +文件名;
        productCategoryImagePath.put(ⅰ,completeUrl);
    }
    imgBitmapUrls = productInterface
            .DownloadImages(productCategoryImagePath);
     适配器=新DisplayProductCategoryListArrayAdapter(
                getActivity(),listProCategory,
                imgBitmapUrls);
    listOfProductsCategory.setAdapter(适配器);
    listOfProductsCategory.setOnItemClickListener(本);
}
 静态ProductCategoryFragment的newInstance(束捆){
        ProductCategoryFragment productCategoryFragment =新ProductCategoryFragment();
        productCategoryFragment.setArguments(包);
        饼干= bundle.getString(BConstant.WEB_SERVICES_COOKIES);
        TIDS = bundle.getStringArray(BConstant.TAXONOMY_TID);
        publicPath = bundle.getString(BConstant.PUBLIC_PATH);
listProCategory = bundle.getStringArray(BConstant.PRODUCT_C​​ATEGORY_NAMES);
        返回productCategoryFragment;
    }

@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
        长ID){
    字符串clickedItemName =(字符串)listOfProductsCategory
            .getItemAtPosition(位置);
    INT clickedItemId =(INT)listOfProductsCategory
            .getItemIdAtPosition(位置);

    Toast.makeText(this.getActivity(),
            产品越来越载:+ clickedItemName,
            Toast.LENGTH_SHORT).show();
        productInterface.clickedProductCategory(TIDS [clickedItemId]);
}


公共类DisplayProductCategoryListArrayAdapter扩展ArrayAdapter<字符串> {

    上下文语境;
    HashMap的<整数,位图> prodctImgs;
    的String [] proCategoryNames;
    HashMap的<整数,位图> biturls;
    DisplayProductCategoryListArrayAdapter(上下文C,
            的String [] listCategory,HashMap的<整数,位图> imgUrls){
        超级(C,
                R.layout.product_category_single_layout,
                R.id.productCategoryName,listCategory);
        this.context = C;
        this.prodctImgs = imgUrls;
        this.proCategoryNames = listCategory;
        this.biturls = imgUrls;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
    查看排= inflater.inflate(R.layout.product_category_single_layout,父母,假);
        ImageView的productCategoryImage =(ImageView的)行
                .findViewById(R.id.productCategoryImageId);
        点阵位图= imgBitmapUrls.get(位置);
        // productCategoryImage.setFocusable(假);
        TextView的productCategoryName =(TextView中)一行
                .findViewById(R.id.productCategoryName);
        productCategoryImage.setImageBitmap(位);
        productCategoryName.setText(proCategoryNames [位置]);
        返回行;
    }
}
  }
 

Secoond片段:

 公共类ProductListFragment扩展片段实现OnItemClickListener {
私有静态HashMap的<整数,位图> imgBitmapUrls;
公众的ListView listOfProducts;
ProductInterface productInterfaceForProductList;
私有静态字符串jsonPorductsCategoryListResponse;
私有静态字符串的cookie;
私有静态的String [] productImgPath;
私有静态的String [] NIDS;
私有静态的String []称号;
私有静态的String [] TIDS;
私人字符串TID;
私有静态HashMap的<整数,字符串> productListImagePath;
DisplayProductListArrayAdapter proListAdapter;
@覆盖
 公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
     查看fragProListView = inflater.inflate(R.layout.product_category_list,集装箱,
                假);
     listOfProducts =(ListView控件)fragProListView .findViewById(R.id.productCategorylistView);
    返回fragProListView;
    }

@覆盖
公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    productInterfaceForProductList =(ProductInterface)getActivity();
    jsonPorductsCategoryListResponse = WebserviceBUtil。
            callWebServicesGetProductsList(饼干,TIDS [0]);
    NIDS = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_NODE_ID);
    标题= ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_TITLE);
    productImgPath = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_IMAGE);
            productListImagePath =新的HashMap<整数,字符串>();
            的for(int i = 0; I< productImgPath.length;我++){
                productListImagePath.put(ⅰ,productImgPath [I]);
            }
            的System.out.println(productListImagePath);
    imgBitmapUrls = productInterfaceForProductList
            .DownloadImages(productListImagePath);

    proListAdapter =新DisplayProductListArrayAdapter(
                getActivity(),标题,
                imgBitmapUrls);
     listOfProducts.setAdapter(proListAdapter);
     listOfProducts.setOnItemClickListener(本);
}
 静态ProductListFragment的newInstance(束捆){
     ProductListFragment productListFragment =新ProductListFragment();
     productListFragment.setArguments(包);
        饼干= bundle.getString(BConstant.WEB_SERVICES_COOKIES);
        TIDS = bundle.getStringArray(BConstant.TAXONOMY_TID);
        返回productListFragment;
    }
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
    // TODO自动生成方法存根

}



公共类DisplayProductListArrayAdapter扩展ArrayAdapter<字符串> {

    上下文语境;
    HashMap的<整数,位图> prodctImgs;
    的String [] proCategoryNames;
    HashMap的<整数,位图> biturls;
    DisplayProductListArrayAdapter(上下文C,
            的String [] listCategory,HashMap的<整数,位图> imgUrls){
        超级(C,
                R.layout.products_list_single_layout,
                R.id.productCategoryName,listCategory);
        this.context = C;
        this.prodctImgs = imgUrls;
        this.proCategoryNames = listCategory;
        this.biturls = imgUrls;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
查看排= inflater.inflate(R.layout.products_list_single_layout,父母,假);
        ImageView的productCategoryImage =(ImageView的)行
                .findViewById(R.id.productImageId);
        点阵位图= imgBitmapUrls.get(位置);
        // productCategoryImage.setFocusable(假);
        TextView的productCategoryName =(TextView中)一行
                .findViewById(R.id.productName);
        productCategoryImage.setImageBitmap(位);
        productCategoryName.setText(proCategoryNames [位置]);
        返回行;
    }
}

公共无效clickedProductCategoryIdByProductCategoryFragment(字符串TID){
    jsonPorductsCategoryListResponse = WebserviceBUtil。
            callWebServicesGetProductsList(饼干,TID);

}
 }
 

解决方案

您可以创建界面,将监听器,并从第一个分片行动,并通过行动,你的活动。

而在你的活动,你可以在第二个片段更新数据。

 公共接口的IFoo
{
 公共无效美孚();
}
 

在第一个片段

  IFoo的响应; //全局变量

    私人ProductCategoryFragment(IFoo的RESP)
    {
    this.responder = RESP;
    }

     静态ProductCategoryFragment的newInstance(捆绑包,IFoo的RESP){
        ProductCategoryFragment productCategoryFragment =新ProductCategoryFragment(RESP);
        productCategoryFragment.setArguments(包);
        饼干= bundle.getString(BsharpConstant.WEB_SERVICES_COOKIES);
        TIDS = bundle.getStringArray(BsharpConstant.TAXONOMY_TID);
        publicPath = bundle.getString(BsharpConstant.PUBLIC_PATH);
listProCategory = bundle.getStringArray(BsharpConstant.PRODUCT_C​​ATEGORY_NAMES);
        返回productCategoryFragment;
    }
 

和somewere的片段

  responder.foo();
 

在你的活动

 公共类ProductListActivity扩展活动实现的IFoo
{
...
公共无效美孚()
{
 //将数据传递到第二个片段
}

的onCreate
{
...
ProductCategoryFragment.newInstance(捆绑,这一点);
}
}
 

请您片段全局变量来让他们获得在每一个瞬间

我希望这是明确的,会有所帮助。如果还不清楚,请随时提出更多的问题:)

I am very new with android.I have tried but not able to find out what i am missing. I am using two fragments two display the list. Now i want to change the list data in second fragment when user click on the first list item. By default zero position index will be selected to display the data in second list. I am using custom array adapter to display the list in both fragments.Please help me out guys.Thanks for considering. Here i am pasting my code :

Activity Class:

   public class ProductListActivity extends Activity implements ProductInterface {
public static String cookie;
public static String jsonSettingsResponse;
public static String[] tids;
public static String jsonPorductsCategoryListResponseString;
public JSONArray jsonPorductsCategoryListResponseArray;
public static String vid;
public static String publicPath;
public static JSONArray productsList;
public ArrayList<String> listItems;
public String[] listProCategory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_category_and_list);
    Intent intent = getIntent();
    cookie = intent.getStringExtra(BConstant.WEB_SERVICES_COOKIES);
    productsList = new JSONArray();

    FetchProductCategories products = new FetchProductCategories();
    productsList = products.fetchProducts();
    listProCategory = products.onDisplayProductList(productsList).toArray(
            new String[0]);

    Bundle bundle = new Bundle();
    bundle.putString(BConstant.WEB_SERVICES_COOKIES, cookie);
    bundle.putString(BConstant.PUBLIC_PATH, publicPath);
    bundle.putStringArray(BConstant.TAXONOMY_TID, tids);
    bundle.putStringArray(BConstant.PRODUCT_CATEGORY_NAMES,
            listProCategory);
    ProductCategoryFragment.newInstance(bundle);
    Bundle bundleForProductList = new Bundle();
    bundleForProductList.putStringArray(BConstant.TAXONOMY_TID, tids);
    bundleForProductList.putString(BConstant.WEB_SERVICES_COOKIES, cookie);
    ProductListFragment.newInstance(bundleForProductList);
}


private class FetchProductCategories {
    protected JSONArray fetchProducts(String... params) {
        jsonSettingsResponse = WebserviceBUtil
                .callWebServicesGetVocabularyList(cookie);
        vid = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.TAXONOMY_VID);
        publicPath = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.PUBLIC_PATH);
        jsonPorductsCategoryListResponseString = WebserviceBUtil
                .callWebServicesGetProductsCategoryList(cookie, vid);
        tids = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponseString,
                BConstant.TAXONOMY_TID);
        try {
            jsonPorductsCategoryListResponseArray = new JSONArray(
                    jsonPorductsCategoryListResponseString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonPorductsCategoryListResponseArray;
    }


    protected ArrayList<String> onDisplayProductList(JSONArray result) {
        listItems = new ArrayList<String>();
        for (int i = 0; i < result.length(); i++) {
            try {
                listItems
                        .add(result.getJSONObject(i)
                                .getString(BConstant.NAME_CONSTANT)
                                .toString());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return listItems;
    }
}


@Override
public HashMap<Integer, Bitmap> DownloadImages(
        HashMap<Integer, String> productCategoryImagePath) {
    HashMap<Integer, Bitmap> imgBitmap = new HashMap<Integer, Bitmap>();
    for (int pos = 0; pos < productCategoryImagePath.size(); pos++) {
        Bitmap bval = ImageDownloader
                .getBitmapFromURL(productCategoryImagePath.get(pos));
        imgBitmap.put(pos, bval);
    }
    return imgBitmap;

}

@Override
public void clickedProductCategory(String tid) {
    FragmentManager productListFragmentManager = getFragmentManager();
    ProductListFragment productListFragment = (ProductListFragment) productListFragmentManager
            .findFragmentById(R.id.proListfragment);
    productListFragment.clickedProductCategoryIdByProductCategoryFragment(tid);

}

}

First Fragment:

    public class ProductCategoryFragment extends Fragment implements OnItemClickListener{
ProductInterface productInterface;
private static String[] tids;
private static HashMap<Integer, String> productCategoryImagePath;
private static String jsonPorductsDetailsImagePathResponse;
private static String publicPath;
private static String cookie;
public static String[] listProCategory;
public  ListView listOfProductsCategory;
private static HashMap<Integer, Bitmap> imgBitmapUrls;
DisplayProductCategoryListArrayAdapter adapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View fragProcatView = inflater.inflate(R.layout.product_category_list, container,
                false);

listOfProductsCategory =(ListView) fragProcatView .findViewById(R.id.productCategorylistView);
    return fragProcatView;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    productInterface = (ProductInterface) getActivity();
    productCategoryImagePath = new HashMap<Integer, String>();
    for (int i = 0; i < tids.length; i++) {
        jsonPorductsDetailsImagePathResponse = WebserviceBUtil
                .callWebServicesGetProductsDetails(cookie, tids[i]);
        String filename = ProductCategoryIds
                .parseJSONResponseToGetVidOfProductsFromVocabulary(
                        jsonPorductsDetailsImagePathResponse,
                        BConstant.FILE_NAME);
        String completeUrl = publicPath + filename;
        productCategoryImagePath.put(i, completeUrl);
    }
    imgBitmapUrls = productInterface
            .DownloadImages(productCategoryImagePath);
     adapter = new DisplayProductCategoryListArrayAdapter(
                getActivity(), listProCategory,
                imgBitmapUrls);
    listOfProductsCategory.setAdapter(adapter);
    listOfProductsCategory.setOnItemClickListener(this);
}
 static ProductCategoryFragment newInstance(Bundle bundle) {
        ProductCategoryFragment productCategoryFragment = new ProductCategoryFragment();
        productCategoryFragment.setArguments(bundle);
        cookie = bundle.getString(BConstant.WEB_SERVICES_COOKIES);
        tids = bundle.getStringArray(BConstant.TAXONOMY_TID);
        publicPath = bundle.getString(BConstant.PUBLIC_PATH);
listProCategory = bundle.getStringArray(BConstant.PRODUCT_CATEGORY_NAMES);
        return productCategoryFragment;
    }

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    String clickedItemName = (String) listOfProductsCategory
            .getItemAtPosition(position);
    int clickedItemId = (int) listOfProductsCategory
            .getItemIdAtPosition(position);

    Toast.makeText(this.getActivity(),
            "Product is getting load : " + clickedItemName,
            Toast.LENGTH_SHORT).show();
        productInterface.clickedProductCategory(tids[clickedItemId]);
}


public class DisplayProductCategoryListArrayAdapter extends ArrayAdapter<String> {

    Context context;
    HashMap<Integer, Bitmap> prodctImgs;
    String[] proCategoryNames;
    HashMap<Integer, Bitmap>biturls;
    DisplayProductCategoryListArrayAdapter(Context c,
            String[] listCategory, HashMap<Integer, Bitmap> imgUrls) {
        super(c,
                R.layout.product_category_single_layout,
                R.id.productCategoryName, listCategory);
        this.context = c;
        this.prodctImgs = imgUrls;
        this.proCategoryNames = listCategory;
        this.biturls = imgUrls;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=((Activity)context).getLayoutInflater(); 
    View row = inflater.inflate(R.layout.product_category_single_layout, parent, false);
        ImageView productCategoryImage = (ImageView) row
                .findViewById(R.id.productCategoryImageId);
        Bitmap bitmap = imgBitmapUrls.get(position);
        // productCategoryImage.setFocusable(false);
        TextView productCategoryName = (TextView) row
                .findViewById(R.id.productCategoryName);
        productCategoryImage.setImageBitmap(bitmap);
        productCategoryName.setText(proCategoryNames[position]);
        return row;
    }
}
  }

Secoond Fragment:

    public class ProductListFragment extends Fragment implements OnItemClickListener{
private static HashMap<Integer, Bitmap> imgBitmapUrls;
public  ListView listOfProducts;
ProductInterface productInterfaceForProductList;
private static String jsonPorductsCategoryListResponse;
private static String cookie;
private static String[] productImgPath;
private static String[] nids;
private static String[] title;
private static String[] tids;
private String tid;
private static HashMap<Integer, String> productListImagePath;
DisplayProductListArrayAdapter proListAdapter;
@Override
 public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState ) {
     View fragProListView = inflater.inflate(R.layout.product_category_list, container,
                false);
     listOfProducts =(ListView) fragProListView .findViewById(R.id.productCategorylistView);
    return fragProListView;
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    productInterfaceForProductList = (ProductInterface) getActivity();
    jsonPorductsCategoryListResponse = WebserviceBUtil.
            callWebServicesGetProductsList(cookie, tids[0]);
    nids = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_NODE_ID);
    title = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_TITLE);
    productImgPath = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
            jsonPorductsCategoryListResponse,
            BConstant.PRODUCT_IMAGE);
            productListImagePath = new HashMap<Integer, String>();
            for(int i =0;i<productImgPath.length;i++){
                productListImagePath.put(i, productImgPath[i]);
            }
            System.out.println(productListImagePath);
    imgBitmapUrls = productInterfaceForProductList
            .DownloadImages(productListImagePath);

    proListAdapter = new DisplayProductListArrayAdapter(
                getActivity(), title,
                imgBitmapUrls);
     listOfProducts.setAdapter(proListAdapter);
     listOfProducts.setOnItemClickListener(this);
}
 static ProductListFragment newInstance(Bundle bundle) {
     ProductListFragment productListFragment = new ProductListFragment();
     productListFragment.setArguments(bundle);
        cookie = bundle.getString(BConstant.WEB_SERVICES_COOKIES);
        tids = bundle.getStringArray(BConstant.TAXONOMY_TID);
        return productListFragment;
    }
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}



public class DisplayProductListArrayAdapter extends ArrayAdapter<String> {

    Context context;
    HashMap<Integer, Bitmap> prodctImgs;
    String[] proCategoryNames;
    HashMap<Integer, Bitmap>biturls;
    DisplayProductListArrayAdapter(Context c,
            String[] listCategory, HashMap<Integer, Bitmap> imgUrls) {
        super(c,
                R.layout.products_list_single_layout,
                R.id.productCategoryName, listCategory);
        this.context = c;
        this.prodctImgs = imgUrls;
        this.proCategoryNames = listCategory;
        this.biturls = imgUrls;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=((Activity)context).getLayoutInflater(); 
View row = inflater.inflate(R.layout.products_list_single_layout, parent, false);
        ImageView productCategoryImage = (ImageView) row
                .findViewById(R.id.productImageId);
        Bitmap bitmap = imgBitmapUrls.get(position);
        // productCategoryImage.setFocusable(false);
        TextView productCategoryName = (TextView) row
                .findViewById(R.id.productName);
        productCategoryImage.setImageBitmap(bitmap);
        productCategoryName.setText(proCategoryNames[position]);
        return row;
    }
}

public void clickedProductCategoryIdByProductCategoryFragment(String tid){
    jsonPorductsCategoryListResponse = WebserviceBUtil.
            callWebServicesGetProductsList(cookie, tid);

}
 }

解决方案

You can create interface that will be listener for and action from first fragment and passing action to your activity.

And in your Activity you can update data in second Fragment.

public interface IFoo
{
 public void foo();
}

in first Fragment

    IFoo responder; //global variable

    private ProductCategoryFragment(IFoo resp)
    {
    this.responder=resp;
    }

     static ProductCategoryFragment newInstance(Bundle bundle, IFoo resp) {
        ProductCategoryFragment productCategoryFragment = new ProductCategoryFragment(resp);
        productCategoryFragment.setArguments(bundle);
        cookie = bundle.getString(BsharpConstant.WEB_SERVICES_COOKIES);
        tids = bundle.getStringArray(BsharpConstant.TAXONOMY_TID);
        publicPath = bundle.getString(BsharpConstant.PUBLIC_PATH);
listProCategory = bundle.getStringArray(BsharpConstant.PRODUCT_CATEGORY_NAMES);
        return productCategoryFragment;
    }

and somewere in fragment

responder.foo();

in your Activity

public class ProductListActivity extends Activity implements IFoo
{
...
public void foo()
{
 //pass data to second fragment
}

onCreate
{
...
ProductCategoryFragment.newInstance(bundle, this);
}
}

Make your fragments global variable to have access them in every moment

I hope it is clear and would help. If is not clear, feel free to ask additional questions :)

这篇关于onItemClickListener之间的两个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆