1

I've got a ListView which populates a list of quotes from a String Array, however onClick I require the selected <item> to be read to a String. This is the code I use:

 ListView sample = (ListView)findViewById(R.id.Samplelist);

    String[] backup = getResources().getStringArray(R.array.months);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, backup);

    sample.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                                int position, long id) {
            final String item = (String) parent.getItemAtPosition(position);
            view.animate().setDuration(2000).alpha(0)
                    .withEndAction(new Runnable() {
                        @Override
                        public final void run()
                        {

                            String readItem;
                            finish();

                        }
                    });
        }


    sample.setAdapter(adapter);


}

And here sample.setAdapter(adapter) seems to be an error and is underlined with red.

Is there a way to get this done correct? Any help will be appreciated

3
  • another error ? can you post logcat of error message Commented Aug 24, 2015 at 6:02
  • show onClick method code Commented Aug 24, 2015 at 6:04
  • can you please post the error? Commented Aug 24, 2015 at 6:19

3 Answers 3

2

If you look at layout of simple_list_item_1 then you can find id of textview is text1.

You can define OnItemClickListener to your listview like this:

   listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();
            }
        });

EDIT:

You can also try

String selectedItem = (String)parent.getItemAtPosition(position);

instead of

String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();

Hope it helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but actually what is text1 and I am not using a TextView in my XML
text1 is id of your textview defined in simple_list_item_1 xml file provided by android sdk
It works, thanks, but again what exactly is simple_list_item_1
simple_list_item_1 is default list item layout provided by android sdk. you can also design your custom layout
0

You can try this approach:

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {

                String data=(String)parent.getItemAtPosition(position);
            }
        });

Comments

0

You may need to parse the text from View in OnItemClickListener

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedItemText = ((TextView)view).getText().toString();
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.