2015-06-09 51 views
0

我有以下的列表視圖Click事件中列表查看每個對象 - 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處理程序?

+0

通過 「對象」 你的意思是一個ListItem?或物品內的任何東西? – theMfromA

回答

1

你可以設置你的ListViewOnItemClickListener,然後在您的適配器這樣的定義每個元素的操作:

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 ... 
      } 
     } 
    }); 
1

onItemClickListener添加到您的ListView。然後,您可以在點擊方法中使用livarray.get(position)找回您的房間。

你可以添加一個可運行到你的房間對象或徘徊無論你想在這個地方使用它...

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

     Room mRoom= livarray.get(position). 

     }); 
相關問題