2013-08-01 45 views
0

我有下一个问题,我有一个活性1:活动, 用下面的代码:烤面包消息问题,Android的ListView

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


    } 
} 

的问题如下,当我调试的应用程序,并点击一排,没有出现敬酒消息,没什么......一些帮助请.....

的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" /> 

+0

大家好! :) –

回答

3

使用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; 

     } 

    } 
+0

嗨密码,我不能使用setOnItemSelectedListener,我只有setonclicklistener,因为我有一个Activity类不是一个列表Activity类。我认为这就是为什么我不能使用setOnItemSelectedListener的原因,因为我得到“不包含定义“setOnItemSelectedListener”“当我建立它。 –

+0

不定义类似于“protected void OnListClick(ListView listView,View view,int pos,long id)” 在lv.Adapter = ListAdapter之前添加此代码; (不要忘记把lv换成listview:D) – KEYSAN

+0

你可以请更具体些,我不太明白我应该做什么。对不起,谢谢! –