为listview写一个adapter,可以实现点击按钮查看每个列的数据
时间: 2024-05-05 17:16:30 浏览: 93
使用adapter实现listview的多种功能
首先,需要定义一个数据模型类,用于存储每个列表项的数据:
```
public class ListItem {
private String column1;
private String column2;
private String column3;
public ListItem(String column1, String column2, String column3) {
this.column1 = column1;
this.column2 = column2;
this.column3 = column3;
}
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public String getColumn2() {
return column2;
}
public void setColumn2(String column2) {
this.column2 = column2;
}
public String getColumn3() {
return column3;
}
public void setColumn3(String column3) {
this.column3 = column3;
}
}
```
接下来,需要定义一个适配器类,用于将数据模型类中的数据绑定到列表项布局中:
```
public class MyListAdapter extends BaseAdapter {
private Context mContext;
private List<ListItem> mList;
public MyListAdapter(Context context, List<ListItem> list) {
mContext = context;
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) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_layout, null);
holder = new ViewHolder();
holder.column1TextView = convertView.findViewById(R.id.column1_text_view);
holder.column2TextView = convertView.findViewById(R.id.column2_text_view);
holder.column3TextView = convertView.findViewById(R.id.column3_text_view);
holder.showDataButton = convertView.findViewById(R.id.show_data_button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ListItem item = mList.get(position);
holder.column1TextView.setText(item.getColumn1());
holder.column2TextView.setText(item.getColumn2());
holder.column3TextView.setText(item.getColumn3());
holder.showDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "Column 1: " + item.getColumn1() + "\nColumn 2: " + item.getColumn2() + "\nColumn 3: " + item.getColumn3(), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
private static class ViewHolder {
TextView column1TextView;
TextView column2TextView;
TextView column3TextView;
Button showDataButton;
}
}
```
在适配器的getView()方法中,通过ViewHolder来缓存列表项布局中的控件,避免频繁地调用findViewById()方法,提高性能。
在按钮的点击事件中,通过Toast来显示该列表项中每一列的数据。
最后,在Activity中使用这个适配器类来显示列表:
```
public class MyActivity extends AppCompatActivity {
private ListView mListView;
private MyListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mListView = findViewById(R.id.list_view);
List<ListItem> list = new ArrayList<>();
list.add(new ListItem("A1", "B1", "C1"));
list.add(new ListItem("A2", "B2", "C2"));
list.add(new ListItem("A3", "B3", "C3"));
list.add(new ListItem("A4", "B4", "C4"));
list.add(new ListItem("A5", "B5", "C5"));
mAdapter = new MyListAdapter(this, list);
mListView.setAdapter(mAdapter);
}
}
```
这样就完成了列表的显示和按钮点击事件的处理。
阅读全文