单击Listview项时,应从SQLite数据库中使该Listview项的特定数据可用,但所有Listview项的数据均可用。我该如何纠正?
Main2Activity:
fragment
}
DatabaseHelper: 这些是我的表格:
RecycleView
我的insertData函数:
position = intent.getIntExtra("position",0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = listView.getItemAtPosition(position).toString();
Toast.makeText(Main2Activity.this, ""+text, Toast.LENGTH_SHORT).show();
}
});
add_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String place = editText.getText().toString();
if(!place.equals("")&& databaseHelper1.insertData1(place)){
editText.setText("");
listItem.clear();
viewData1();
} else
{
Toast.makeText(Main2Activity.this, "Data not added", Toast.LENGTH_SHORT).show();
}
}
});
}
private void viewData1() {
Cursor cursor = databaseHelper1.viewData1(position);
if(cursor.getCount()==0){
Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show();
}else
{
while(cursor.moveToNext()){
listItem.add(cursor.getString(0));
}
listView.setAdapter(adapter);
}
我的viewData函数:
private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
+ " TEXT " + ")";
private static final String CREATE_TABLE1 = "CREATE TABLE " + DB_TABLE1 + " (" + ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
PLACE + " TEXT " + " , " + ID + " INTEGER REFERENCES " + DB_TABLE + ")";
使用此代码,我可以从数据库中获得对Listitem位置可用的所有数据,而不管它是位置0还是1。 实际输出应该是每个数据都与单击的项目一致。
答案 0 :(得分:0)
要从ListView中访问项目,请设置ListView的 onItemClickListener ,然后调用 onItemClick 方法。
onItemClick 传递了4个参数:-
由于您没有使用CursorAdapter,而是可以使用List / ArrayListAdapter然后通过Adapter的getItemAtPositition方法使用position来检索所选项目的字符串。如果该String是唯一的,则可以使用它来检索与该项目关联的行。如果不是唯一的,则没有真正的方法可以确切地确定与该项目相关联的行。
这是仅使用/提取要显示的字符串的问题之一,这不理想,并且还需要额外的工作。
您可以在从游标中提取字符串的同时,提取 id 并将其放入一个long型数组,然后根据位置访问此long型数组。使用 id ,然后您可以查询表以从数据库中获取该行。
您可以具有一个类,该类定义代表行的对象,包括行的 id ,然后具有ArrayList,在这种情况下,getItemAtPosition方法可以返回包含的对象id (也许所有值都不需要再访问数据库)。
也许最简单的方法是利用游标适配器,在这种情况下,游标将可用(并适当放置),第4个参数将是所选项目的 id 。但是,这要求 id 列使用特定的列名,即该列必须命名为 _id 。这可以通过重命名 ID1 列或使用 AS 关键字替换地提供 ID1 和别名来实现。
您的代码似乎有一些问题,并且有很多遗漏。因此,这是一个基于您的代码的有效示例。
此示例将显示具有5行的ListView,每一行均由根据关系的两个表中的数据组成。单击一个项目将吐出基础值。
DatabaseHelper.java:-
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String DB_TABLE = "dbtable";
public static final String DB_TABLE1 = "dbtable1";
public static final String COL_TABLE_ID = BaseColumns._ID;
public static final String COL_TABLE_NAME = "name";
public static final String COL_TABLE1_ID1 = BaseColumns._ID;
public static final String COL_TABLE1_ID1_ALTERNATIVE = DB_TABLE1 + "_" + COL_TABLE1_ID1; //alternative column name so no ambiguity
public static final String COl_TABLE1_PLACE = "place";
public static final String COL_TABLE1_ID = "id";
SQLiteDatabase mDB;
//Note AUTOINCREMENT removed not needed/inefficient
private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (" +
COL_TABLE_ID + " INTEGER PRIMARY KEY, " +
COL_TABLE_NAME + " TEXT " +
")";
private static final String CREATE_TABLE1 = "CREATE TABLE " + DB_TABLE1 + " (" +
COL_TABLE1_ID1 + " INTEGER PRIMARY KEY, " + //(limits 1 to 1 relationship, due to needing to be unique)
COl_TABLE1_PLACE + " TEXT " + " , " +
COL_TABLE1_ID + " INTEGER REFERENCES " + DB_TABLE + "(" +
COL_TABLE_ID +
")" +
")";
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
// Foreign kjey support need to be turned on
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long insertTableRow(String name) {
ContentValues cv = new ContentValues();
cv.put(COL_TABLE_NAME,name);
return mDB.insert(DB_TABLE,null,cv);
}
public long insertTable1Row(long table_reference, String place) {
ContentValues cv = new ContentValues();
cv.put(COL_TABLE1_ID,table_reference);
cv.put(COl_TABLE1_PLACE,place);
return mDB.insert(DB_TABLE1,null,cv);
}
public Cursor getAllFromBothTables() {
String table = DB_TABLE + " JOIN " + DB_TABLE1 + " ON " + DB_TABLE + "." + COL_TABLE_ID + " = " + DB_TABLE1 + "." + COL_TABLE1_ID;
String[] columns = new String[]{
DB_TABLE + "." + COL_TABLE_ID,
DB_TABLE + "." + COL_TABLE_NAME,
DB_TABLE1 + "." + COL_TABLE1_ID1 + " AS " + COL_TABLE1_ID1_ALTERNATIVE,
DB_TABLE1 + "." + COl_TABLE1_PLACE,
DB_TABLE1 + "." + COL_TABLE1_ID
};
return mDB.query(table,columns,null,null,null,null,null);
}
}
Main2Activity.java
public class Main2Activity extends AppCompatActivity {
DatabaseHelper DatabaseHelper1;
ListView listview;
SimpleCursorAdapter mSCA;
Cursor mCsr;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_main2);
listview = this.findViewById(R.id.mylistview);
DatabaseHelper1 = new DatabaseHelper(this);
addSomeTestData();
handleListView();
}
private void addSomeTestData() {
if (DatabaseUtils.queryNumEntries(DatabaseHelper1.getWritableDatabase(),DatabaseHelper.DB_TABLE) > 0) return;
DatabaseHelper1.insertTableRow("Fred");
DatabaseHelper1.insertTableRow("Mary");
DatabaseHelper1.insertTableRow("Sue");
DatabaseHelper1.insertTable1Row(1,"London");
DatabaseHelper1.insertTable1Row(2,"New York");
DatabaseHelper1.insertTable1Row(3,"Sydney");
DatabaseHelper1.insertTable1Row(1,"Paris");
DatabaseHelper1.insertTable1Row(1,"Toronto");
}
private void handleListView() {
mCsr = DatabaseHelper1.getAllFromBothTables();
DatabaseUtils.dumpCursor(mCsr);
if (mSCA == null) {
mSCA = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,mCsr,
new String[]{
DatabaseHelper.COL_TABLE_NAME,
DatabaseHelper.COl_TABLE1_PLACE
},
new int[]{android.R.id.text1,android.R.id.text2},
0
);
listview.setAdapter(mSCA);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(
mContext,
"You clicked on name = " + mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COL_TABLE_NAME)) +
" place = " + mCsr.getString(mCsr.getColumnIndex(DatabaseHelper.COl_TABLE1_PLACE)) +
" id of TABLE is " + String.valueOf(l) +
" id of TABLE from cursor is " + String.valueOf(mCsr.getLong(mCsr.getColumnIndex(DatabaseHelper.COL_TABLE_ID))) +
" id of TABLE1 is " + String.valueOf(mCsr.getLong(mCsr.getColumnIndex(DatabaseHelper.COL_TABLE1_ID1_ALTERNATIVE))),
Toast.LENGTH_SHORT).show();
}
});
} else {
mSCA.swapCursor(mCsr);
}
}
}
运行以上命令后,它看起来像:-