单击列表视图中的每个对象的事件 - Android

时间:2015-06-09 15:19:36

标签: android

我有以下列表视图

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

    lv = (ListView) findViewById(R.id.liv1);

    List<Room> livarray = new ArrayList<Room>();

    livarray.add(new Room(R.drawable.room, "Meeting Room 1", "Small", "Yes"));
    livarray.add(new Room(R.drawable.room, "IT Floor", "Large", "Yes"));
    livarray.add(new Room(R.drawable.room, "Data Innovations Room", "Medium", "No"));
    livarray.add(new Room(R.drawable.room, "Batman Room", "Large", "No"));
    livarray.add(new Room(R.drawable.room, "Brainstorming Room", "Small", "No"));
    livarray.add(new Room(R.drawable.room, "Room 1408", "Expanding", "No"));

    ListView listViewRooms = (ListView) findViewById(R.id.liv1);

    ListRoomsAdapter adapter = new ListRoomsAdapter(this, livarray);

    listViewRooms.setAdapter(adapter);


}

如何为列表中的每个对象设置特定的onClick处理程序?

3 个答案:

答案 0 :(得分:1)

onItemClickListener添加到ListView。然后,您可以使用livarray.get(position)在点击方法中检索您的房间。

你可以将一个runnable添加到你的Room对象或你想要的地方,并在这个地方使用它......

答案 1 :(得分:1)

您可以在OnItemClickListener上设置ListView,然后为适配器中的每个元素定义操作:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Room item = livarray.get(position);
            switch(position){
                case item ...
            }
        }
    });

答案 2 :(得分:1)

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

         Room mRoom=  livarray.get(position).

        });