单击listview中的项目时显示视图

时间:2014-08-15 04:38:06

标签: android json android-fragments android-listview bundle

我有这个列表视图,这是一个新闻项目列表。我想在列表视图中点击该新闻项目时显示特定新闻项目的详细版本。

到目前为止,我能够创建列表视图并在列表视图中显示新闻项目。 (注意:新闻项目取自JSON)

我可以在列表视图中点击新闻项目时显示视图。但问题是当点击列表视图项目时,会显示新闻项目详细版本的视图,但它会显示新闻的所有详细版本。

我只想显示点击的新闻项目的详细版本。我怎么能这样做?

研究之后,我认为我应该使用一个捆绑包。但我不知道如何做到这一点。

我会在这里发布课程

NewsFragment.java

public class NewsFramgment extends Fragment {


    private ListView listView;


    private ArrayList<BaseElement> News;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.news_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();


        listView = (ListView) view.findViewById(R.id.list);


        listView.setOnItemClickListener(new OnItemClickListener() {

               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id)

               {


              android.support.v4.app.Fragment detail = new NewsDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit(); 

                }
              });



            new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            News = JSONServices.getNewsDescription();
            return null;
        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setNewsDescription(News);

            adapter = new LazyAdapter(News, activity,Element.NEWS_LIST.getType());

            listView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

NewsDetailFramgment.java

public class NewsDetailFragment extends Fragment {

private View view1;

    private ArrayList<BaseElement> newsdetail;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.newsdetail_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        view1 = (View) view.findViewById(R.id.list);


        new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {



            newsdetail = JSONServices.getNewsDescription();

            return null;

        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(newsdetail);

            adapter = new LazyAdapter(newsdetail, activity,Element.NEWS_DETAIL.getType());

            ((AdapterView<ListAdapter>) view1).setAdapter(adapter);

            super.onPostExecute(result);
        }



        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

1 个答案:

答案 0 :(得分:1)

看起来您正在呼叫newsdetail = JSONServices.getNewsDescription();,然后将结果分配给adapter中的NewsDetailFragmentJSONServices.getNewsDescription();返回了什么?我想这是所有的NewsDetail文章。

@Raghunandan是对的,该示例显示了如何处理活动中的两个片段,并根据“新闻”列表中选择的项目显示“详细信息”片段。

您可以在父Activity中实现调用,如“实现接口”下的示例所示。

YourParentActivity.java

public static class YourParentActivity extends Activity {

    implements NewsFragment.OnHeadlineSelectedListener{
        ...
    }

    public void onArticleSelected(int position) {
        NewsDetailFramgment articleFrag = (NewsDetailFramgment)
        getSupportFragmentManager().findFragmentById(R.id.news_detail_framgment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the NewsDetailFramgment to update its content
            articleFrag.updateArticleView(position);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            NewsDetailFramgment newFragment = new NewsDetailFramgment ();
            Bundle args = new Bundle();
            args.putInt(NewsDetailFramgment.ARG_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

请注意onArticleSelected()接受int position?这是单击的新闻标题适配器中的位置。您可以使用此功能从“详细信息”结果中获取相应的“新闻详细信息”项。

这纯粹是基于示例的大纲,但应该向您展示您在父活动中需要做什么。

阅读示例并将相关部分添加到您的片段中(查看“定义界面”以了解对NewsFragment.java的更改)。

修改

您需要在NewsFragment类中定义一个接口,如“定义接口”下的文档中所示。

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

这为您的父类提供回调,以便在按下所选标题的位置按下新闻标题时进行处理。在示例中,它会在您的父活动中调用onArticleSelected(int position);,然后填充NewsDetailFragment