我使用此代码通过 buttonClick 事件从我的数据库中动态打印值。
用于删除数据库条目的按钮单击事件存在于循环内。
这是我的代码:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3);
final DatabaseHandler dbpin = new DatabaseHandler(this);
// Log.d("Reading: ", "Reading all tasks..");
List<Detail> detail1 = dbpin.getAllDetail();
Button[] button=new Button[1000];
for (Detail cn : detail1) {
String log = cn.getTitle();
final int i = cn.getID();
button[i] = new Button(this);
button[i].setText("Delete");
button[i].setTextSize(10);
button[i].setId(2000 + i);
int width = 80;
int height = 60;
TextView textview = new TextView(this);
textview.setText(log);
textview.setWidth(200);
textview.setTextSize(20);
textview.setPadding( 0, 10, 0, 0);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
}
button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), details.class);
Detail detail = new Detail();
detail.setID(i);
dbpin.deleteDetail(detail);
startActivityForResult(myIntent, 1);
}
});
}
在数据库处理程序代码之后,使用循环从数据库中检索所有详细信息:
// Getting All detail
public List<Detail> getAllDetail() {
List<Detail> detailList = new ArrayList<Detail>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DETAIL;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Detail detail = new Detail();
detail.setID(Integer.parseInt(cursor.getString(0)));
detail.setTitle(cursor.getString(1));
detail.setDetail(cursor.getString(2));
// Adding contact to list
detailList.add(detail);
} while (cursor.moveToNext());
}
// return contact list
return detailList;
}
// Deleting single detail
public void deleteDetail(Detail detail) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DETAIL, KEY_DETID + " = ?",
new String[] { String.valueOf(detail.getID()) });
db.close();
}
起初布局是正常的。删除第一个或最后一个数据行不会导致任何变化,但如果删除中间的一行,则布局相互重叠。
请给我建议以清除此逻辑错误。