记账本APP10
今天将数据库中记录的数据全部导入到主页面中,进行一个收入及支出的记录的显示
代码如下:
适配器AccountAdapter.java
1 import android.content.Context; 2 import android.view.LayoutInflater; 3 import android.view.View; 4 import android.view.ViewGroup; 5 import android.widget.ArrayAdapter; 6 import android.widget.TextView; 7 8 import java.util.List; 9 10 public class AccountAdapter extends ArrayAdapter { 11 private final int resourceId; 12 public AccountAdapter(Context context,int textViewResourceId,List<Account> objects){ 13 super(context,textViewResourceId,objects); 14 resourceId=textViewResourceId; 15 } 16 @Override 17 public View getView(int position, View convertView, ViewGroup parent){ 18 Account account = (Account) getItem(position); // 获取当前项的Account/实例 19 View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象 20 TextView typetv,nametv,moneytv,timetv,notetv; 21 typetv=view.findViewById(R.id.item_main_lv_type); 22 nametv=view.findViewById(R.id.item_main_lv_name); 23 moneytv=view.findViewById(R.id.item_main_lv_money); 24 timetv=view.findViewById(R.id.item_main_lv_time); 25 notetv=view.findViewById(R.id.item_main_lv_note); 26 typetv.setText(account.getType()); 27 nametv.setText(account.getName()); 28 moneytv.setText(account.getMoney()); 29 timetv.setText(account.getTime()); 30 notetv.setText(account.getNote()); 31 return view; 32 } 33 }
MainActivity.java
1 import androidx.appcompat.app.AppCompatActivity; 2 3 import android.content.Intent; 4 import android.database.Cursor; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.ImageButton; 9 import android.widget.ListView; 10 import android.widget.Toast; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 public class MainActivity extends AppCompatActivity {//ListView 16 private List<Account> accountList=new ArrayList<Account>(); 17 private ListView listView; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 //ListView 25 initAccount();//初始化 26 AccountAdapter adapter=new AccountAdapter(MainActivity.this,R.layout.item,accountList); 27 listView=findViewById(R.id.main_lv);//绑定listview 28 listView.setAdapter(adapter);//设置adapter 29 30 } 31 private void initAccount(){ 32 //从数据库里面把数据取出来 33 MyDatabaseHelper databaseHelper=new MyDatabaseHelper(this); 34 SQLiteDatabase db=databaseHelper.getWritableDatabase(); 35 Cursor cursor=db.query("AcountBook",null,null,null,null,null,null); 36 while(cursor.moveToNext()){ 37 String type=cursor.getString(cursor.getColumnIndex("type")); 38 String name=cursor.getString(cursor.getColumnIndex("name")); 39 String money=cursor.getString(cursor.getColumnIndex("money")); 40 String time=cursor.getString(cursor.getColumnIndex("time")); 41 String note=cursor.getString(cursor.getColumnIndex("note")); 42 Account account=new Account(type,name,note,time,money); 43 accountList.add(account); 44 } 45 } 46 }