记账本开发6
今天完成账单的显示。(代码参考自CSDN、百度等)
package com.example.myapp0;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import java.util.ArrayList; import java.util.List; public class ZD extends AppCompatActivity
{ private List<Account> accountList=new ArrayList<Account>(); private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zd); initAccount(); AccountAdapter adapter=new AccountAdapter(ZD.this,R.layout.item,accountList); listView=findViewById(R.id.zd); listView.setAdapter(adapter); } private void initAccount()
{ MyDatabaseHelper databaseHelper=new MyDatabaseHelper(this); SQLiteDatabase db=databaseHelper.getWritableDatabase(); Cursor cursor=db.query("AcountBook",null,null,null,null,null); while(cursor.moveToNext())
{ String je=cursor.getString(cursor.getColumnIndex("je")); String name=cursor.getString(cursor.getColumnIndex("name")); String bz=cursor.getString(cursor.getColumnIndex("bz")); String time=cursor.getString(cursor.getColumnIndex("time")); Account account=new Account(je,name,bz,time); accountList.add(account); } } }
package com.example.myapp0;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.content.Context;
import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import java.util.List;
public class AccountAdapter extends ArrayAdapter
{ private final int resourceId; public AccountAdapter(Context context,int textViewResourceId,List<Account> objects){ super(context,textViewResourceId,objects); resourceId=textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent)
{ Account account = (Account) getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceId, null); TextView je,name,bz,time; je=view.findViewById(R.id.je); name=view.findViewById(R.id.name); bz=view.findViewById(R.id.bz); time=view.findViewById(R.id.time); je.setText(account.getJe()); name.setText(account.getName()); bz.setText(account.getBz()); time.setText(account.getTime()); return view; } }