dodaję ListView
na starcie tak:android.widget.LinearLayout nie mogą być oddane do android.widget.TextView
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);
a następnie dodać artykuł kliknij słuchacza tak:
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();
}
});
}
Ale mam błąd:
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
na tej linii:
final String text = (String) ((TextView)view).getText();
Każdy pomysł, jak zdobyć tekst w tym wydaniu? adapter wygląda następująco:
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;
}
i R.layout.shortmenue
jest proste, tylko TextView
jak poniżej:
<?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>
Pracowałem! Na tyle głupi: p –
Lub po prostu spróbuj uderzyć przycisk REBUILD –