帮助类:
JavaBean:
package com.example.f405.sqlitetest.bean;
/**
* Created by F405 on 2018/4/24.
*/
public class History {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
int carNum;
int rechargeAmount;
String userName;
String time;
public int getCarNum() {
return carNum;
}
public void setCarNum(int carNum) {
this.carNum = carNum;
}
public int getRechargeAmount() {
return rechargeAmount;
}
public void setRechargeAmount(int rechargeAmount) {
this.rechargeAmount = rechargeAmount;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
Adapter:
package com.example.f405.sqlitetest.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.f405.sqlitetest.R;
import com.example.f405.sqlitetest.bean.History;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by F405 on 2018/4/24.
*/
public class MyHistoryAdapter extends ArrayAdapter{
int resource;
View view ;
ViewHolder viewHolder;
private static final String TAG = "MyHistoryAdapter";
public MyHistoryAdapter(@NonNull Context context, int resource, @NonNull List objects) {
super(context, resource, objects);
this.resource = resource;
}
// 初始化控件
class ViewHolder{
TextView tv_num;
TextView tv_carNum;
TextView tv_rechargeAmount;
TextView tv_userName;
TextView tv_time;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
History history = (History) getItem(position);
if ( convertView == null){
// 初始化
view = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false);
viewHolder = new ViewHolder();
viewHolder.tv_num = view.findViewById(R.id.tv_num);
viewHolder.tv_carNum = view.findViewById(R.id.tv_carNum);
viewHolder.tv_rechargeAmount = view.findViewById(R.id.tv_rechargeAmount);
viewHolder.tv_userName = view.findViewById(R.id.tv_userName);
viewHolder.tv_time = view.findViewById(R.id.tv_time);
view.setTag(viewHolder);
}else {
// 复用
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
// 设置数据
Log.d(TAG, "getView: ----"+history.getId()+history.getUserName());
viewHolder.tv_num.setText(history.getId()+"");
viewHolder.tv_carNum.setText(history.getCarNum()+"");
viewHolder.tv_rechargeAmount.setText(history.getRechargeAmount()+"");
viewHolder.tv_userName.setText(history.getUserName());
viewHolder.tv_time.setText(history.getTime());
return view;
}
}
xml文件:
子项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
-
<TextView
android:id="@+id/tv_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name" />
<TextView
android:id="@+id/tv_carNum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name" />
<TextView
android:id="@+id/tv_rechargeAmount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name" />
<TextView
android:id="@+id/tv_userName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name" />
<TextView
android:id="@+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_name" />
</LinearLayout>
main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.f405.sqlitetest.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_insert"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="插入数据"/>
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="查询数据"/>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="17dp"
android:layout_below="@id/ll"></ListView>
</RelativeLayout>
java代码:
package com.example.f405.sqlitetest;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.provider.FontsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.f405.sqlitetest.adapter.MyHistoryAdapter;
import com.example.f405.sqlitetest.bean.History;
import com.example.f405.sqlitetest.sql.MyOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private SQLiteDatabase db;
private static final String TAG = "MainActivity";
private ListView lv;
private List history_list = new ArrayList();
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 创建表
MyOpenHelper myOpenHelper = new MyOpenHelper(this,"history.db",1);
db = myOpenHelper.getReadableDatabase();
}
private void initView() {
Button btn_insert = findViewById(R.id.btn_insert);
Button btn_query = findViewById(R.id.btn_query);
lv = findViewById(R.id.lv);
btn_insert.setOnClickListener(this);
btn_query.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_insert:
ContentValues values = new ContentValues();
values.put("carNum",2);
values.put("rechargeAmount",6);
values.put("userName","admin");
values.put("time",System.currentTimeMillis());
db.insert("history",null,values);
break;
case R.id.btn_query:
getInfoFromSql();
initData();
break;
}
}
private void getInfoFromSql() {
Cursor cursor = db.query("history", null, null, null, null, null, null);
if (cursor != null){
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
int carNum = cursor.getInt(cursor.getColumnIndex("carNum"));
int rechargeAmount = cursor.getInt(cursor.getColumnIndex("rechargeAmount"));
String userName = cursor.getString(cursor.getColumnIndex("userName"));
String time = cursor.getString(cursor.getColumnIndex("time"));
// 使用集合存储,ListView显示
Log.d(TAG, "onClick: ----"+carNum+rechargeAmount+userName+time);
//加入集合
History history = new History();
history.setId(id);
history.setCarNum(carNum);
history.setRechargeAmount(rechargeAmount);
history.setUserName(userName);
history.setTime(time);
history_list.add(history);
}
}
cursor.close();
}
private void initData() {
// ListView的数据
MyHistoryAdapter adapter = new MyHistoryAdapter(this,R.layout.item,history_list);
lv.setAdapter(adapter);
}
}