androidandroid-fragmentsxamarinxamarin.androidxamarin-studio

Listview Itemclick event not firing android xamarin


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

Here is my code please help, When i am click on the lisview Itemclick function is not calling Please tell me where am i doing mistake.

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

Solution

  • Your Adapter has to inherit from BaseAdapter<T> or ArrayAdapter<T>.

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

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