Rehan K
Rehan K

Reputation: 193

Getting clicked String from ListView

i'm new to android and Its developing.i have Listview .when i press the item of it.i want to show its name-String name.how do i do ? please find below the code i used.there im getting myItem in Toast values such as 0,1,2,3... instead of String names. please find below the code snippet.

menu2.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {


                     String selectedFromList = String.valueOf(menu2.getItemAtPosition(arg2));



                        ImageView imgbtn2 = (ImageView) arg1
                                    .findViewById(R.id.imageView1);
                        Object item = arg0.getItemAtPosition(arg2);
                        final String myitem = item.toString();

                            imgbtn2.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) { // TODO Auto-generated method




                                    Toast.makeText(getActivity(),myitem, Toast.LENGTH_LONG).show();


                                }
                            });

Upvotes: 0

Views: 53

Answers (4)

Simmant
Simmant

Reputation: 1513

If you want to get some from your list view item than its better go with the costume listview, In that you can handle all the event of component of your costume listview in getView() method for more follow this url

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

On Rehan's demand. ;-)

You should move the ImageView in getView method of the adapter and add a separate onClickListener to it.

Glad I was able to help. :)

Upvotes: 1

ucsunil
ucsunil

Reputation: 7494

To get your item:

String item = arg0.getItemAtPosition(arg2);

Upvotes: 0

nEwbie
nEwbie

Reputation: 96

try this

String selectedFromList = (String) arg0.getItemAtPosition(arg2);

Upvotes: 1

Related Questions