mohammedsuhail
mohammedsuhail

Reputation: 701

Getting the selected item from a ListView

Please Help me for getting the selected Item from a ListView. Items for the ListView are getting from a xml file. Elements of the ListView are filled up by the adapter(adpter contains ImageView and textView). I only need the TextView content from the ListView.By using the onItemClick i get only the index of the item.

Thank You

Upvotes: 1

Views: 1425

Answers (2)

hqt
hqt

Reputation: 30266

If I remember true, getSelectedItem() just use if your Activity is ListActivity.

In normal Activity, and you add a component ListView. here is an example code, wish you can follow it :

private ListView listContainer; public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        listContainer = (ListView) findViewById(R.id.listContainer);
        listContainer.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adView, View target, int postion, long id) {
                alert("notice", "you have selected: " + id);            }

        });

        protected void alertbox(String title, String mymessage) {
    new AlertDialog.Builder(this).setMessage(mymessage).setTitle(title).setCancelable(true)
        .setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton){}  
         }).show();  
    }

Upvotes: 0

Adrian F&#226;ciu
Adrian F&#226;ciu

Reputation: 12552

Using getSelectedItem() is the correct thing to do. You get a null value back when no item is selected.

Upvotes: 2

Related Questions