自己定义了适配器,listview中实现checkbox由我传人参数控制有几个

舒国立 发布于 2012/03/01 10:45
阅读 314
收藏 0
适配器代码:
package com.wsqh.guozi;
import java.util.zip.Inflater;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
 Activity mContext;
 int id;
 String[] strings={"111111","222222","333333"};
 public MyAdapter(Activity mContext, int id) {
  super();
  this.mContext = mContext;
  this.id = id;
 }
 static class ViewHolder {
  TextView content;
  //CheckBox checkbox;
  //ImageView imageView;
  LinearLayout linLayout;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder=null;
  if (convertView == null) {// 如果现在没有视图
   // 初始化布局文件和布局的组建结构
   holder = new ViewHolder();
   LayoutInflater inflater = mContext.getLayoutInflater();
   convertView = inflater.inflate(R.layout.lsit, null);
   holder.linLayout=(LinearLayout) convertView.findViewById(R.id.linearLayout1);
   // holder.linLayout.setTag("aa");
   holder.content=(TextView) convertView.findViewById(R.id.textView1);
   // 将布局指向一个模式
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
    // LinearLayout layout=(LinearLayout) row.findViewWithTag("aa");
     holder.content.setText(strings[position]);
     CheckBox c;
     for (int i = 0; i < id; i++) {
      System.out.println("第"+i+":::::");
      c=new CheckBox(holder.linLayout.getContext());
      holder.linLayout.addView(c,i);
  }
  
  return convertView;
 }
 @Override
 public int getCount() {
  return strings.length;
 }
 @Override
 public Object getItem(int arg0) {
  // TODO Auto-generated method stub
  return arg0;
 }
 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }
 
} 

以下是问题补充:

@舒国立:我现在就是我通过传人变量id控制生成几个checkbox,还有怎么控制checkbox之间的距离? (2012/03/01 10:47)
加载中
0
迷途d书童
迷途d书童
控制checkbox之间的距离,用checkbox.setLayoutParams(params);LayoutParams根据你实际需要设置。
返回顶部
顶部