游标不遍历所有数据库 Table
Cursor doesn't iterate through all Database Table
我写了一个游标,它将在数据库助手 class 中读取 table 中的所有信息,并为其分配一个自定义列表视图,我希望每个元素都对应于相应的字段。但是 Cursor 只打印最后一个元素,并且只打印 3 次迭代。
这是数据库助手Class:
public Cursor getPrograss(){
SQLiteDatabase db = this.getReadableDatabase ();
StatsitcsHelper object = new StatsitcsHelper ();
Cursor cursor = db.rawQuery ("select * from " + TABLE_PROGGRES, null);
cursor.moveToFirst();
if (cursor!= null && cursor.getCount() > 0 && !cursor.isAfterLast()){
cursor.moveToNext();
}
return cursor;
}
这是调用它的片段函数:
public void ShowStatstics() {
DatabaseHelper db = new DatabaseHelper (getActivity ());
Cursor cursor = db.getPrograss ();
String DataA = cursor.getString(cursor.getColumnIndex(COL_P_Date));
String WeightA = cursor.getString(cursor.getColumnIndex(COL_P_Weight));
String PercentA = cursor.getString(cursor.getColumnIndex(COL_P_Percentage));
String[] Prograss = {DataA,WeightA,PercentA};
ListAdapter listAdapter = new StatisticsAdapter (getActivity (),Prograss);
lest.setAdapter (listAdapter);
}
这是适配器:
class StatisticsAdapter extends ArrayAdapter<String>{
public StatisticsAdapter(@NonNull Context context, String[] resource) {
super (context, R.layout.listview_layout ,resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater ListInflate = LayoutInflater.from(getContext ());
StatsitcsHelper statsitcsHelper = new StatsitcsHelper ();
View customView = ListInflate.inflate (R.layout.listview_layout,parent,false);
String first = getItem (0);
TextView date = (TextView) customView.findViewById (R.id.List_Date);
String Second = getItem (1);
TextView Weight = (TextView) customView.findViewById (R.id.List_Weight);
String Thierd = getItem (2);
TextView Percent = (TextView) customView.findViewById (R.id.List_Percentage);
date.setText (first);
Weight.setText (Second);
Percent.setText (Thierd);
return customView;
}
}
您可以像这样创建一个 POJO class,
public class Data {
String date;
String weight;
String percentage;
}
然后在您的方法中,您可以 return ArrayList 并显示在如下列表中,
public ArrayList<Data> getPrograss() {
SQLiteDatabase db = this.getReadableDatabase();
StatsitcsHelper object = new StatsitcsHelper();
Cursor cursor = db.rawQuery ("select * from " + TABLE_PROGGRES, null);
ArrayList<Data> datas = new ArrayList<>();
if(cursor != null) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
Data data = new Data();
data.date = cursor.getString(cursor.getColumnIndex(COL_P_Date));
data.weight = cursor.getString(cursor.getColumnIndex(COL_P_Weight));
data.percentage = cursor.getString(cursor.getColumnIndex(COL_P_Percentage));
datas.add(data);
}
cursor.close();
}
return datas;
}
你的适配器class看起来像这样,
class StatisticsAdapter extends ArrayAdapter<Data> {
public StatisticsAdapter(@NonNull Context context, ArrayList<Data> resource) {
super (context, R.layout.listview_layout ,resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater ListInflate = LayoutInflater.from(getContext ());
Data data = getItem(position);
View customView = ListInflate.inflate (R.layout.listview_layout,parent,false);
String first = data.date;
TextView date = (TextView) customView.findViewById (R.id.List_Date);
String Second = data.weight;
TextView Weight = (TextView) customView.findViewById (R.id.List_Weight);
String Thierd = data.percentage;
TextView Percent = (TextView) customView.findViewById (R.id.List_Percentage);
date.setText (first);
Weight.setText (Second);
Percent.setText (Thierd);
return customView;
}
}
您可以像这样从您的方法创建适配器,
public void showStatstics() {
DatabaseHelper db = new DatabaseHelper (getActivity ());
ArrayList<Data> datas = db.getPrograss ();
ListAdapter listAdapter = new StatisticsAdapter (getActivity (),datas);
lest.setAdapter (listAdapter);
}
我写了一个游标,它将在数据库助手 class 中读取 table 中的所有信息,并为其分配一个自定义列表视图,我希望每个元素都对应于相应的字段。但是 Cursor 只打印最后一个元素,并且只打印 3 次迭代。
这是数据库助手Class:
public Cursor getPrograss(){
SQLiteDatabase db = this.getReadableDatabase ();
StatsitcsHelper object = new StatsitcsHelper ();
Cursor cursor = db.rawQuery ("select * from " + TABLE_PROGGRES, null);
cursor.moveToFirst();
if (cursor!= null && cursor.getCount() > 0 && !cursor.isAfterLast()){
cursor.moveToNext();
}
return cursor;
} 这是调用它的片段函数:
public void ShowStatstics() {
DatabaseHelper db = new DatabaseHelper (getActivity ());
Cursor cursor = db.getPrograss ();
String DataA = cursor.getString(cursor.getColumnIndex(COL_P_Date));
String WeightA = cursor.getString(cursor.getColumnIndex(COL_P_Weight));
String PercentA = cursor.getString(cursor.getColumnIndex(COL_P_Percentage));
String[] Prograss = {DataA,WeightA,PercentA};
ListAdapter listAdapter = new StatisticsAdapter (getActivity (),Prograss);
lest.setAdapter (listAdapter);
}
这是适配器:
class StatisticsAdapter extends ArrayAdapter<String>{
public StatisticsAdapter(@NonNull Context context, String[] resource) {
super (context, R.layout.listview_layout ,resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater ListInflate = LayoutInflater.from(getContext ());
StatsitcsHelper statsitcsHelper = new StatsitcsHelper ();
View customView = ListInflate.inflate (R.layout.listview_layout,parent,false);
String first = getItem (0);
TextView date = (TextView) customView.findViewById (R.id.List_Date);
String Second = getItem (1);
TextView Weight = (TextView) customView.findViewById (R.id.List_Weight);
String Thierd = getItem (2);
TextView Percent = (TextView) customView.findViewById (R.id.List_Percentage);
date.setText (first);
Weight.setText (Second);
Percent.setText (Thierd);
return customView;
}
}
您可以像这样创建一个 POJO class,
public class Data {
String date;
String weight;
String percentage;
}
然后在您的方法中,您可以 return ArrayList 并显示在如下列表中,
public ArrayList<Data> getPrograss() {
SQLiteDatabase db = this.getReadableDatabase();
StatsitcsHelper object = new StatsitcsHelper();
Cursor cursor = db.rawQuery ("select * from " + TABLE_PROGGRES, null);
ArrayList<Data> datas = new ArrayList<>();
if(cursor != null) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
Data data = new Data();
data.date = cursor.getString(cursor.getColumnIndex(COL_P_Date));
data.weight = cursor.getString(cursor.getColumnIndex(COL_P_Weight));
data.percentage = cursor.getString(cursor.getColumnIndex(COL_P_Percentage));
datas.add(data);
}
cursor.close();
}
return datas;
}
你的适配器class看起来像这样,
class StatisticsAdapter extends ArrayAdapter<Data> {
public StatisticsAdapter(@NonNull Context context, ArrayList<Data> resource) {
super (context, R.layout.listview_layout ,resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater ListInflate = LayoutInflater.from(getContext ());
Data data = getItem(position);
View customView = ListInflate.inflate (R.layout.listview_layout,parent,false);
String first = data.date;
TextView date = (TextView) customView.findViewById (R.id.List_Date);
String Second = data.weight;
TextView Weight = (TextView) customView.findViewById (R.id.List_Weight);
String Thierd = data.percentage;
TextView Percent = (TextView) customView.findViewById (R.id.List_Percentage);
date.setText (first);
Weight.setText (Second);
Percent.setText (Thierd);
return customView;
}
}
您可以像这样从您的方法创建适配器,
public void showStatstics() {
DatabaseHelper db = new DatabaseHelper (getActivity ());
ArrayList<Data> datas = db.getPrograss ();
ListAdapter listAdapter = new StatisticsAdapter (getActivity (),datas);
lest.setAdapter (listAdapter);
}