Android 更新列表的数据时自动更新Arraylist
插入时,我的listview会自动刷新,但不会在更新listview中的项目时更新。它只在数据库上更新。当我关闭应用程序并再次打开时,或者从以前的活动返回时,我可以看到listview被更新 我发现一些与我的问题有关的讨论。比如:。我发现,创建一个新方法来填充Listview,并在活动的onResume方法中调用它。 用这个解决了这个问题。但我不知道如何制作像上面提到的新方法。有谁能帮我解释一下吗 活动类中的我的代码:Android 更新列表的数据时自动更新Arraylist,android,listview,android-listview,android-arrayadapter,Android,Listview,Android Listview,Android Arrayadapter,插入时,我的listview会自动刷新,但不会在更新listview中的项目时更新。它只在数据库上更新。当我关闭应用程序并再次打开时,或者从以前的活动返回时,我可以看到listview被更新 我发现一些与我的问题有关的讨论。比如:。我发现,创建一个新方法来填充Listview,并在活动的onResume方法中调用它。 用这个解决了这个问题。但我不知道如何制作像上面提到的新方法。有谁能帮我解释一下吗 活动类中的我的代码: personNamesListView = (ListView) findV
personNamesListView = (ListView) findViewById(R.id.traineeslist);
traineeListAdapter = new ArrayAdapter<Trainee>(this,
android.R.layout.simple_list_item_1,
currentTraining.getTraineeArrayList());
personNamesListView.setAdapter(traineeListAdapter);
protected void onResume() {
super.onResume();
}
无论何时更新表示
列表视图中的项目的ArrayList
,都应该调用traineeListAdapter.notifyDataSetChanged()
,有一个类似的问题可以给您一些帮助
虽然我已经完成了一些类似的使用
yourlistview.invalidateViews()
更改要在listview中显示的数据后
notifyDataSetChanged()不起作用时
编辑:
在我想显示的数据中完成所有操作后,我只需设置适配器,并通过调用invalidateViews()尝试刷新我的listview
selectedStrings=newarraylist(typeFilterStrings);
adapter.setArrayResultados(SelectedString);
listTypeFilter.invalidateViews();
在我的情况下,不必再次设置适配器。如下使用:
创建自定义适配器的实例,以便您可以在任何地方使用它
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
((BaseAdapter) listViewScore.getAdapter()).notifyDataSetChanged();
}
}
公共类记分表扩展了SherlockFragmentActivity{
私有ListView listViewScore;
专用适配器;
静态列表listScore=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.score_列表);
ctx=这个;
listScore=dbh.getAllScores();
listViewScore=(ListView)findViewById(R.id.score\u列表);
适配器=新的ScoreListAdapter(ctx,R.layout.score\u行\u项,listScore);
listViewScore.setAdapter(适配器);
((BaseAdapter)listViewScore.getAdapter()).notifyDataSetChanged();
}
}
顺便说一下,如果已经加载了listScore数组,则不需要使用
adapter.notifyDatasetChanged() 在插入和删除列表中的项目后使用personNamesListView.notifyDataSetchange()或在键入personNamesListView时更新listAnd的项目。没有类似NOTIFYDATASETCHENGES()的建议。很抱歉,它的traineeListAdapter.NOTIFYDATASETCHENGED()仍然没有显示任何类似的提示。它只是显示;notify()和notifyAll();我是白痴。我制作了如下变量:私有ListAdapter TraineListAdapter;并在onCreate(){traineeListAdapter=new ArrayAdapter}上声明。现在,我已更改并能够获得TraineListAdapter.NotifyDataSetChange。但它仍然不起作用(我应该在同一个活动中执行此操作吗?不一定来自同一个活动,但您必须在UI线程上调用它。很抱歉,我的listview中填充了项目数组,而不是每个视图中都有单个项目。我没有选择项目的界面,或者项目无法单击。)(我有使用自动完成文本视图的搜索框。当用户发现自己时,他单击自动完成下拉字符串,所有数据从数据库重定向到表单。在那里他可以编辑他的数据并更新到数据库。但是你每次使用搜索框时都查询数据库吗?我很难理解应用程序的工作原理抱歉。是的,我每次查询时都会使用if-else语句进行搜索。如果用户发现自己->它会返回person\u id,如果他没有找到,我会像(-1)一样传递id。您知道在数据库中没有带-1的id。然后它会将空字符串传递给窗体以添加新用户。因此,如果您在数据库中编辑并保存,您应该从数据库中检索保存的数据。您确定已正确保存数据库吗?请创建一些日志,而不是用您的结果填充listview。如果我真的理解了,我不能使用此命令dapter.notifyDatasetChanged();在我的情况下,因为我的listview是直接从列表数组填充的。请参阅我编辑过的问题。我真的可以在我的情况下使用此方法吗?brother您可以在适配器上使用此方法在PersonNameListView.setAdapter(TraineListAdapter)之后;使用TraineListAdapter.notifyDatasetChanged();@user3180746不管您的数据是从任何地方填充的对不起,朋友。如果我这样使用((BaseAdapter)traineeListAdapter)。notifyDataSetChanged();我可以使用此方法,但它仍然不起作用。
public void storeToDB() {
DatabaseHelper db = DatabaseHelper.getInstance();
db.getWritableDatabase();
if (this.id == -1) {
// Person not yet stored into Db => SQL INSERT
// ContentValues class is used to store a set of values that the
// ContentResolver can process.
ContentValues contentValues = new ContentValues();
// Get values from the Person class and passing them to the
// ContentValues class
contentValues.put(DatabaseHelper.PERSON_FIRSTNAME, this
.getFirstname().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_LASTNAME, this
.getLastname().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_JOBTITLE, this
.getJobTitle().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
contentValues.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
.trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_DEPARTMENT, this
.getDepartment().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_BADGE, this
.getBadgeNumber().trim().toUpperCase());
// here we insert the data we have put in values
this.setID((int) db.insert(DatabaseHelper.TABLE_PERSON,
contentValues));
} else {
// Person already existing into Db => SQL UPDATE
ContentValues updateTrainee = new ContentValues();
updateTrainee.put(DatabaseHelper.PERSON_FIRSTNAME, this
.getFirstname().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_LASTNAME, this
.getLastname().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_JOBTITLE, this
.getJobTitle().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
updateTrainee.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
.trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_DEPARTMENT, this
.getDepartment().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_BADGE, this
.getBadgeNumber().trim().toUpperCase());
db.update(DatabaseHelper.TABLE_PERSON, updateTrainee,
DatabaseHelper.PERSON_ID+"= ?", new String[]{Integer.toString(this.getId())});
System.out.println("Data updated");
}
}
selectedStrings = new ArrayList<String>(typeFilterStrings);
adapter.setArrayResultados(selectedStrings);
listTypeFilter.invalidateViews();
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
((BaseAdapter) listViewScore.getAdapter()).notifyDataSetChanged();
}
}