gpt4 book ai didi

android - 刷新listview/显示新添加的数据到listview android

转载 作者:行者123 更新时间:2023-11-30 03:20:42 25 4
gpt4 key购买 nike

我是 Android Sqlite DB Dev' 的新手,所以我需要你们的帮助。我能够进行基本的 CRUD 操作。现在我想知道如何刷新 ListView 来显示新添加的数据。我查看了 adapter.notifyDataSetChanged 但我想知道为什么它对我不起作用。这是我到目前为止尝试过的方法

   private void initControls() {

SearchAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] SearchItems = getResources().getStringArray(R.array.DogSearch);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.searchitems,SearchItems);
SearchAuto.setAdapter(adapter);
SearchAuto.setThreshold(0);

search = (Button) findViewById (R.id.handle);
search.setOnClickListener(this);
lv = (ListView) findViewById (R.id.lv);

values = dbHelper.getAllAnimals(ANIMALTYPE);
for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}

// TODO onItemClick
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String strDog = parent.getItemAtPosition(position).toString();

Intent i = new Intent(DogClass.this, Dogs.class);
i.putExtra("dog_name", strDog);
startActivity(i);
finish();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addanimal, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// get prompt_addprayer.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt_addanimal, null);

AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);

final EditText etBreed = (EditText) promptsView
.findViewById(R.id.etBreed);

final EditText etDescription = (EditText) promptsView
.findViewById(R.id.etDescription);

final EditText etDiet = (EditText) promptsView
.findViewById(R.id.etDiet);

final EditText etShelter = (EditText) promptsView
.findViewById(R.id.etShelter);

final EditText etHygiene = (EditText) promptsView
.findViewById(R.id.etHygiene);

final EditText etMedication = (EditText) promptsView
.findViewById(R.id.etMedication);

// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {

String strAnimalType = ANIMALTYPE;
String strBreed = etBreed.getText().toString();
String strDesc = etDescription.getText().toString();
String strDiet = etDiet.getText().toString();
String strShelter = etShelter.getText().toString();
String strHygiene = etHygiene.getText().toString();
String strMedication = etMedication.getText().toString();

dbHelper.addAnimalInfo(strAnimalType, strDesc,
strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();

lvAdapter.notifyDataSetChanged();
}

})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;



}

有什么想法吗?我真的需要让它工作。谢谢

最佳答案

dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();

lvAdapter.notifyDataSetChanged();

这就是你欠缺的地方。你所做的是,你只是将数据插入数据库,你仍然需要从数据库中获取数据并将其与适配器绑定(bind),然后你可以调用 notifyDataSetChanged(); ,你会实现你想要的...

试试这个:

dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
values = dbHelper.getAllAnimals(ANIMALTYPE);
lvAdapter.clear();
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);

lvAdapter.notifyDataSetChanged();

注意:你不应该这样写。您的代码:

for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}

如果您的 getAllAnimals()方法返回一个 arrayList 那么你只需要:

lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);

ArrayAdapter 构造函数的最后一个参数需要 List<Item>被通过。然后调用setAdapter(adapter)ListView .

有关更多说明,请参阅此 SO 链接:notifyDataSetChanged example http://androidadapternotifiydatasetchanged.blogspot.in

关于android - 刷新listview/显示新添加的数据到listview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196586/

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