ListView Itemclick在xamarin android中不起作用

时间:2017-08-24 11:29:33

标签: c# listview xamarin.android

大家好我已经开发了一个xamarin android app.In那个应用程序我实现了list-view .List视图正确显示其项目但问题是列表视图项目点击不起作用。这是我的代码

Home.cs

 public class Home : Fragment
{
    public static List<User> Users1;
    public static ListView myList;
    public static string filename = "";
    public static WebView localWebView;
    public static FloatingActionButton fab;
    public static EditText et_key;
    List<AlbumTable> albtbl = new List<AlbumTable>();
      DatabaseHelper db;
   private  MyCustomListAdapter adaptor;
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.Main1, container, false);
        string storagePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/aaaa/index.html";
        fab = view.FindViewById<FloatingActionButton>(Resource.Id.fabaddalbum);
        myList = view.FindViewById<ListView>(Resource.Id.listalbumm);
        db = new DatabaseHelper();

        albtbl = db.getalldata();
        System.Console.WriteLine("Abhijitlist", "" + albtbl.Count());
        if (albtbl.Count() == 0)
        {
            Dialog dd = new Dialog(this.Activity);
            dd.SetContentView(Resource.Layout.addalbum);
            WindowManagerLayoutParams lp = new WindowManagerLayoutParams();
            lp.CopyFrom(dd.Window.Attributes);
            lp.Width = WindowManagerLayoutParams.MatchParent;
            lp.Height = WindowManagerLayoutParams.WrapContent;
            lp.Gravity = GravityFlags.Center;
            dd.Window.Attributes = lp;
            Button add = dd.FindViewById<Button>(Resource.Id.addalbum_btn);
            Button cancel = dd.FindViewById<Button>(Resource.Id.cancel_btn);
             et_key = dd.FindViewById<EditText>(Resource.Id.albumkey_et);

            add.Click += delegate {

                string key = ((EditText)et_key).ToString();
                System.Console.WriteLine("album key", "" + key);
                if (key != null)
                {

                     DatabaseHelper db = new DatabaseHelper();
             AlbumTable at = new AlbumTable()
             {


                 SR_ID = albtbl.Count+1+"",
                FileName="sadgfsdg"


         };
             db.InsertIntoTable(at);

                }
                dd.Dismiss();
            };

            dd.Show();
        }
        else
        {
            myList.Adapter = new MyCustomListAdapter(this.Activity, albtbl);
        }

        myList.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
        {
            string selectedFromList = myList.GetItemAtPosition(e.Position).ToString();
            var activity2 = new Intent(this.Activity, typeof(AlbumViewer));
            activity2.PutExtra("position", e.Position.ToString());

            StartActivity(activity2);
            System.Console.WriteLine(selectedFromList);
        };

        return view;
    }



    class MyCustomListAdapter : BaseAdapter
    {
        private Activity activity;
        private List<AlbumTable> albtbl;

        public MyCustomListAdapter(Activity activity, List<AlbumTable> albtbl)
        {
            this.activity = activity;
            this.albtbl = albtbl;
        }

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

        public override Java.Lang.Object GetItem(int position)
        {
            return albtbl[position].SR_ID;
        }

        public override long GetItemId(int position)
        {
            return Long.ParseLong(albtbl[position].SR_ID);
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;

            if (view == null)
            {
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.inflate_album, parent, false);

                var name = view.FindViewById<TextView>(Resource.Id.text1);
                Button remove = view.FindViewById<Button>(Resource.Id.btn_remove);
                view.Tag = new ViewHolder()
                {
                    Name = name,
                    Remove = remove
                };
            }

            var holder = (ViewHolder)view.Tag;

            holder.Name.Text = albtbl[position].SR_ID;
            holder.Remove.Click += delegate
            {
                DatabaseHelper db = new DatabaseHelper();
                AlbumTable at = new AlbumTable()
                {
                    SR_ID = albtbl[position].SR_ID,
                    FileName = albtbl[position].FileName,
                };
                db.DeleteFromTable(at);
                albtbl = db.getalldata();
                myList.Adapter = new MyCustomListAdapter(activity, albtbl);

            };

            return view;

        }
    }

}

}

这里我在数据库中添加一些数据,并根据数据显示列表视图.list-view正确显示其项目,删除和共享按钮也工作正常但列表视图项目单击不起作用...

我上周面对这个问题,请帮助我。

0 个答案:

没有答案