今天解决昨天的问题,按照网上的例子没有实现,发现是变量的名称没有修改正确,因此,接下来要做的就是从数据库读取出内容然后放在一个HashMap中,然后根据固定的代码格式取值显示就行,代码如下:
public class XIANSHI extends AppCompatActivity {
private DataBase databaseHelper; //用于创建帮助器对象
private SQLiteDatabase db; //用于创建数据库对象
private static final String name = "JZ.db"; //数据库名称
private static final int version = 1; //数据库版本
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x_i_a_n_s_h_i);
ListView listView = (ListView) this.findViewById(R.id.listView);
List<Bean> list = new ArrayList<Bean>();
databaseHelper = new DataBase(this, name, null, version);
DataBase moh = new DataBase(this, "JZ.db", null, 1);
SQLiteDatabase db = moh.getReadableDatabase(); // 以只读的方式打开数据库
String sql = "select * from ben ;";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
String date = cursor.getString(cursor.getColumnIndex("date"));
String leixing = cursor.getString(cursor.getColumnIndex("leixing"));
String num = cursor.getString(cursor.getColumnIndex("num"));
String yuanyin = cursor.getString(cursor.getColumnIndex("yuanyin"));
Bean JL = new Bean(date, leixing, num, yuanyin);
list.add(JL); // 添加到数组
}
List<HashMap<String, Object>> Item = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
HashMap<String, Object> item = new HashMap<>();
item.put("RQ", list.get(i).getDate());
item.put("LX", list.get(i).getLeixing());
item.put("SUM", list.get(i).getNum());
item.put("YY", list.get(i).getYuanyin());
Item.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(XIANSHI.this, Item, R.layout.item, new String[]{"RQ", "LX", "SUM", "YY"}, new int[]{R.id.RQ, R.id.LX, R.id.SUM, R.id.YY});
listView.setAdapter(adapter);
}
}