簡體   English   中英

Android:ListView和事件每個項目

[英]Android:ListView and events each item

我在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>

我活動的一部分:

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);

        }
...
}

如何從每個CheckBox獲取事件? 例如,如果用戶單擊復選框,則程序必須從第三項的復選框中獲取事件,然后打開警報。

在列表中使用Custom Adaptor ,並在Custom AdaptorgetView()方法中為復選框設置偵聽器,例如參見Android ListView Example 示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM