Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3605102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:04:55+00:00 2026-05-18T21:04:55+00:00

For EfficientAdapter What code I need to use under onListItemClick to get text of

  • 0

For EfficientAdapter What code I need to use under onListItemClick to get text of selected item?
I tried:

str=(String) ((TextView)l.getItemAtPosition(position)).getText()

But this only brings CastException, since it fetches the LinearLayout view holding the textview and imageview (see code here)

Please help!

Some of the code:

public class Bookmarks extends ListActivity {
public static Typeface mFace;
public EfficientAdapter eff;

private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return DAT.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_text, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.text.setTypeface(mFace);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text.setText(DAT[position]);

        return convertView;
    }

    static class ViewHolder {
        TextView text;
    }
}

public Object getItem(int position) { 
    return position; 
}

public void onResume(Bundle icicle) {
    eff.notifyDataSetChanged();
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    //////////CRASHES on next line!!!!!!!!!!!!!!!!!!
TextView tv = (TextView) l.getItemAtPosition(position);
    String str = tv.getText().toString();

}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    eff = (new EfficientAdapter(this));
    setListAdapter(eff);
    registerForContextMenu(getListView());
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:04:56+00:00Added an answer on May 18, 2026 at 9:04 pm

    There isn’t really enough information in your question (see my comment above), but here’s a guess at an answer:

    First, getText() is documented to return a CharSequence, not a String. It might return a String, but you don’t know that it does. So it would be safer to write:

    str = ((TextView)l.getItemAtPosition(position)).getText().toString()
    

    Second, if that doesn’t work, try breaking down that statement so you can get a better idea of where the exception is coming from. Something like this, perhaps, might be clearer:

    TextView tv = (TextView) l.getItemAtPosition(position);
    str = tv.getText().toString();
    

    EDIT based on your update:

    1) If you’re going to implement onListItemClick, be sure that you begin the method by calling up to the base class, as shown below.

    2) Here’s the problem: (I realized this after copying and pasting in a different example, which I think won’t be necessary now): ListView.getItemAtPosition doesn’t return a View at all; it returns an item from your Adapter (a Cursor, an array entry, whatever.) To get the TextView, you need to use findViewById, or better still, your ViewHolder. I think this will work:

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    
        ViewHolder holder = (ViewHolder) v.getTag();
        TextView tv = holder.text;
        String str = tv.getText().toString();
    }
    

    If you’re still having problems, please copy and paste the traceback for the exception that you’re getting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using EfficientAdapter that extends ListActivity. For some reason, when I use code
i use the following code to list few items from array , while scrolling
I have a little problem. I have class private static class EfficientAdapter extends BaseAdapter
In my onCreate, the following code executes: dialog = new ProgressDialog(this); dialog.setCancelable(false); dialog.setCanceledOnTouchOutside(false); dialog.setMessage(Welcome!
I'm using an extended version of BaseAdapter based on the EfficientAdapter example from the
I have a custom listView with a textView, editText and a Button on a
friends, i have code DalCategories selected_object= new DalCategories(); for (DalCategories cat : DalCategories.EditPostCategory) {
friends, i am using following custom adapter to show textview and checboxes in listview.
Hi I am creating a List View in another List View using this code
I have a strange error in my code and i can't see the problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.