当我要点击列表项时,我想获取数据库行ID。在我的自定义列表视图adaper中,我使用onClickListener,从那里我想获得ID。请任何人帮助我....
这是我的自定义列表视图适配器
public class Quiz_list_adapter extends BaseAdapter{
Context context;
ArrayList<String> aL1 = new ArrayList<>();
ArrayList<String> aL2 = new ArrayList<>();
ArrayList<String> aL3 = new ArrayList<>();
ArrayList<String> aL4 = new ArrayList<>();
ArrayList<String> aL5 = new ArrayList<>();
ArrayList<String> aL6 = new ArrayList<>();
ArrayList<String> aL7 = new ArrayList<>();
QuizDatabaseHelper myDb;
private static LayoutInflater inflater = null;
public Quiz_list_adapter(QuizReminder mainAct, ArrayList<String> a1,ArrayList<String> a2,ArrayList<String> a3,
ArrayList<String> a4,ArrayList<String> a5,ArrayList<String> a6,ArrayList<String> a7){
aL1 = a1;
aL2 = a2;
aL3 = a3;
aL4 = a4;
aL5 = a5;
aL6 = a6;
aL7 = a7;
context = mainAct;
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
myDb = new QuizDatabaseHelper(context);
}
@Override
public int getCount() {
return aL1.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class MyHolder{
TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final MyHolder myHolder = new MyHolder();
final View myView;
myView = inflater.inflate(R.layout.custom_quiz_layout, null);
myHolder.tv1 = (TextView) myView.findViewById(R.id.title);
myHolder.tv2 = (TextView) myView.findViewById(R.id.details);
myHolder.tv3 = (TextView) myView.findViewById(R.id.hour);
myHolder.tv4 = (TextView) myView.findViewById(R.id.minute);
myHolder.tv5 = (TextView) myView.findViewById(R.id.day);
myHolder.tv6 = (TextView) myView.findViewById(R.id.month);
myHolder.tv7 = (TextView) myView.findViewById(R.id.year);
myHolder.tv1.setText(aL1.get(position));
myHolder.tv2.setText(aL2.get(position));
myHolder.tv3.setText(aL3.get(position));
myHolder.tv4.setText(aL4.get(position));
myHolder.tv5.setText(aL7.get(position));
myHolder.tv6.setText(aL3.get(position));
myHolder.tv7.setText(aL5.get(position));
myView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer deletedRow = myDb.deleteData(String.valueOf(position));
aL1.remove(position);
notifyDataSetChanged();
if(deletedRow >0){
Toast.makeText(context, "Data Deleted !", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(context,"Data not Deleted !", Toast.LENGTH_LONG).show();
}
}
});
return myView;
}
}
这是我的数据库助手类
public class QuizDatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "quiz.db";
public static final String TABLE_NAME = "quiz_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TITLE";
public static final String COL_3 = "DETAILS";
public static final String COL_4 = "HOUR";
public static final String COL_5 = "MINUTE";
public static final String COL_6 = "YEAR";
public static final String COL_7 = "MONTH";
public static final String COL_8 = "DAY";
public QuizDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"TITLE TEXT, DETAILS TEXT, HOUR TEXT, MINUTE TEXT, YEAR TEXT, MONTH TEXT, DAY TEXT)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String title, String details, String hour, String minute,
String year, String month, String day){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, details);
contentValues.put(COL_4, hour);
contentValues.put(COL_5, minute);
contentValues.put(COL_6, year);
contentValues.put(COL_7, month);
contentValues.put(COL_8, day);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}
else{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " +TABLE_NAME, null);
return result;
}
public boolean updateData(String id, String title, String details, String hour,
String minute, String year, String month, String day){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, title);
contentValues.put(COL_3, details);
contentValues.put(COL_4, hour);
contentValues.put(COL_5, minute);
contentValues.put(COL_6, year);
contentValues.put(COL_7, month);
contentValues.put(COL_8, day);
db.update(TABLE_NAME,contentValues, "ID = ?", new String[] { id });
return true;
}
public Integer deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME,"ID = ?",new String[] {id});
}
}
答案 0 :(得分:0)
在QuizDataBaseHelper中添加此方法
public Integer getId(int position){
int id = 0;
Cursor cur = db.rawQuery("SELECT * FROM " +TABLE_NAME, null);
cur.moveToPosition(position);
id = cur.getInteger(1);
cur.close();
return id;
}
在您的商品点击上调用它;我没有执行它,但这是获取数据的方法,如果有任何问题,请在评论中提及它们。
答案 1 :(得分:0)
在adpater之外使用setOnItemClickListener
而不是onClickListener,即在相关活动中。然后提供 id (假设光标适配器)。
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position,
long id) {
......
}
});
类似于LongClick的。