0

I have slidingTabLayout, with different Json request. Until now i have managed to get for each page_adapter the different requests based on different queries. What i want to do now, is when i click on the item, it sends me to the article_layout, but the problem is i dont know how to set the onItemClick, to different views.

Here is the code for the onItemClick

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);
            String imageURL = jsonObject.optString("image_url");
            String Text1 = jsonObject.optJSONObject("content").optString("rendered");
            String text2 = jsonObject.optJSONObject("title").optString("rendered");


            Intent detailIntent = new Intent(getActivity(), SingleArticle.class);


            detailIntent.putExtra("imgURL", imageURL);
            detailIntent.putExtra("articleText", Text1);
            detailIntent.putExtra("articleTitle", Text2);

            startActivity(detailIntent);

        }

in the line JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);the jsonObject is set only to homeJSONAdapter. What i want is to have different adapters based on which view the user is, so to read the article of that list

ddnice
  • 25
  • 9

2 Answers2

0
 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ......
        ......

        if(position ==1)
        {


        Intent detailIntent = new Intent(getActivity(), SingleArticle.class);


        detailIntent.putExtra("imgURL", imageURL);
        detailIntent.putExtra("articleText", Text1);
        detailIntent.putExtra("articleTitle", Text2);

        startActivity(detailIntent);

    }

      if(position ==2)
        {

          //start another activity
        Intent detailIntent1 = new Intent(getActivity(), SingleArticle1.class);


        detailIntent1.putExtra("imgURL1", imageURL);
        detailIntent1.putExtra("articleText1", Text1);
        detailIntent1.putExtra("articleTitle1", Text1);

        startActivity(detailIntent1);

    }

       if(position ==3)
        {

          //start another activity
        Intent detailIntent2 = new Intent(getActivity(), SingleArticle2.class);


        detailIntent2.putExtra("imgURL2", imageURL);
        detailIntent2.putExtra("articleText2", Text2);
        detailIntent2.putExtra("articleTitle2", Text2);

        startActivity(detailIntent2);

    }

   .....
   .....
    so on

    }
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • position, gets the position of the list item, i guess i need something to check what view i am in. any idea? – ddnice May 11 '16 at 18:58
0

Ok, here is the situation

     @Override
            public Object instantiateItem(ViewGroup container, int position) {

                View view=null;
                if(position == 0){
                   view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,container, false);
                    homeList = (ListView) view.findViewById(R.id.home_list);
                    queryImp(view, "");
                   homeJSONAdapter = new JSONAdapter(getActivity(), getActivity().getLayoutInflater());

                    homeList.setAdapter(homeJSONAdapter);
                    homeList.setOnItemClickListener(this);


                }else{
                    view = getActivity().getLayoutInflater().inflate(R.layout.other_pager_item,container, false);
                    otherList = (ListView) view.findViewById(R.id.other_list);
                    queryImp(view, "other");
                    otherJSONAdapter = new JSONAdapterOther(getActivity(), getActivity().getLayoutInflater());

                    otherList.setAdapter(ekonomiaJSONAdapter);
                    otherList.setOnItemClickListener(this);

                }

                container.addView(view);

                view.findViewById(R.id.item_title);

                return view;
            }

this is the code that populates the tabs on SlidingTabsBasicFragment and the following is the onclick for the items of the lists

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            if(position == 0)
            {

                JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);
                String imageURL = jsonObject.optString("img_url");
                String artText = jsonObject.optJSONObject("content").optString("rendered");
                String artTitle = jsonObject.optJSONObject("title").optString("rendered");


                Intent detailIntent = new Intent(getActivity(), SingleArticle.class);


                detailIntent.putExtra("imgURL", imageURL);
                detailIntent.putExtra("articleText", artText);
                detailIntent.putExtra("articleTitle", artTitle);

                startActivity(detailIntent);

            }else{


                JSONObject jsonObject1 = (JSONObject) otherJSONAdapter.getItem(position);
                String imageURL1 = jsonObject1.optString("img_url");
                String artText1 = jsonObject1.optJSONObject("content").optString("rendered");
                String artTitle1 = jsonObject1.optJSONObject("title").optString("rendered");

               y
                Intent detailIntent1 = new Intent(getActivity(), SingleArticleOther.class);


                detailIntent1.putExtra("imgURL1", imageURL1);
                detailIntent1.putExtra("articleText1", artText1);
                detailIntent1.putExtra("articleTitle1", artTitle1);


                startActivity(detailIntent1);

            }

which basically, all it does is click on the item of the list and gets to the full article to read. For some reason it all gets a mess. When i try to click on an item of the homeJsonAdapter it goes to the article of the otherJsonAdapter. I dont know why?

ddnice
  • 25
  • 9