简体   繁体   English

Android:ListView和事件每个项目

[英]Android:ListView and events each item

I made custom listview in xml 我在xml中创建了自定义listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView
           ... />
        <TextView
           ... />

        <TextView
            ... />
         <CheckBox
            ...
             />
</RelativeLayout>

A part of my activity: 我活动的一部分:

public class SPMMainActivity extends Activity {  
        private ArrayList <HashMap<String, Object>> myBooks; 
        private static final String KEY1 = "key1";    
        private static final String KEY2 = "key2";  
        private static final String KEY3 = "key3";  
        private static final String CHECK = "check"; 

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_spmmain);

            schedule_listview = (ListView) findViewById(R.id.MainActivity_schedule);
            myBooks = new ArrayList<HashMap<String,Object>>();      
            HashMap<String, Object> hm;   

            hm = new HashMap<String, Object>();
            hm.put(KEY1, "1First");     
            hm.put(KEY2, "test");    
            hm.put(KEY3, "test2");     
            hm.put(CHECK, Boolean.FALSE);   
            myBooks.add(hm);

            hm = new HashMap<String, Object>();
            hm.put(KEY1, "Second");        
            hm.put(KEY2, "test3");         
            hm.put(KEY3, "test4 ");         
            hm.put(CHECK, Boolean.TRUE);   
            myBooks.add(hm);

            SimpleAdapter adapter = new SimpleAdapter(this, 
                    myBooks, 
                    R.layout.mainactivity_schedule, 
                    new String[]{ 
                    KEY1,         
                    KEY2,        
                    KEY3,
                    CHECK
                    },
                    new int[]{    
                    R.id.text1,  
                    R.id.text2,
                    R.id.text3,
                    R.id.checkBox1}
            );      

            schedule_listview.setAdapter(adapter);                        
            schedule_listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

            registerForContextMenu(schedule_listview);

        }
...
}

How I can get events from each CheckBox? 如何从每个CheckBox获取事件? For example, if user click on checkbox, program must get event from third item's CheckBox, and turn on alarm. 例如,如果用户单击复选框,则程序必须从第三项的复选框中获取事件,然后打开警报。

在列表中使用Custom Adaptor ,并在Custom AdaptorgetView()方法中为复选框设置侦听器,例如参见Android ListView Example 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM