M
M
Maxim Zelenkov2014-02-03 20:31:23
Android
Maxim Zelenkov, 2014-02-03 20:31:23

How to determine the selected item of a ListView?

The question is simple, but I can't get it.
The bottom line is that I need to determine the text of the selected ListView element . The ListView
is filled with a parser from an XML file, here is the code:

public class Marsh extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rasp);
        ArrayList<String> list = new ArrayList<String>();
        try {
        XmlPullParser parser = getResources().getXml(R.xml.troll);
            while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) 
            {
            	if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("bus"))
                {
                        list.add(parser.getAttributeValue(0));
                }
           parser.next();
            }
        }
        catch (Throwable t) {
            Toast.makeText(this,
                    "Ошибка при загрузке XML-документа: " + t.toString(), 4000)
                    .show();
        }
 
        setListAdapter(new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, list));

        OnItemClickListener itemListener = new OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView<?> parent, View v,
        			int position, long id)  {
                  //Что мне тут написать
        	}
        };
        getListView().setOnItemClickListener(itemListener);
    }   
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tarikb, 2014-02-04
@st1nger757

OnItemClickListener itemListener = new OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView<?> parent, View v,
        			int position, long id)  {
                 String value = getListAdapter().getItem(position);
        	}
        };

C
constv, 2014-02-03
@constv

item value from Android ListView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question