首先是创建一个基础的item组件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/item_container"android:orientation="horizontal"><TextViewandroid:id="@+id/id"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/leix"android:layout_weight="2"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/amo"android:layout_weight="2"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>
在需要插入的页面写入ListView:
<TextViewandroid:id="@+id/amount"android:layout_width="150dp"android:layout_height="50dp"android:layout_toLeftOf="@id/main_btn_edit"android:layout_alignBottom="@id/main_btn_edit"android:layout_marginRight="60dp"/>
之后是实现代码的My Adapter:实现item传值
package com.syh.jizhangben;import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;import com.syh.jizhangben.pojo.JZB; import com.syh.jizhangben.util.DBHelper;import java.util.ArrayList;public class MyAdapter extends BaseAdapter {private DBHelper dbHelper; // 确保DBHelper有适当的构造函数和删除方法private Context context;private ArrayList<JZB> dataList;public MyAdapter(Context context, ArrayList<JZB> dataList) {this.context = context;this.dataList = dataList;this.dbHelper = new DBHelper(context); // 初始化dbHelper }@Overridepublic int getCount() {return dataList.size();}@Overridepublic Object getItem(int position) {return dataList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if(convertView == null) {// inflate the layoutLayoutInflater inflater = LayoutInflater.from(context);convertView = inflater.inflate(R.layout.find, parent, false);viewHolder = new ViewHolder();// viewHolder.itemView = convertView.findViewById(R.id.item_container);viewHolder.textViewId = convertView.findViewById(R.id.id);viewHolder.textViewType = convertView.findViewById(R.id.leix);viewHolder.textViewAmount = convertView.findViewById(R.id.amo);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}// set the data to textviewsJZB currentItem = dataList.get(position);viewHolder.textViewId.setText(String.valueOf(currentItem.getId()));viewHolder.textViewType.setText(currentItem.getInorout());viewHolder.textViewAmount.setText(String.valueOf(currentItem.getAmount()));return convertView;}static class ViewHolder {public View itemView;TextView textViewId;TextView textViewType;TextView textViewAmount;} }
在需要展示item列表的地方书写以下代码:
ArrayList<JZB> filteredData;
filteredData = dbHelper.getAllDATA();
MyAdapter adapter = new MyAdapter(getApplicationContext(), filteredData);ListView listView = findViewById(R.id.list_view_data);listView.setAdapter(adapter);
实现数据的填入创建。