2016-05-11 16 views
0

Ich habe slidingTabLayout, mit anderen JSON-Anfrage. Bis jetzt habe ich es geschafft, für jeden page_adapter die verschiedenen Anfragen basierend auf verschiedenen Anfragen zu bekommen. Was ich jetzt tun möchte, ist, wenn ich auf das Element klicke, schickt es mich zum article_layout, aber das Problem ist, ich weiß nicht, wie man das onItemClick auf verschiedene Ansichten setzt. HieronItemClick ListView, verschiedene Adapter

ist der Code für die 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 der Linie JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position); die JSONObject gesetzt ist nur homeJSONAdapter. Was ich will, ist verschiedene Adapter zu haben, basierend auf welche Ansicht der Benutzer, so der Artikel dieser Liste zu lesen

Antwort

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 

    } 
+0

Position, bekommt die Position des Listenelements, ich denke, ich brauche etwas zu überprüfen, in welcher Ansicht ich bin. Irgendeine Idee? – ddnice

0

Ok, hier ist die 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); 

      } 

, die im Grunde alle es tut, Klicken Sie auf das Element der Liste und Sie erhalten den vollständigen Artikel zum Lesen. Aus irgendeinem Grund wird alles ein Durcheinander. Wenn ich versuche, auf einen Gegenstand des homeJsonAdapter zu klicken, geht es zum Artikel des anderenJsonAdapter. Ich weiß nicht warum?

Verwandte Themen