onItemClickListener между двумя фрагментами
Я очень новый с Android. Я пытался, но не смог выяснить, что мне не хватает. Я использую два фрагмента, два отображения списка. Теперь я хочу изменить данные списка во втором фрагменте, когда пользователь нажимает на первый элемент списка. По умолчанию будет выбран индекс нулевой позиции для отображения данных во втором списке. Я использую пользовательский адаптер массива для отображения списка в обоих фрагментах. Пожалуйста, помогите мне, ребята. Спасибо за рассмотрение. Здесь я вставляю свой код:
Класс деятельности:}
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 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 onDisplayProductList(JSONArray result) {
listItems = new ArrayList();
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 DownloadImages(
HashMap productCategoryImagePath) {
HashMap imgBitmap = new HashMap();
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);
}
Первый фрагмент:
public class ProductCategoryFragment extends Fragment implements OnItemClickListener{
ProductInterface productInterface;
private static String[] tids;
private static HashMap productCategoryImagePath;
private static String jsonPorductsDetailsImagePathResponse;
private static String publicPath;
private static String cookie;
public static String[] listProCategory;
public ListView listOfProductsCategory;
private static HashMap 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();
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 {
Context context;
HashMap prodctImgs;
String[] proCategoryNames;
HashMapbiturls;
DisplayProductCategoryListArrayAdapter(Context c,
String[] listCategory, HashMap 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:
public class ProductListFragment extends Fragment implements OnItemClickListener{
private static HashMap 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 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();
for(int i =0;i