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 8026223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:25:14+00:00 2026-06-04T23:25:14+00:00

I add a ListView on runtime like this: MainMenue = getResources().getStringArray(R.array.Unit); // remove all

  • 0

I add a ListView on runtime like this:

     MainMenue =  getResources().getStringArray(R.array.Unit);
    // remove all controls 
    LinearLayout formLayout = (LinearLayout)findViewById(R.id.submenue);
    formLayout.removeAllViews();
    menueview = new ListView(getApplicationContext());               
    menueview.setVisibility(ListView.VISIBLE);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
               LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.RIGHT;
    menueview.setLayoutParams(params);
    menueview.setAdapter(new submenueadapter(menueview.getContext(), MainMenue));
    // Set the on Item 
    SetMenueOnClick() ;
    formLayout.addView(menueview);

and then I add a item click listener like this:

 public void SetMenueOnClick() {
     menueview.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
              final String text = (String) ((TextView)view).getText();
          }
     });
 }

But then I have an error:

06-03 10:59:25.862: E/AndroidRuntime(14732):    at android.view.ViewRoot.handleMessage(ViewRoot.java:2109)
android.widget.LinearLayout cannot be cast to android.widget.TextView

at this line:

final String text = (String) ((TextView)view).getText();

Any idea how to get the text in this issue? the adapter looks like this:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.shortmenue, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.contents);

    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    rowView.setBackgroundResource(R.drawable.alternate_list_color);
    return rowView;
}

and R.layout.shortmenue is simple, only a TextView like below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/contents"
                android:textSize="34dp" 

                />

</LinearLayout>
  • 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-06-04T23:25:16+00:00Added an answer on June 4, 2026 at 11:25 pm

    Your row is a TextView wrapped by a LinearLayout so you might want to do this:

    LinearLayout ll = (LinearLayout) view; // get the parent layout view
    TextView tv = (TextView) ll.findViewById(R.id.contents); // get the child text view
    final String text = tv.getText().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used listview control to add records in it, and wanted to get all
How to add hyperlink column in wpf listview at runtime ?
I have 3 ListView's that are populated like this (well this can be anything,
I will like to use gallery view to add listview or relativelayout. Most of
I have this problem. I want to add image to listView. Exactly I want
How to add items to a ListView in Android during runtime?
I have a listview like this and in FormLoad event I should do some
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I want to add a listview in android app. In listview must to be
i want to add data to listview but my data are as String[] and

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.