onItemClick ListView,不同的适配器

时间:2016-05-11 13:56:06

标签: android json listview onitemclicklistener onitemclick

我有使用不同Json请求的slidingTabLayout。 到目前为止,我已经设法为每个page_adapter获取基于不同查询的不同请求。 我现在要做的是,当我点击该项目时,它会将我发送到article_layout,但问题是我不知道如何将onItemClick设置为不同的视图。

以下是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);

        }

在行JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);中,jsonObject仅设置为homeJSONAdapter。我想要的是根据用户的视图使用不同的适配器,以便阅读该列表的文章

1 个答案:

答案 0 :(得分: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

    }