我有我的ListView和数据库适配器。如果用户点击listview中的某个地方,我想要一个对话框,如果用户点击“确定”按钮,我想删除用户点击的项目,这是怎么回事?
这是我的项目。我已经问了一些问题,非常感谢你们帮助我做这个项目非常感谢
这是我的listview .java文件
public class listview extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
SQLiteDatabase db = openOrCreateDatabase("Orders.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor crs = db.rawQuery("SELECT * FROM OrderedList", null);
ListView listView = (ListView) findViewById(R.id.listView);
String[] column1 = new String[crs.getCount()];
double[] column2 = new double[crs.getCount()];
int[] column3 = new int[crs.getCount()];
double[] v2 = new double[crs.getCount()];
int i = 0;
Double sum = 0.0;
int list_height = 0;
while (crs.moveToNext()) {
String item_name = crs.getString(crs.getColumnIndex("Food"));
double item_price = crs.getDouble(crs.getColumnIndex("Price"));
int temp_item_quantity = crs.getInt(crs.getColumnIndex("Quantity"));
column1[i] = item_name;
column2[i] = item_price;
column3[i] = temp_item_quantity;
v2[i] = column2[i];
sum += v2[i];
i++;
}
crs.close();
ListView_Adapter adapter = new ListView_Adapter(this, column1, column2, column3);
listView.setAdapter(adapter);
TextView ttotal = (TextView)findViewById(R.id.ttotal);
String initialtotal = String.valueOf(sum);
ttotal.setText("P " + initialtotal);
}
这是我的listview适配器
public class ListView_Adapter extends ArrayAdapter<String> {
private final Context context;
private String[] column1;
private double[] column2;
private int[] column3;
public ListView_Adapter(Context context, String[] column1, double[] column2, int[] column3) {
super(context, R.layout.list_adapter, column1);
this.context = context;
this.column1 = column1;
this.column2 = column2;
this.column3 = column3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_adapter, parent, false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.t1);
TextView t2 = (TextView) convertView.findViewById(R.id.t2);
TextView t3 = (TextView) convertView.findViewById(R.id.t3);
t1.setText(column1[position]);
t2.setText("₱" + column2[position]);
t3.setText("" + column3[position]);
return convertView;
}
}
这是我的databasehelper
public class MDB extends SQLiteOpenHelper {
public MDB(Context context, String dbname, CursorFactory factory, int dbversion) {
super(context, dbname, factory, dbversion);
}
SQLiteDatabase db;
Cursor crs;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists OrderedList(Food varchar, Price real, Quantity int);");
}
public void audittrail(){
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
答案 0 :(得分:1)
您可以为ListView编写onItemClickListener。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//call dialog
// you can set ok(positive) and cancel(negative) button
// for the ok button you can remove your listview item
//just do google
}
});