如何点击listview中的项目不会点击mListView.OnItemClickListener

时间:2015-11-19 04:24:44

标签: c# android xml listview onitemclicklistener

甚至有多少次我搜索并组合了我在stackoverflow和网上找到的solutinos,我的mListView.OnItemClickListener就不会工作了。任何帮助将受到高度赞赏。谢谢!

MainActivity.cs

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        var libraryDbContext = new LibraryDbContext();

        if(libraryDbContext.HasDatabase() == false)
        {
            Toast.MakeText(this, "Creating and Intitializing Database", ToastLength.Long).Show();
        }
        SetContentView(Resource.Layout.MainLayout);
        mSearch = FindViewById<EditText>(Resource.Id.etSearch);
        mListView = FindViewById<ListView>(Resource.Id.listView);
        mContainer = FindViewById<LinearLayout>(Resource.Id.llContainer);

        Action<ImageView> action = PicSelected;

        mSearch.Focusable = false;
        mSearch.FocusableInTouchMode = false;
        mSearch.ClearFocus();
        mSearch.Alpha = 0; 
        mSearch.TextChanged += MSearch_TextChanged;





        mBooks = db.Table<LibraryDbModel.Books>().Where(b => b.Delete == false).ToList();

        mAdapter = new BookListAdapter(this, Resource.Layout.row_book, action, mBooks);
        mListView.Adapter = mAdapter;

        mListView.DescendantFocusability = DescendantFocusability.BlockDescendants;
        mListView.Focusable = true;
        mListView.ChoiceMode = ChoiceMode.None;
        mListView.ItemsCanFocus = true;

        mListView.OnItemClickListener = this;


        var item = mListView.IsFocused;

        Toast.MakeText(this, item.ToString(), ToastLength.Long).Show();
    }


    public void OnItemClick(AdapterView parent, View view, int position, long id)
    {
            var getBooksAuthors = db.Table<LibraryDbModel.BooksAuthors>().ToList();
            var getAuthors = new List<LibraryDbModel.Authors>();
            getBooksAuthors.ForEach(ba => getAuthors.Add(db.Table<LibraryDbModel.Authors>().Where(a => a.Id == ba.AuthorId && a.Delete == false).SingleOrDefault()));

            var book = mAdapter[position];

            var DialogViewBookActivity = new Intent(this, typeof(DialogViewBook));
            DialogViewBookActivity.PutExtra("title", book.Title);
            DialogViewBookActivity.PutExtra("author", getAuthors.FirstOrDefault().LastName);
            DialogViewBookActivity.PutExtra("year", book.Copyright.Year);
            DialogViewBookActivity.PutExtra("sypnosis", book.Synopsis);


            StartActivity(DialogViewBookActivity);
        }

这是我的MainActivity的 XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_right">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:id="@+id/llContainer"
        android:focusable="true"
        android:focusableInTouchMode="true">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/etSearch"
            android:hint="Search Books"
            android:background="#FFFFFF"
            android:textColor="#000000"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginTop="8dp" />
        <View
            android:id="@+id/invi"
            android:layout_width="match_parent"
            android:layout_height="2dp" />
        <ListView
            android:layout_below="@id/invi"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:minWidth="25px"
            android:minHeight="25px" />
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="#FF8B1A"
        android:textColor="#000000"
        android:id="@+id/fakeActionBar">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ImageView
                android:src="@drawable/ic_action_preferences"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:id="@+id/imgPreferences"
                android:layout_gravity="left"
                android:scaleType="fitCenter"
                android:showAsAction="always"
                android:layout_marginLeft="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtTitle"
                android:text="Library"
                android:layout_gravity="left"
                android:showAsAction="always"
                android:layout_marginLeft="10dp"
                android:textSize="30sp"
                android:textColor="#FFFFFF"
                android:layout_toRightOf="@id/imgPreferences"
                android:layout_centerVertical="true" />
        </RelativeLayout>
        <ImageView
            android:src="@drawable/ic_action_search"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:id="@+id/imgSearch"
            android:layout_gravity="right"
            android:scaleType="fitCenter"
            android:showAsAction="always"
            android:layout_marginRight="50dp" />
        <ImageView
            android:src="@drawable/ic_action_download"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:id="@+id/imgDownload"
            android:layout_gravity="right"
            android:scaleType="fitCenter"
            android:showAsAction="always"
            android:layout_marginRight="10dp" />
    </FrameLayout>
</FrameLayout>

这是我的 BookListAdapter.cs

namespace App1
{
    class BookListAdapter : BaseAdapter<LibraryDbModel.Books>//, ListView.IOnItemClickListener
    {
        private Context mContext;
        private Context mMainActivityContext;
        private int mLayout;
        private List<LibraryDbModel.Books> mBooks;
        private Action<ImageView> mActionPicSelected;
        private int position;


        private SQLiteConnection db = new SQLiteConnection(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LibraryDbContext"));

        public BookListAdapter(Context context, int layout, Action<ImageView> picSelected, List<LibraryDbModel.Books> books)
        {
            mMainActivityContext = context;
            mContext = context;
            mLayout = layout;
            mBooks = books;
            mActionPicSelected = picSelected;
        }

        public override LibraryDbModel.Books this[int position]
        {
            get { return mBooks[position]; }
        }

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

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

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

            if (row == null)
            {
                row = LayoutInflater.From(mContext).Inflate(mLayout, parent, false);
            }

            var getBooksAuthors = db.Table<LibraryDbModel.BooksAuthors>().ToList();
            var getAuthors = new List<LibraryDbModel.Authors>();
            getBooksAuthors.ForEach(ba => getAuthors.Add(db.Table<LibraryDbModel.Authors>().Where(a => a.Id == ba.AuthorId && a.Delete == false).SingleOrDefault()));

            row.FindViewById<TextView>(Resource.Id.txtBook).Text = mBooks[position].Title;
            row.FindViewById<TextView>(Resource.Id.txtAuthor).Text = getAuthors.FirstOrDefault().LastName + " et al (" + mBooks[position].Copyright.Year + ")";
            row.FindViewById<TextView>(Resource.Id.txtSynopsis).Text = mBooks[position].Synopsis;
            ImageView pic = row.FindViewById<ImageView>(Resource.Id.imgPic);

            row.Clickable = true;
            row.Focusable = true;
            row.FocusableInTouchMode = true;

            pic.Click -= pic_Click;
            pic.Click += pic_Click;

            return row;
        }

        void pic_Click(object sender, EventArgs e)
        {
            //Picture has been clicked
            mActionPicSelected.Invoke((ImageView)sender);
        }


    }
}

先谢谢!

0 个答案:

没有答案