我有一个用SQL Lite表中的数据填充的列表视图,该列表在数据旁边有一个ImageView(垃圾桶图标),如何单击图像并删除特定列表视图单元格中的数据。 在我离开的图像示例和我的代码下面,添加按钮已经起作用。
示例:
主要代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:weightSum="2"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true">
>
<android.support.design.widget.TextInputEditText
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="@+id/txtNome"
android:layout_weight="1.5"
android:singleLine="true"/>
<Button
android:text="Add"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/btnIncluir" />
</LinearLayout>
<ListView
android:layout_weight="2.7"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/lvDados" android:paddingLeft="15dp" android:paddingRight="15dp" />
</LinearLayout>
ListviewLayout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="1"
android:columnCount="2" android:paddingRight="2dp" >
<TextView
android:id="@+id/txtvNome"
android:text="Nome"
android:textColor="#000000"
android:textSize="22sp"
android:paddingLeft="5dp"
android:layout_row="0"
android:layout_column="0" />
<ImageView
android:id="@+id/btnDeleteLv"
android:src="@drawable/delete"
android:layout_row="0"
android:layout_column="1" android:layout_gravity="end"
android:layout_width="28dp"
android:layout_height="28dp"
android:scaleType="fitXY" />
ListviewAdapter:
public class ListViewAdapter : BaseAdapter
{
private readonly Activity context;
private readonly List<Jogador> jogadores;
public ListViewAdapter(Activity _context, List<Jogador> _jogadores)
{
this.context = _context;
this.jogadores = _jogadores;
}
public override int Count
{
get
{
return jogadores.Count;
}
}
public override long GetItemId(int position)
{
return jogadores[position].Id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? context.LayoutInflater.Inflate(Resource.Layout.ListViewLayout, parent, false);
var lvtxtNome = view.FindViewById<TextView>(Resource.Id.txtvNome);
lvtxtNome.Text = jogadores[position].Nome;
return view;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
}
数据库:
public class DataBase
{
string pasta = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
public bool CriarBancoDeDados()
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
conexao.CreateTable<Jogador>();
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool InserirJogador(Jogador jogador)
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
conexao.Insert(jogador);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public List<Jogador> GetJogadores()
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
return conexao.Table<Jogador>().ToList();
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
public bool AtualizarJogador(Jogador jogador)
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
conexao.Query<Jogador>("UPDATE Jogador set Nome=?,Nivel=? Where Id=?", jogador.Nome, jogador.Nivel, jogador.Id);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool DeletarAluno(Jogador jogador)
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
conexao.Delete(jogador);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool GetJogador(int Id)
{
try
{
using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Jogadores.db")))
{
conexao.Query<Jogador>("SELECT * FROM Jogador Where Id=?", Id);
//conexao.Update(aluno);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
}
型号:
public class Jogador
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Nome { get; set; }
public int Nivel { get; set; }
}
答案 0 :(得分:1)
单击imageView时删除Listview项
您可以在adapter
的代码中添加删除项功能,例如:
public class FruitAdapter : BaseAdapter
{
private readonly Activity context;
private readonly List<Fruit> fruits;
public FruitAdapter(Activity _context, List<Fruit> _fruits)
{
this.context = _context;
this.fruits = _fruits;
}
public override int Count
{
get
{
return fruits.Count;
}
}
public override long GetItemId(int position)
{
return fruits[position].imageId;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
FruitHolder holder = null;
View view = convertView;
if (view == null)
{
holder = new FruitHolder();
view = context.LayoutInflater.Inflate(Resource.Layout.fruit_item, parent, false);
holder.FruittName = view.FindViewById<TextView>(Resource.Id.fruit_name);
holder.FruitButton = view.FindViewById<ImageView>(Resource.Id.fruit_image);
holder.FruitButton.Click += (object sender, EventArgs e) =>
{
fruits.RemoveAt(position);
NotifyDataSetChanged();
};
view.Tag = holder;
}
else
{
holder = (FruitHolder)view.Tag;
}
holder.FruittName.Text = fruits[position].Name;
holder.FruitButton.SetImageResource(fruits[position].imageId);
return view;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
private class FruitHolder : Java.Lang.Object
{
public TextView FruittName { get; set; }
public ImageView FruitButton { get; set; }
}
}
public class Fruit
{
public int imageId { get; set; }
public string Name { get; set; }
public Fruit(string name, int id)
{
imageId = id;
Name = name;
}
}
答案 1 :(得分:1)
好吧,我会这样做。
查找按钮ID在getView方法中添加按钮单击事件:
var button= view.FindViewById<ImageView>(Resource.Id.btnDeleteLv);
button.Click+= (s, e) =>
{
};
然后从列表中删除数据,通知我们的数据集已发生更改,并从SQLite中删除数据,如下所示:
jogadores.RemoveAt(position);
NotifyDataSetChanged();
sqliteConnectionObject.DeletarAluno<Jogador>(jogadores[position]);