Android ListView与toast消息问题

本文关键字:消息 问题 toast ListView Android | 更新日期: 2023-09-27 18:25:45

我有下一个问题,我有一个Activity1:Activity,带有以下代码:

private string[] items;
    public ListView lv;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        ListView lv = FindViewById<ListView>(Resource.Id.listView1);
          items = new string[] { "Aaaa", "bbb", "cccc", "dddd",    "eeee", "ffff", "gggg" };
        var ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items);
        lv.Adapter = ListAdapter;

    }

    protected void OnListClick(ListView listView, View view, int pos, long id)
    {
        var t = items[pos];
        Android.Widget.Toast.MakeText(this, t, Android.Widget.ToastLength.Short).Show();

    }
}

问题如下,当我调试应用程序时,点击一行,没有出现toast消息,什么都没有。。。。。。请帮忙。。。。。

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:orientation="horizontal"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="fill_parent"
p1:layout_height="fill_parent"
p1:id="@+id/linearLayout1">
<LinearLayout
    p1:id="@+id/layoutCalendar"
    p1:orientation="horizontal"
    p1:layout_width="200.0dp"
    p1:layout_height="fill_parent"
    p1:layout_gravity="right"
    p1:minWidth="25px"
    p1:minHeight="25px">
    <ListView
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="wrap_content"
        p1:layout_height="fill_parent"
        p1:id="@+id/listView1" />
   </LinearLayout>
   <calendarcontrol.CalendarMonthView
    p1:id="@+id/calendarView2"
    p1:clickable="true"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_gravity="right" />

Android ListView与toast消息问题

使用onItemSelectListener而不是onClick

 public class AppActivity extends Activity {    
            public Activity activity;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    activity = this;
                    ListView lv = FindViewById<ListView>(Resource.Id.listView1);
                    items = new string[] { "Aaaa", "bbb", "cccc", "dddd",    "eeee", "ffff", "gggg" };
                    var ListAdapter = new ArrayAdapter<String>(this, android.Resource.Layout.SimpleListItem1, items);
                    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
                         public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id) {  
                              var t = items[position];
                              Android.Widget.Toast.MakeText(activity, t, Android.Widget.ToastLength.Short).Show();
                               }
                        }); 
                    lv.Adapter = ListAdapter;
        }
    }