添加onclicklistener以列表视图(安卓) [英] Adding an onclicklistener to listview (android)

查看:225
本文介绍了添加onclicklistener以列表视图(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地实现一个伟大的列表视图,我发现这里的http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/ 但我似乎无法添加onclicklistener 我只是希望能够做一个动作,当我点击行,与该行包含课程资料 有任何想法吗? 谢谢

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.liste);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //设置列表视图
    最终的ListView prestListView =(ListView控件)findViewById(R.id.list);
    最后prestationAdapterEco prestationAdapterEco =新的prestationAdapterEco(这一点,R.layout prestation。);

    prestListView.setAdapter(prestationAdapterEco);

    //填充列表中,通过适配器
    对于(最终prestationEco项:获得prestations()){
        prestationAdapterEco.add(项);
    }
    prestListView.setClickable(真正的);
    prestListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3){
            对象o = prestListView.getItemAtPosition(位置);
            字符串str =(字符串)O; //当您使用的是默认的字符串适配器
            Toast.makeText(getApplicationContext(),STR,Toast.LENGTH_SHORT).show();
        }
    });
}
 

解决方案

  listView.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){

       对象o = prestListView.getItemAtPosition(位置);
       prestationEco海峡=(prestationEco)O; //当您使用的是默认的字符串适配器
       Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
        }
    });
 

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/ but I can't seem to add an onclicklistener I just want to be able to do an action when I click on the row, with the data that the row contains of course any ideas? thanks

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.liste);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Setup the list view
    final ListView prestListView = (ListView) findViewById(R.id.list);
    final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);

    prestListView.setAdapter(prestationAdapterEco);

    // Populate the list, through the adapter
    for(final prestationEco entry : getPrestations()) {
        prestationAdapterEco.add(entry);
    }
    prestListView.setClickable(true);
    prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Object o = prestListView.getItemAtPosition(position);
            String str=(String)o;//As you are using Default String Adapter
            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
        }
    });
}

解决方案

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

       Object o = prestListView.getItemAtPosition(position);
       prestationEco str=(prestationEco)o;//As you are using Default String Adapter
       Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
        }
    });

这篇关于添加onclicklistener以列表视图(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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