2012-03-20 37 views
-1

我想單擊下面的代碼中的列表項。Android:如何點擊listitem與listadpater的列表活動

public class Attendance extends ListActivity { 
    // implements OnItemClickListener 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 



     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     RestClient client = new RestClient("http://192.168.69.1/Webservice/Servlet"); 

     String result = null; 
     try { 
      result = client.Execute(RequestMethod.GET); 
     } catch (Exception e) { 

     } 

     Document doc = XMLfunctions.XMLfromString(result); 
     int numResults = XMLfunctions.numResults(doc); 

     if ((numResults <= 0)) { 
      Toast.makeText(Attendance.this, "numresults<=0", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
     } 

     NodeList nodes = doc.getElementsByTagName("Test1"); 

     for (int i = 0; i < nodes.getLength(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      Element e = (Element) nodes.item(i); 
      map.put("name", XMLfunctions.getValue(e, "name")); 
      mylist.add(map); 
     } 

     ListAdapter adapter = new SimpleAdapter(this, mylist, 
       R.layout.testing, new String[] { "name" }, 
       new int[] { R.id.name }); 

     setListAdapter(adapter); 

     //ListView lv = getListView(); 

     // lv.setAdapter(adapter); 

    } 
    public void onListItemClick(ListView parent, View view, int position, long id) 
    { 
     Toast.makeText(this, "Clicking", Toast.LENGTH_SHORT) 
     .show(); 
    } 
} 

我需要在上面的代碼中進行更正和更改。

+1

然後是什麼問題? – 2012-03-20 14:17:40

+0

我無法調用ListItem。 – Pacs 2012-03-20 14:22:12

+0

我已經嘗試了很多方法,但無法將單擊事件放在列表視圖中。 – Pacs 2012-03-20 14:22:49

回答

4
getListView().setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> a, View v, int position, 
        long id) { 
          //get position and do what you want here  


      } 
     }); 
+0

這不起作用。我正在給你我的團隊查看器來顯示我的錯誤.. – Pacs 2012-03-20 14:25:10

相關問題