效果图:
适配器:
public class CrazyAdapter extends BaseAdapter {
private Context mContext;
private List<String> mList;
public CrazyAdapter(Context mContext, List<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
holder = new ViewHolder();
holder.view = (TextView) convertView.findViewById(R.id.tv_city);
holder.box = (CheckBox) convertView.findViewById(R.id.rb_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.view.setText(mList.get(position));
holder.box.setChecked(((ListView) parent).isItemChecked(position));
return convertView;
}
public final class ViewHolder {
private TextView view;
private CheckBox box;
}
}
页面代码:
public class ListViewActivity extends Activity {
private ListView mListView;
private List<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mListView = (ListView)findViewById(R.id.ls_view);
String[] mStrs = {"芥川龙之介", "三岛由纪夫", "川端康成", "村上春树", "东野圭吾", "张爱玲", "金庸", "钱钟书", "老舍", "梁实秋", "亨利米勒", "海明威", "菲兹杰拉德", "凯鲁亚克", "杰克伦敦", "小仲马", "杜拉斯", "福楼拜", "雨果", "巴尔扎克", "莎士比亚", "劳伦斯", "毛姆", "柯南道尔", "笛福"};
mList = Arrays.asList(mStrs);
final CrazyAdapter mAdapter = new CrazyAdapter(this, mList);
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAdapter.notifyDataSetChanged();
}
});
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-04 15:41:27