在ListView Android中显示ArrayList
我很难将保存在SqlLite数据库中的按钮单击存储的数据显示到ListView中。当我单击按钮将数据添加到数据库时,它不会在按钮下方显示我想要的数据 我的代码:在ListView Android中显示ArrayList,android,sqlite,listview,arraylist,android-sqlite,Android,Sqlite,Listview,Arraylist,Android Sqlite,我很难将保存在SqlLite数据库中的按钮单击存储的数据显示到ListView中。当我单击按钮将数据添加到数据库时,它不会在按钮下方显示我想要的数据 我的代码: boolean hasMoreData = cursor.moveToFirst(); while (hasMoreData) { // get the value out of each column long key = cursor.getLong(cursor.getColumnIndex
boolean hasMoreData = cursor.moveToFirst();
while (hasMoreData) {
// get the value out of each column
long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
//For now, just print out the record to the log.
ArrayList<String> myList = new ArrayList<>();
myList.add( " student id: "+ studentID);
myList.add(" student grade : " +studentGrade);
ArrayAdapter<String> myAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,myList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myAdapter);
System.out.println("RECORD KEY: " + key + " student id: " + studentID + " student grade : " + studentGrade);
//move on to the next row!
hasMoreData = cursor.moveToNext();
}
boolean hasMoreData=cursor.moveToFirst();
while(hasMoreData){
//从每列中获取值
long key=cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry.\u ID));
String studentID=cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
String studentGrade=cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
//现在,只需将记录打印到日志中即可。
ArrayList myList=新的ArrayList();
添加(“学生id:+studentID”);
myList.add(“学生成绩:”+studentGrade);
阵列适配器=
新的ArrayAdapter(这是android.R.layout.simple\u expandable\u list\u item\u 1,myList);
ListView ListView=(ListView)findViewById(R.id.ListView);
setAdapter(myAdapter);
System.out.println(“记录键:+KEY+”学生id:+studentID+”学生成绩:+studentGrade”);
//转到下一排!
hasMoreData=cursor.moveToNext();
}
这在emulator中是什么样子的:
我想要的是:
每次调用
ArrayList myList=new ArrayList()
和ArrayAdapter myAdapter=new ArrayAdapter()代码>您正在重置列表
删除ArrayList myList=new ArrayList()代码>
搬家
ArrayAdapter myAdapter=
新的ArrayAdapter(这是android.R.layout.simple\u expandable\u list\u item\u 1);
ListView ListView=(ListView)findViewById(R.id.ListView);
setAdapter(myAdapter)代码>
在您的循环上方,您将修复此问题
在循环内调用“myAdapter.add”(“任意”);”然后myAdapter.notifyDataSetChanged()
更新ui将阵列适配器和列表视图放置在循环外部
ArrayList<String> myList = new ArrayList<>();
boolean hasMoreData = cursor.moveToFirst();
while (hasMoreData) {
long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
myList.add( " student id: "+ studentID);
myList.add(" student grade : " +studentGrade);
hasMoreData = cursor.moveToNext();
}
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, myList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myAdapter);
ArrayList myList=new ArrayList();
boolean hasMoreData=cursor.moveToFirst();
while(hasMoreData){
long key=cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry.\u ID));
String studentID=cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
String studentGrade=cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
添加(“学生id:+studentID”);
myList.add(“学生成绩:”+studentGrade);
hasMoreData=cursor.moveToNext();
}
ArrayAdapter myAdapter=新的ArrayAdapter(这是android.R.layout.simple\u expandable\u list\u item\u 1,myList);
ListView ListView=(ListView)findViewById(R.id.ListView);
setAdapter(myAdapter);
在我的循环之外,具体在哪里?在while的条目上方或下方?while loopok的正上方,用于获取所有数据。现在我的问题是,如何显示当前通过单击按钮添加的数据?更新了我的答案。但EYN的答案要干净得多;)将ArrayList放入myList;上面的while循环给出了myList的新ArrayAdapter调用中的错误没有初始化NVM我通过调用newt修复了它。这只显示了添加的最后一级。出于某种原因,似乎将ArrayList放在循环的顶部。