Reputation: 6451
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>
Upvotes: 16
Views: 31995
Reputation: 101
You can resolve this by replacing
final String text = (String) ((TextView)view).getText();
with
TextView temp= (TextView) view.findViewById(R.id.textView);
This works for me.
Upvotes: 0
Reputation: 28748
In my case an XML contained a TextView, but I made a mistake writing
final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout);
instead of
final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout, false);
Upvotes: 0
Reputation: 33
I had the same problem and I just renamed inserted view object.
Like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newTextViewName"
android:textSize="34dp"
/>
Upvotes: 0
Reputation: 660
I just added android:id="@+id/my_layout" to LinearLayout that wrapped TextView and that solved similar problem.
Upvotes: 1
Reputation: 6054
If you are running on a similar problem but you sure you have targeted a linearLayout:
Delete the file gen/your.app.package/R.java.
This happens because of a xml bug, when you delete R.java it will be recreated on the next build/run.
Upvotes: 11
Reputation: 87064
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();
Upvotes: 22