gpt4 book ai didi

java - 需要帮助刷新我的应用程序中的数据库 ListView

转载 作者:行者123 更新时间:2023-12-02 12:06:43 25 4
gpt4 key购买 nike

我的应用布局显然不是正常布局,因此我无法将列表适配器设置为在进行编辑时自动更新。

我在这个 Java 文件中对数据库进行编辑,该文件由其自己的 Activity 和布局控制。

    public void onClick(View view){
if (view == findViewById(R.id.addsave)) {
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
if (editTextName.getText().toString().equals("")) {
editTextName.setError("Recipe name required!");
return;
} else {
recipe.name = editTextName.getText().toString();
}
if (textImagePath.getText().toString().equals("") ) {
recipe.image = ("");
}else{
recipe.image = textImagePath.getText().toString();
}
recipe.category = staticSpinner.getSelectedItem().toString();
if (editTextIngredients.getText().toString().equals("")) {
editTextIngredients.setError("Ingredient required!");
return;
} else {
recipe.ingredients = editTextIngredients.getText().toString();
}
if (editTextInstruct.getText().toString().equals("")) {
editTextIngredients.setError("Instruction required!");
return;
} else {
recipe.instructions = editTextInstruct.getText().toString();
}
recipe.cooktemp = editTextCookTemp.getText().toString();
recipe.cooktime = editTextCookTime.getText().toString();
recipe.serves = editTextServings.getText().toString();
recipe.recipe_Id = _Recipe_Id;

if (_Recipe_Id == 0) {
_Recipe_Id = repo.insert(recipe);

Toast.makeText(this, "New Recipe Added", Toast.LENGTH_SHORT).show();
finish();

它实际上在这个java文件中插入和更新

    int insert(Recipe recipe){

//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Recipe.KEY_SERVES, recipe.serves);
values.put(Recipe.KEY_COOKTIME, recipe.cooktime);
values.put(Recipe.KEY_COOKTEMP, recipe.cooktemp);
values.put(Recipe.KEY_INSTRUCT, recipe.instructions);
values.put(Recipe.KEY_INGREDIENTS, recipe.ingredients);
values.put(Recipe.KEY_CATEGORY, recipe.category);
values.put(Recipe.KEY_IMAGE, recipe.image);
values.put(Recipe.KEY_NAME, recipe.name);

//Inserting Row
long recipe_Id = db.insert(Recipe.TABLE, null, values);
db.close();// Closing database connection
return (int) recipe_Id;
}

void delete(int recipe_Id){

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(Recipe.TABLE, Recipe.KEY_ID + "=?", new String[] {String.valueOf(recipe_Id)});
db.close();
}

void update(Recipe recipe){

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(Recipe.KEY_SERVES, recipe.serves);
values.put(Recipe.KEY_COOKTIME, recipe.cooktime);
values.put(Recipe.KEY_COOKTEMP, recipe.cooktemp);
values.put(Recipe.KEY_INSTRUCT, recipe.instructions);
values.put(Recipe.KEY_INGREDIENTS, recipe.ingredients);
values.put(Recipe.KEY_CATEGORY, recipe.category);
values.put(Recipe.KEY_IMAGE, recipe.image);
values.put(Recipe.KEY_NAME, recipe.name);

db.update(Recipe.TABLE, values, Recipe.KEY_ID + "=?", new String[]{String.valueOf(recipe.recipe_Id)});
db.close();
}

最后它从这个 Java 文件和单独的布局中放入 ListView 中。这是我的适配器所在的位置,但我根本无法让 notificationDataSetChanged() 在这里工作...因为它甚至不会出现。

     public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
RecipeRepo repo = new RecipeRepo(this);

if (id == R.id.nav_meat) {

final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeMeat();


if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt(recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter(SousChef.this, recipeList, R.layout.view_recipe_entry, new String[]{"id", "category", "name"}, new int[]{R.id.recipe_Id, R.id.recipe_list_category, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.nav_veg) {

final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeVeg();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt(recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter(SousChef.this, recipeList, R.layout.view_recipe_entry, new String[]{"id", "category", "name"}, new int[]{R.id.recipe_Id, R.id.recipe_list_category, R.id.recipe_list_name});
lv.setAdapter(adapter);

}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}

因此,任何有关将其设置为自动更新的建议都会有很大帮助。我已经为此绞尽脑汁好几天了,现在正在查看不同的示例以及其他示例,但是没有一个设置能像这个那样不允许我将所有内容都放在一个文件中。

提前谢谢您。

类别选择图像: Category picking Image

最佳答案

肯定还有更多答案,但这一个可能会有所帮助,

<强> Quick Example for the proposed solution

简短说明

MainActivity

//create a public static adapter
public static ListAdapter adapter

内部onCreateView()

//Create your adapter and set it to the right ListView
ListView lv = findViewById(R.id.listView_in_xml);
adapter = new SimpleAdapter(...)
lv.setAdapter(adapter)

CustomAdapter里面,在你的情况下我假设是SimpleAdapter

//add a public method to be called so that the Adapter updates and displays the new data
public void updateMethod(){
//update your List<Recipe> that I would guess you have calling the database again
//if needed update your getCount() return value so that it returns the number of childs in your ListView which most of the cases is just the List<Recipe>.size()
//notifyDataSetChanged()
}

在您的DB HANDLER CLASS

//in every update, add, delete or any method that requires the ListView to Update just call the created method,
MainActivity.CustomAdapter.updateMethod();

问题

您必须确保公共(public)静态适配器已初始化且不为空,或者只需检查适配器是否不null 并更新,因为如果适配器为 null,则该 Activity 尚未启动,因此无需触发 updateMethod()

其他解决方案

不要创建一个公共(public)静态适配器,而是创建一个公共(public)静态 boolean 值,然后每当数据发生更改时,都会从数据库将该 boolean 值设置为 true。最后,每当您恢复 Activity 时,都会检查该 boolean 值并根据需要更新您的 ListViewAdapter。

我知道可以使用的更复杂的解决方案,因为我使用它

使用 TaskAsyncTaskLoader,它在 MainActivity 中利用 Loader 并实现 LoaderManager.LoaderCallbacks

(可选)您可以将 Loader 设置为 public static Loader,并在 DBHandler 中触发加载器再次加载数据,或者使用您想要的任何其他逻辑。

工作证明建议的解决方案, enter image description here

关于java - 需要帮助刷新我的应用程序中的数据库 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860370/

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