开发者

how to get listitem text in onItemClick listener?

开发者 https://www.devze.com 2023-03-27 17:10 出处:网络
开发者_C百科I have created a list view and i have added some item, can anybody help me to get the item text in on Item Click listener?

开发者_C百科I have created a list view and i have added some item, can anybody help me to get the item text in on Item Click listener?

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {
            Object o = parent.getItemAtPosition(position);
            String keyword = o.toString();
            Log.v("value ", "result is " + keyword);

        }
    });

I have tried the above code but it does not work...


I assume you have TextView on the item.

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {
            TextView txt = (TextView) parent.getChildAt(position - lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview);
            String keyword = txt.getText().toString();
            Log.v("value ", "result is " + keyword);

        }
    });       

or you can retrieve the text from the ArrayList you feed the ListView


First of all, the correct event to intercept for selection is onItemSelected because a selection might not have been made in the onClick handler. And then the question has been well treated here on stackoverflow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号