名单上的片段​​点击项目监听 [英] List Fragment on item click listener

查看:148
本文介绍了名单上的片段​​点击项目监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个列表上的Andr​​oid's ListFragment与OnItemClickListener,但我不能,我点击ñ列表项,没有任何反应。我是新手,有碎片。这里是我的code。谢谢!

 公共类MyFragment扩展ListFragment {@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    视图V = inflater.inflate(R.layout.hello,集装箱,FALSE);     LV的ListView =(ListView控件)v.findViewById(android.R.id.list);
        //列表视图对项目点击监听器
        lv.setOnItemClickListener(新OnItemClickListener(){            公共无效onItemClick(适配器视图<>母公司,视图V,
                    INT位置,长的id){
                //从选定的ListItem得到的值
                字符串TITULO =((的TextView)v.findViewById(R.id.title))
                        .getText()的toString()。
                字符串contenido =((的TextView)v.findViewById(R.id.content))
                .getText()的toString()。
                字符串出生日期=((的TextView)v.findViewById(R.id.date))
                        .getText()的toString()。                //开始新的细节
                在意向=新意图(getActivity()。getApplicationContext()
                        NoticiasFragment.class);
                in.putExtra(称号,TITULO);
                in.putExtra(内容,contenido);
                in.putExtra(DATE,出生日期);
                startActivity(在);
            }
    返回伏;
}


解决方案

ListFragment 有一个内置的 onListItemClick 功能覆盖。

您应该做的是这样的:

 公共类MyFragment扩展ListFragment {    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        视图V = inflater.inflate(R.layout.hello,集装箱,FALSE);
        返回伏;
    }    @覆盖
    公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        //做你的东西..
    }}

I am trying to do a list on an Android´s ListFragment with an OnItemClickListener but I can´t, I click n list item and nothing happens. I am novice with fragments. Here is my code. Thank you!

public class MyFragment extends ListFragment {

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

    View v= inflater.inflate(R.layout.hello, container, false);



     ListView lv =(ListView) v.findViewById(android.R.id.list);


        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // getting values from selected ListItem
                String titulo = ((TextView) v.findViewById(R.id.title))
                        .getText().toString();
                String contenido = ((TextView) v.findViewById(R.id.content))
                .getText().toString();
                String fecha = ((TextView) v.findViewById(R.id.date))
                        .getText().toString();

                // Starting new detail
                Intent in = new Intent(getActivity().getApplicationContext(),
                        NoticiasFragment.class);
                in.putExtra("title", titulo);
                in.putExtra("content", contenido);
                in.putExtra("date", fecha);
                startActivity(in);


            }


    return v;
}

解决方案

ListFragment has a built-in onListItemClick function to override.

You should do it like this:

public class MyFragment extends ListFragment {

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

        View v= inflater.inflate(R.layout.hello, container, false);
        return v;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Do your stuff..
    }

}

这篇关于名单上的片段​​点击项目监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆