恐怕您不会更新相关适配器项的实例。
如果您具有与此ListView适配器一起附加的字符串列表,则应使用想要显示的名称在相关位置初始化String项。然后调用Adapter.notifyDataSetChanged刷新具有更新列表的ListView中的所有UI元素。
看来,您永远不会更新适配器列表中的String实例
,
您的问题有两个方面。
- 首先,您不是从数据库中提取更新的数据(您可以假设基础数据已被更新,这是有风险的)。
- 您没有使用更改刷新Listview,因此它显示了它知道的数据。
我建议利用 CursorAdapter 来简化事务,因为Cursor Adapters是为与Cursor一起使用而设计的,并且由于Cursor通常位于适当的位置,因此它们尤其使所有数据都可以通过adpater轻松获得。行(例如,使用onItemClick和onItemLongClick时)
下面是一个基于您的代码的示例,该代码利用 SimpleCursorAdapter 来更改单击列表中的项目时的数据库和列表视图(以演示单击同一行而不是将测试更改为完成后,文本会反转,因此每次点击都会翻转。
但是,要使用Cursor Adapoter,您必须必须有一个名为 _id 的列,并且该列应该是唯一的整数(应为长整数,而不是int),并且通常,它是rowid的别名(即使用_id INTEGER PRIMARY KEY定义(带或不带AUTOINCREMENT,最好不带)。因此,存在一个常量BaseColumns._ID,其中包含值 _id 。
在这种情况下,首先 DatabaseHelper (SQLiteOpenHelper的子类) DatabaseHelper.java :-
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TABLE_NAME = "mytable";
/*<<<<<<<<<< id column must be _id (BaseColumns._ID) for Cursor Adapters >>>>>>>>>*/
public static final String COLID = BaseColumns._ID;
public static final String COL1 = "mycol1";
public static final String COL2 = "mycol2";
public DatabaseHelper(@Nullable Context context) {
super(context,DBNAME,null,DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
COLID + " INTEGER PRIMARY KEY," +
COL1 + " TEXT," +
COL2 + " TEXT " +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
}
public long insert(String col1,String col2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL1,col1);
cv.put(COL2,col2);
return db.insert(TABLE_NAME,cv);
}
public int doneName(String finishedName,int id,String oldName) {
/*
Alternative using the update convenience method
Based upon :-
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 + " = '" + finishedName + "' WHERE " + COL1 +
" = '" + id + "'" + " AND " + COL2 + " = '" + oldName + "'";
writes the SQL for you.
protects against SQL Injection
returns the number of rows updated
*/
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL2,finishedName);
return db.update(TABLE_NAME,cv,COLID + "=? AND " + COL2 + "=?",new String[]{String.valueOf(id),oldName});
}
public Cursor getAll() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(TABLE_NAME,null);
}
}
活动 MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView mListView;
DatabaseHelper th;
Cursor csr;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = this.findViewById(R.id.listview);
th = new DatabaseHelper(this);
addSomeTestData();
manageAdapter();
}
@Override
protected void onDestroy() {
super.onDestroy();
csr.close(); //<<<<<<<<<< Should always close Cursors when done with them
}
private void manageAdapter() {
/* This handles but the initialisation and the refreshing of the Listview */
/* First time it is called it initialises the Adapter and Listview */
/* On subsequent calls it refreshes the ListView */
csr = th.getAll();
if (adapter == null) {
adapter = new SimpleCursorAdapter(
this,android.R.layout.simple_expandable_list_item_2,csr,new String[]{
DatabaseHelper.COL1,DatabaseHelper.COL2
},new int[]{
android.R.id.text1,android.R.id.text2},0
);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
if (th.doneName(
/* reverses string to test multiple updates of the same row */
new StringBuilder(csr.getString(csr.getColumnIndex(DatabaseHelper.COL2))).reverse().toString()
/* "Done" */,(int )id /* NOTE ID IS PASSED to onItemClick FOR CURSOR ADAPTER */,csr.getString(csr.getColumnIndex(DatabaseHelper.COL2)) /* NOTE Oldname isn't required as ID will identify the row */
) > 0) {
manageAdapter(); //<<<<<<<<< after updating refresh the Cursor and the ListView
Toast.makeText(view.getContext(),"Updated OK.",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(view.getContext(),"Not Updated!!!",Toast.LENGTH_SHORT).show();
}
}
});
} else {
adapter.swapCursor(csr);
}
}
private void addSomeTestData() {
//Add some data but only if none exists
if (DatabaseUtils.queryNumEntries(th.getWritableDatabase(),DatabaseHelper.TABLE_NAME) > 0) return;
th.insert("TEST1 COL1","TEST1 COL2");
th.insert("TEST2 COL1","TEST2 COL2");
th.insert("TEST3 COL1","TEST3 COL2");
th.insert("TEST4 COL1","TEST4 COL2");
th.insert("TEST5 COL1","TEST5 COL2");
}
}
- 请注意,您可以将自己的布局与SimpleCursorAdapter一起使用(第4个(String [])和第5个(int [])用于指定数据来自的列以及ID的ID)数据进入到的各个视图)
结果:-
最初运行时,您会得到:-
单击一行,您会得到:-
如果您随后单击所有行,则会得到:-
以此类推。
本文链接:https://www.f2er.com/3159882.html