一个listView的例子代码

时间:2022-06-28 19:47:12

MainActivity

package com.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015/8/25.
*/

public class ListFruitActivity extends Activity {
private ListView mListView;
private List<Fruit> mFruits;
private FruitAdapter mAdapter;
private LayoutInflater mInflater;
private Button mBtnCheckedFan;
private View mHeaderView;
private Button mBtnCheckedAll;
private View mFootView;
private View mViewTou;
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);//放相当于书架作用的ListView布局
mListView = (ListView) findViewById(R.id.listview);
mInflater = getLayoutInflater();
initData();
mBtnCheckedAll= (Button) findViewById(R.id.button_all_checked);
mBtnCheckedFan= (Button) findViewById(R.id.button_fan);
mBtnCheckedAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.checkedAll();
}
});
mBtnCheckedFan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.checkedFan();
}
});
mAdapter = new FruitAdapter(mInflater, mFruits);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAdapter.itemSelect(position);
}
});
}
private void initData() {
mFruits = new ArrayList<>();
for (int i = 0; i <10; i++) {
Fruit apple = new Fruit("苹果", R.mipmap.apple);
Fruit caomei = new Fruit("草莓", R.mipmap.caomei);
Fruit lizhi = new Fruit("荔枝", R.mipmap.lizhi);
Fruit cherry = new Fruit("樱桃", R.mipmap.cherry);
mFruits.add(apple);
mFruits.add(caomei);
mFruits.add(lizhi);
mFruits.add(cherry);
}

}
}

adapter

package com.example.administrator.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
* Created by Administrator on 2015/8/25.
*/

public class FruitAdapter extends BaseAdapter {
private List<Fruit> mFruits;
private LayoutInflater mInflater;
private boolean [] mManagerAllCheckBox;
public FruitAdapter(LayoutInflater inflater, List<Fruit> date) {
//将inflater
mInflater = inflater;
mFruits = date;
mManagerAllCheckBox=new boolean[mFruits.size()];
}
public void checkedFan(){
for (int i=0;i<mManagerAllCheckBox.length;i++){
mManagerAllCheckBox[i]=!mManagerAllCheckBox[i];

}
notifyDataSetChanged();
}
public void checkedAll(){
for (int i=0;i<mManagerAllCheckBox.length;i++){
mManagerAllCheckBox[i]=true;
}
notifyDataSetChanged();
}
public void itemSelect(int position){
mManagerAllCheckBox[position]=!mManagerAllCheckBox[position];//点空白地方也能选中checkBox
notifyDataSetChanged();//刷新
}
@Override
public int getCount() {
//得到listaview将要显示的数据条数
return mFruits.size();
}

@Override
public Object getItem(int position) {
//返回索引
return position;
}

@Override
public long getItemId(int position) {
//返回索引
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Fruit fruit = mFruits.get(position);
ViewHolder vh = null;//局部变量赋初值
if (convertView == null) {//convertView是滑出屏幕的view
vh = new ViewHolder();
convertView = mInflater.inflate(R.layout.activity_item_fruit, null);//这里放相当于书的布局
vh.checbox_xuanze = (CheckBox) convertView.findViewById(R.id.checbox_xuanze);
vh.imageView = (ImageView) convertView.findViewById(R.id.image);
vh.textView_name = (TextView) convertView.findViewById(R.id.textView_name);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.textView_name.setText(fruit.getName());
vh.imageView.setImageResource(fruit.getImg());
vh.checbox_xuanze.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mManagerAllCheckBox[position]=isChecked;
notifyDataSetChanged();
}
});
vh.checbox_xuanze.setChecked(mManagerAllCheckBox[position]);
return convertView;
}
class ViewHolder {
TextView textView_name;
CheckBox checbox_xuanze;
ImageView imageView;
}
}

item信息类

public class Fruit {
private String name;
private int img;
public Fruit(String name,int img){
this.name=name;
this.img=img;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getImg() {
return img;
}

public void setImg(int img) {
this.img = img;
}
}