单击列表视图中的项目时显示视图

疯狂的DroiD

我有这个listview是新闻列表。在列表视图中单击某个新闻项时,我想显示该新闻项的详细版本。

到目前为止,我已经能够创建列表视图并在列表视图中显示新闻项。(注意:新闻项来自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();
        }

    }


}
CodeMonkey

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单击列表视图中的项目时如何显示数据库中的数据

单击某些列表视图中的项目时出错

单击列表视图中的某些项目时,如何影响列表视图之外的元素?

列表视图中单击的项目位置错误

从列表视图中单击项目开始动画

无法单击列表视图中的项目

在列表视图中突出显示多个项目

项目未显示在列表视图中

显示列表视图中的选定项目

无法显示我的列表视图中的项目

使用arraylist在列表视图的项目上单击时,如何显示列表视图?

Xamarin 列表视图在滚动视图中时未显示完整项目

列表视图中的搜索过滤器在单击项目时返回错误值

在列表视图中显示哈希图中的特定项目

单击任何列表视图项目时,将项目文本显示为EditText

如何基于列表视图中单击的项目启动新的活动?

单击按钮从自定义列表视图中删除项目

单击ArrayAdapter内的图像以删除列表视图中的项目

获取按钮的项目位置,使用cursorAdapter在列表视图中单击

单击列表视图中每个项目的侦听器

单击列表视图中的图像后删除项目(Fragment,BaseAdapter)

如何在 Django 的新视图中显示单击的项目/图像?

单击“回收者”视图中的项目时,查看全屏图像

如何在列表视图中显示广告横幅时管理项目位置

从列表视图中删除项目

识别列表视图中的项目

在列表视图中拖放项目

编辑列表视图中的项目

当用户单击单个项目时,如何在 textview 中显示列表视图数据