gpt4 book ai didi

android - 如何在数据插入到 SQLite 后立即更新 ListView

转载 作者:行者123 更新时间:2023-11-29 15:07:08 25 4
gpt4 key购买 nike

我有一个名为 Custom 的类,它扩展了 fragment 。 OnCreateView 它将通过将 View v 传递给它来调用同一类中的 populateListView 方法。单击 fab 按钮,它将打开一个名为 CustomForm 的新 Activity ,该 Activity 允许用户填写表单,从而将数据插入 SQLite 数据库。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_custom, container, false);
openDB();

ListView myList = (ListView) v.findViewById(R.id.customlistview);
populateListView(v);
View fabbutton = v.findViewById(R.id.fab);
fabbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), CustomForm.class);
startActivity(myIntent);

}
});
return v;
}

public void populateListView(View v) {
Cursor cursor = dbAdapter.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_DRUG_NAME, DBAdapter.KEY_DRUG_OTHER_NAME, DBAdapter.KEY_DRUG_DOSE_1};
int[] toViewIDs = new int[]{R.id.lineone, R.id.linetwo, R.id.dose1};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.custom_row, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) v.findViewById(R.id.customlistview);
myList.setAdapter(myCursorAdapter);

}

这是 CustomForm 类

public class CustomForm extends AppCompatActivity {
View create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Drug Item Created", Toast.LENGTH_SHORT).show();
if (!TextUtils.isEmpty(custom_drug_name_main.getText())) {
dbAdapter.insertRow(custom_drug_name_main.getText().toString(), custom_drug_name_other.getText().toString(), dose1.getText().toString());

} else {

}
finish();



}
});
}

这是我的适配器类

public class DBAdapter {

private static final String TAG = "DBAdapter"; //used for logging database version changes

// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_DRUG_NAME = "drugName";
public static final String KEY_DRUG_OTHER_NAME = "drugOtherName";
public static final String KEY_DRUG_DOSE_1 = "dose1";

public static final String[] ALL_KEYS = new String[]{KEY_ROWID, KEY_DRUG_NAME, KEY_DRUG_OTHER_NAME,KEY_DRUG_DOSE_1};

// DataBase info:
public static final String DATABASE_NAME = "custom_drug";
public static final String DATABASE_TABLE = "custom_drug_table";
public static final int DATABASE_VERSION = 3; // The version number must be incremented each time a change to DB structure occurs.

//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_DRUG_NAME + " TEXT NOT NULL, "
+ KEY_DRUG_OTHER_NAME + " TEXT,"
+ KEY_DRUG_DOSE_1 + " TEXT NOT NULL"
+ ");";

private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}

// Close the database connection.
public void close() {
myDBHelper.close();
}

// Add a new set of values to be inserted into the database.
public long insertRow(String drugName, String drugOtherName, String dose1) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DRUG_NAME, drugName);
initialValues.put(KEY_DRUG_OTHER_NAME, drugOtherName);
initialValues.put(KEY_DRUG_DOSE_1, dose1);

// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String drugName, String drugOtherName) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_DRUG_NAME, drugName);
newValues.put(KEY_DRUG_OTHER_NAME, drugOtherName);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}


private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}

@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");

// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

// Recreate new database:
onCreate(_db);
}
}
}

我的问题是:如何从自定义类中调用 populateListView 方法,以便在插入数据后立即更新 ListView 。我是编程新手,希望我把问题说清楚了。谢谢。

最佳答案

更好的做法是创建一个 CursorAdapter,然后您只需调用 swapCursor():

adapter.swapCursor(cursor_update);

但在您的情况下,您有一个要从其他 Activity 访问的 SimpleCursorAdapter,因此您可以尝试这样做:

public class CustomForm extends AppCompatActivity {
View create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Drug Item Created", Toast.LENGTH_SHORT).show();
if (!TextUtils.isEmpty(custom_drug_name_main.getText())) {
dbAdapter.insertRow(custom_drug_name_main.getText().toString(), custom_drug_name_other.getText().toString(), dose1.getText().toString());

} else {

}
Intent myIntent = new Intent(getActivity(), yourLastActivity.class);
startActivity(myIntent);
}
});
}

这样做是为了调用 populateListView(v);当您返回上一个 Activity 时在 onCreateView() 中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_custom, container, false);
openDB();

ListView myList = (ListView) v.findViewById(R.id.customlistview);
populateListView(v); // we are trying to call this after the dbAdapter.insertRow

希望对你有帮助

关于android - 如何在数据插入到 SQLite 后立即更新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com