Listview Itemclick事件没有触发android xamarin

时间:2016-04-09 07:54:35

标签: android android-fragments xamarin xamarin.android xamarin-studio

public override Java.Lang.Object InstantiateItem (ViewGroup container, int position)
{
    View view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.pager_item, container, false);
    container.AddView (view);
     navlistArray = new List<jtaskItem>();
    string apiUrl = MainActivity.GetData("http://"+MainActivity.IPAddress+"/api/jtasks/GetItemList?uName=admin&tgId=1");
    JSONArray obj = new JSONArray(apiUrl);
    //JSONObject obj = new JSONObject(apiUrl);
    for (int i = 0; i < obj.Length(); i++)
    {
        JSONObject e = obj.GetJSONObject(i);
        navlistArray.Add(new jtaskItem() { ID = e.GetString("ID"), Title = e.GetString("Title"), DueDate = e.GetString("DueDate")});
        //navlistArray.Add(e.GetString("Title"));

    }

    //loading listview
    //TextView txtTitle = view.FindViewById<TextView> (Resource.Id.item_title);
    listView = view.FindViewById <ListView> (Resource.Id.list);
    MyListAdapter adapter = new MyListAdapter(container.Context, navlistArray);
    //ArrayAdapter adapter = new ArrayAdapter<string>(container.Context, Resource.Layout.pager_item, Resource.Id.item_title, navlistArray);
    listView.Adapter = adapter;
    listView.ItemClick += listView_ItemClick;


    return view;
}

void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{

    Console.Write(navlistArray[e.Position].ID);
}

这是我的代码,请帮忙,当我点击lisview时,Itemclick函数没有调用请告诉我我在哪里做错了。

public class MyListAdapter:BaseAdapter<jtaskItem>{

    public List<jtaskItem> mItems;
    public Context mContext;     

    public MyListAdapter(Context context, List<jtaskItem> items)
    {
        mItems = items;
        mContext = context;            
    }

    public override int Count
    {
        get { return mItems.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override jtaskItem this[int position]        {

        get { return mItems[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)  
    {
        View row=convertView;
        if (row == null)
        {
             row=LayoutInflater.From(mContext).Inflate(Resource.Layout.List_Item, null,false);                  
        }

        TextView taskTitle=row.FindViewById<TextView>Resource.Id.TaskTitle);
        taskTitle.Text = mItems[position].Title;
        TextView dueOn = row.FindViewById<TextView>(Resource.Id.DueOn);
        dueOn.Text = mItems[position].DueDate;
        TextView id = row.FindViewById<TextView>(Resource.Id.ID);
        id.Text = mItems[position].ID;
        CheckBox mycheckBox=row.FindViewById<CheckBox>Resource.Id.checkBox);
        mycheckBox.Checked = true;

        return row;
    }
}

2 个答案:

答案 0 :(得分:1)

您的适配器必须继承BaseAdapter<T>ArrayAdapter<T>

public class MyListAdapter : BaseAdapter<jtaskItem> { /* ... */ }

请参阅:https://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_2_-_populating_a_listview_with_data/#Implementing_a_ListAdapter

答案 1 :(得分:0)

问题是Android不允许您选择包含可关注元素的列表项。

修改列表项上的复选框,使其具有如下属性:

android:focusable="false"