点击列表行显示祝酒消息
我遇到了错误
我能够在列表视图中显示元素,但点击列表项我会收到错误
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Names[]={"Mango","Banana","grapes"};
ListView LV=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView1,Names);
LV.setAdapter(adapter);
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="TextView"
android:textStyle="italic|bold" />
</LinearLayout>
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
</ListView>
</LinearLayout>
LOG
08-19 18:29:18.621: D/AndroidRuntime(692): Shutting down VM
08-19 18:29:18.621: W/dalvikvm(692): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-19 18:29:18.921: E/AndroidRuntime(692): FATAL EXCEPTION: main
08-19 18:29:18.921: E/AndroidRuntime(692): java.lang.ClassCastException: android.widget.LinearLayout
08-19 18:29:18.921: E/AndroidRuntime(692): at com.example.listviews.MainActivity$1.onItemClick(MainActivity.java:34)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.ListView.performItemClick(ListView.java:3513)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Handler.handleCallback(Handler.java:587)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Looper.loop(Looper.java:123)
08-19 18:29:18.921: E/AndroidRuntime(692): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 18:29:18.921: E/AndroidRuntime(692): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 18:29:18.921: E/AndroidRuntime(692): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 18:29:18.921: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-19 18:29:18.921: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-19 18:29:18.921: E/AndroidRuntime(692): at dalvik.system.NativeStart.main(Native Method)
08-19 18:29:23.481: I/Process(692): Sending signal. PID: 692 SIG: 9
08-19 18:35:01.833: D/AndroidRuntime(726): Shutting down VM
08-19 18:35:01.833: W/dalvikvm(726): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-19 18:35:01.851: E/AndroidRuntime(726): FATAL EXCEPTION: main
08-19 18:35:01.851: E/AndroidRuntime(726): java.lang.ClassCastException: android.widget.LinearLayout
08-19 18:35:01.851: E/AndroidRuntime(726): at com.example.listviews.MainActivity$1.onItemClick(MainActivity.java:34)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.ListView.performItemClick(ListView.java:3513)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Handler.handleCallback(Handler.java:587)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Looper.loop(Looper.java:123)
08-19 18:35:01.851: E/AndroidRuntime(726): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 18:35:01.851: E/AndroidRuntime(726): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 18:35:01.851: E/AndroidRuntime(726): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 18:35:01.851: E/AndroidRuntime(726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-19 18:35:01.851: E/AndroidRuntime(726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-19 18:35:01.851: E/AndroidRuntime(726): at dalvik.system.NativeStart.main(Native Method)
有关克服此问题的任何想法
答案 0 :(得分:5)
正如您从logcat中看到的那样,您正在获得类强制转换异常,因为您尝试将线性布局(对于您的listitem)转换为TextView的根视图(在本例中为View v)。
您应该做的事情如下:
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
//here v is your ListItem's layout.
TextView tv = (TextView) v.findViewById(R.id.textView1);
Toast.makeText(getApplicationContext(), tv.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:1)
试试这段代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Names[]={"Mango","Banana","grapes"};
ListView LV=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView1,Names);
LV.setAdapter(adapter);
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
String itemStr = adapter.getItem(arg2);
Toast.makeText(getActivity(),itemStr ,Toast.LENGTH_SHORT).show();
}
});
}
}
答案 2 :(得分:0)
试试这个:
String s =(String) ((TextView) view.findViewById(R.id.textView1)).getText();
希望它能帮助!!
答案 3 :(得分:0)
您的视图是LinearLayout而不是TextView 您需要从布局中获取此信息。
更改为:
Toast.makeText(getApplicationContext(), ((TextView)((LinearLayout ) v).findViewbyId(R.id.textView1)).getText().toString(), Toast.LENGTH_SHORT).show();