Android:如何使用listadpater单击listactivity中的listitem

时间:2012-03-20 14:10:46

标签: android

我想点击下面代码中的list-item。

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 个答案:

答案 0 :(得分: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    


            }
        });