C# 单击ListView中的项目不会';无法使用mListView.com侦听器
我已经搜索并组合了在Stack Overflow和over the net中找到的解决方案,myC# 单击ListView中的项目不会';无法使用mListView.com侦听器,c#,android,xml,listview,onitemclicklistener,C#,Android,Xml,Listview,Onitemclicklistener,我已经搜索并组合了在Stack Overflow和over the net中找到的解决方案,mymListView.OnItemClickListener根本不起作用 MainActivity.cs: protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var libraryDbContext = new LibraryDbContext();
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);
}
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);
}
}
}
BookListAdapter.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);
}
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);
}
}
}
名称空间App1
{
类BookListAdapter:BaseAdapter/,ListView.IOnItemClickListener
{
私有上下文;
私密语境;
私人内部布局;
私人清单MBook;
选择私人行动;
私人职位;
私有SQLiteConnection db=newsqliteconnection(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal),“LibraryDbContext”);
public BookListAdapter(上下文上下文、int布局、选定操作、列出书籍)
{
mMainActivityContext=上下文;
mContext=上下文;
mLayout=布局;
mBooks=书籍;
mActionPicSelected=picSelected;
}
public override LibraryDbModel.Books此[int位置]
{
获取{return mBooks[position];}
}
公共覆盖整数计数
{
获取{return mBooks.Count;}
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
if(行==null)
{
行=布局更平坦。从(mContext)。充气(mLayout,父项,false);
}
var getBooksAuthors=db.Table().ToList();
var getAuthors=new List();
getBooksAuthors.ForEach(ba=>getAuthors.Add(db.Table().Where(a=>a.Id==ba.AuthorId&&a.Delete==false.SingleOrDefault());
row.findviewbyd(Resource.Id.txtBook).Text=mBooks[position].Title;
row.FindViewById(Resource.Id.txtAuthor).Text=getAuthors.FirstOrDefault().LastName+“等(“+mBooks[position].Copyright.Year+”)”;
row.findviewbyd(Resource.Id.txtSynopsis).Text=mBooks[position].概要;
ImageView pic=row.findviewbyd(Resource.Id.imgPic);
row.Clickable=true;
row.Focusable=true;
row.FocusableInTouchMode=true;
pic.Click-=pic\u Click;
pic.Click+=pic\u Click;
返回行;
}
无效pic_单击(对象发送者,事件参数e)
{
//图片已被点击
mActionPicSelected.Invoke((图像视图)发送方);
}
}
}
显示您的适配器代码我添加了我的BookListAdapter.cs高级版谢谢!显示您的适配器代码我添加了我的BookListAdapter.cs高级版谢谢!