安卓开发六——账本的条目视图的适配
我们的一条数据项目包括,收入(指出)、说明、日期、金额四项,所以我们要自定义一个适配器
这里适配器的一个列表的各个单位的类型是一个打包好的类的类型。这个类也是自己创建的
package com.example.myapplication; public class costList { private String _id; private String se; private String Title; private String Date; private String Money; public String getMoney() { return Money; } public void setMoney(String money) { Money = money; } public String getDate() { return Date; } public void setDate(String date) { Date = date; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getSe() { return se; } public void setSe(String se) { this.se = se; } }
package com.example.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class ListAdapter extends BaseAdapter { List<costList> mList; public ListAdapter(List<costList>list) { mList=list; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=mLayoutInflater.inflate(R.layout.list_item,null); //取出数据赋值 costList item=mList.get(position); TextView tv_spin=view.findViewById(R.id.tv_spin); TextView tv_title=view.findViewById(R.id.tv_title); TextView tv_date=view.findViewById(R.id.tv_date); TextView tv_money=view.findViewById(R.id.tv_money); //绑定 tv_spin.setText(mList.get(position).getSe()); tv_title.setText(mList.get(position).getTitle()); tv_date.setText(mList.get(position).getDate()); tv_money.setText(mList.get(position).getMoney()); return view; } private List<costList>getmList; private LayoutInflater mLayoutInflater; public ListAdapter(Context context, List<costList>list) { mList=list; //通过外部传来的Context初始化LayoutInflater对象 mLayoutInflater=LayoutInflater.from(context); } }
package com.example.myapplication; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private DBHelper helper; private ListView listView; private List<costList>list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } @SuppressLint("Range") private void initData() { list=new ArrayList<>(); SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.query("account2",null,null,null,null, null,null); while (cursor.moveToNext()){ costList clist=new costList();//构造实例 clist.setSe(cursor.getString(cursor.getColumnIndex("Se"))); clist.set_id(cursor.getString(cursor.getColumnIndex("_id"))); clist.setTitle(cursor.getString(cursor.getColumnIndex("Title"))); clist.setDate(cursor.getString(cursor.getColumnIndex("Date"))); clist.setMoney(cursor.getString(cursor.getColumnIndex("Money"))); list.add(clist); } //绑定适配器 listView.setAdapter(new ListAdapter(this,list)); db.close(); } private void initView() { helper=new DBHelper(MainActivity.this); listView = findViewById(R.id.list_view); } public void addAccount(View view){//跳转 Intent intent=new Intent(MainActivity.this,new_cost.class); //noinspection deprecation startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)//返回会刷新页面 { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==1) { iniView(); } } }
最后再在我们的页面操作了Java程序中按照我们打包好的列表进行适配得到我们需要的页面。