列表视图中的Andr​​oid烤面包消息 [英] Listview in android with toast message

查看:129
本文介绍了列表视图中的Andr​​oid烤面包消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 和表行的onclick显示敬酒消息

  • And onclick of list row display a toast message

我有错误

我能够显示在列表视图中的元素,但是,点击列表项的我收到错误

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)

克服任何想法此

推荐答案

正如你可以从logcat的看,你得到类转换异常,因为你想投的线性布局是根视图(视图V在这种情况下, )为您的列表项到一个TextView。

As you can see from the logcat, You are getting class cast exception because you are trying to cast linear layout which is the root view (View v in this case) for your listitem to a TextView.

你应该做的是如下:

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();
        }
    });

这篇关于列表视图中的Andr​​oid烤面包消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆