我想帮助解决以下问题:我有一个AlertDialog,它似乎带有一个sqlite列表,做了一个布局xml来加载这个列表,并且布局包含一个带垃圾桶的ImageView。我创建了代码public void deleteClick(View v){其中显示了另一个AlertDialog,询问您是否确定要排除。但是我无法获得被点击的垃圾的位置。
以下是一些代码片段:
public void FavClick(View v){
SQLiteDatabase db = openOrCreateDatabase("rmtc.db", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM favpontos ORDER BY apelido ASC", null);
String[] from = {"apelido", "ponto"};
int[] to = {R.id.txvApelido, R.id.txvPonto};
final SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.favoritos, cursor, from, to);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Pontos favoritos");
builder.setIcon(R.drawable.ic_logo_white);
builder.setAdapter(ad, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something with the selection
int posicao = (int) ad.getItemId(id);
SQLiteDatabase banco = openOrCreateDatabase("rmtc.db", Context.MODE_PRIVATE, null);
Cursor c = banco.rawQuery("SELECT * FROM favpontos WHERE _id = "+ posicao, null);
while(c.moveToNext()){
EditText txtPonto = (EditText)findViewById(R.id.txtPonto);
txtPonto.setText(c.getString(c.getColumnIndex("ponto")));
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void deleteClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Deletar ponto favorito")
.setMessage("Tem certeza que deseja deletar este ponto?")
.setIcon(R.drawable.ic_logo_white)
.setPositiveButton("Deletar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "( " +POSITION????+ " )", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
和xml layout:favoritos.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_menu_delete"
android:onClick="deleteClick" />
<TextView
android:id="@+id/txvApelido"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txvPonto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txvApelido"
android:layout_toLeftOf="@+id/imageView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
谢谢!
原谅英语,我来自巴西