通过继承BaseAdapter写一个子类,可以创建自定义列表视图:
public class MyListAdapter extends BaseAdapter { private LayoutInflater mInflater;//声明一个LayoutInflater类变量 private Context mContext;//声明一个Context类变量 private List<Map<String,String>> mitem;//声明一个元素为<Map<String,String>>类型的List对象,存储testitem的值 private Map<String,Integer> selectedtestItem = new HashMap<String,Integer>();//声明一个Map对象,用HashMap来实现 private List<Map<String, String>> mData; // 声明一个元素为<Map<String,String>>类型的List对象,存储的editTex值 private Map<String, String> editorValue = new HashMap<String, String>();//声明一个Map对象,用HashMap来实现 private List<Map<String,String>> mParameter;//用于保存parameter_edit参数值 //private static HashMap<Integer, Boolean> isSelected;//声明一个HashMap<Integer,Boolean>类型的变量 private String[] testItems; private String promptForSpinner[] = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"};
public MyListAdapter(Context context,String[] testItems) {//构造函数 // TODO Auto-generated constructor stub mInflater = LayoutInflater.from(context);//获得LayoutInflater this.testItems = testItems; puttestitemName(); } private void puttestitemName() { for (String str : promptForSpinner) { selectedtestItem.put(str,0); } /*for(int i=0;i<20;i++){ selectedtestItem.put("list_item_Name", 0);//allValues保存以项目名称为键值、0为数值的一系列成对的数据 }*/ } public void setData( List<Map<String, String>> data) {//初始化Lisr类型的对象 mData = data; mParameter=data; init(); } private void init() {//实现初始化List类型的对象的方法 editorValue.clear(); /*isSelected = new HashMap<Integer, Boolean>(); if (mData != null) { for (int i = 0; i < mData.size(); i++) { isSelected.put(i, false); } }*/ } public Map<String,Integer> getSelectedtestItem() { return selectedtestItem; }
public List<Map<String,String>> getmData() { return mData; } public List<Map<String,String>> getmParameter() { return mParameter; } @Override public int getCount() {//获得List类型对象的长度; // TODO Auto-generated method stub if (mData != null) { return mData.size(); } return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } private Integer index = -1;//声明并初始化一个整型变量index //Adapter是ListView界面与数据之间的桥梁。当列表里的每一项要显示时,都会调用Adapter的getView方法返回一个View。 @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Log.d("zhang", "position = " + position);//Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择. final ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.listitem, null);//实例化R.layout.list为converView的View对象 holder = new ViewHolder();//ViewHolder就是一个静态类,使用它缓存了数据的视图(View),加快UI的响应速度。 //holder.checkBox = (CheckBox) convertView //.findViewById(R.id.checkbox);
holder.testitemSpinner = (Spinner)convertView.findViewById(R.id.testitems_select); //if(holder.testitemSpinner.getCount()==0) //if(holder.testitemSpinner.getPrompt()==null) //{ ArrayAdapter<String> a = new ArrayAdapter<String>( convertView.getContext(),android.R.layout.simple_spinner_item,testItems); holder.testitemSpinner.setAdapter(a); //} //holder.testitemSpinner.setTag(position); holder.testitemSpinner .setOnItemSelectedListener(new ItemClickSelectListener( holder));//为checkinfo_item_value(Spinner)添加ItemSelectedListener监听器 holder.unit_text = (TextView) convertView.findViewById(R.id.unit_text); //holder.textView = (TextView) convertView.findViewById(R.id.text); holder.downbtn = (Button) convertView.findViewById(R.id.downbtn); holder.downbtn.setFocusable(false); holder.downbtn.setFocusableInTouchMode(false); holder.upbtn = (Button) convertView.findViewById(R.id.upbtn); holder.upbtn.setFocusable(false); holder.upbtn.setFocusableInTouchMode(false); holder.editbtn = (Button)convertView.findViewById(R.id.editbtn); holder.editbtn.setFocusable(false); holder.editbtn.setFocusableInTouchMode(false); /*times_edit控件*/ holder.numEdit = (EditText) convertView.findViewById(R.id.times_edit); holder.numEdit.setTag(position);//给numEdit添加一个格外的数据,使用getTag()可将数据取出; holder.numEdit.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_UP) { index = (Integer) v.getTag(); } return false; } }); /***********/ /*parameter_edti控件*/ holder.paraEdit = (EditText) convertView.findViewById(R.id.parameter_edit); holder.paraEdit.setTag(position);//给numEdit添加一个格外的数据,使用getTag()可将数据取出; holder.paraEdit.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_UP) { index = (Integer) v.getTag(); } return false; } }); /*******/ /*为times_edit控件添加编辑响应*/ class MyTextWatcher implements TextWatcher {
public MyTextWatcher(ViewHolder holder) { mHolder = holder; } private ViewHolder mHolder; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub
} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub if (s != null && !"".equals(s.toString())) { int position = (Integer) mHolder.numEdit.getTag(); // 当EditText数据发生改变的时候存到data变量中 mData.get(position).put("list_item_timesvalue", s.toString()); } } } holder.numEdit.addTextChangedListener(new MyTextWatcher(holder)); /*********/ /*为parameter_edit控件添加编辑响应*/ class ParaTextWatcher implements TextWatcher { public ParaTextWatcher(ViewHolder holder) { mHolder = holder; } private ViewHolder mHolder; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub
} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub
} @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub if (s != null && !"".equals(s.toString())) { int position = (Integer) mHolder.numEdit.getTag(); // 当EditText数据发生改变的时候存到data变量中 mParameter.get(position).put("list_item_parameter", s.toString()); } } } holder.paraEdit.addTextChangedListener(new ParaTextWatcher(holder)); /********/ holder.downbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int position = (Integer) holder.numEdit.getTag(); Log.d("zhang", "clickposition = " + position); String edittextStr = holder.numEdit.getText().toString(); int num = Integer.parseInt(edittextStr); num--; mData.get(position).put("list_item_timesvalue", num+""); holder.numEdit.setText(num+""); } }); holder.upbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int position = (Integer) holder.numEdit.getTag(); String edittextStr = holder.numEdit.getText().toString(); int num = Integer.parseInt(edittextStr); num++; mData.get(position).put("list_item_timesvalue", num+""); holder.numEdit.setText(num+""); } }); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); holder.numEdit.setTag(position); holder.paraEdit.setTag(position); } /*holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub int position = (Integer) holder.numEdit.getTag(); isSelected.put(position, isChecked);//将对应position键值的数值设置为isChecked()返回的值 } }); holder.checkBox.setChecked(isSelected.get(position));//更新CheckBox的选择状态*/ holder.testitemSpinner.setPrompt(promptForSpinner[position]); int spinnerOptionPosition = selectedtestItem.get(promptForSpinner[position]); holder.testitemSpinner.setSelection(spinnerOptionPosition); //if ((spinnerOptionPosition==4)||(spinnerOptionPosition==5)) //holder.unit_text.setText("hz"); //else //holder.unit_text.setText("__");
Object value = mData.get(position).get("list_item_timesvalue"); if (value != null && !"".equals(value)) { holder.numEdit.setText(value.toString()); } else { holder.numEdit.setText("200"); } holder.numEdit.clearFocus(); if (index != -1 && index == position) { holder.numEdit.requestFocus(); } Object value2 = mParameter.get(position).get("list_item_parameter"); if (value2 != null && !"".equals(value2)) { holder.paraEdit.setText(value2.toString()); } else { holder.paraEdit.setText("200"); } holder.paraEdit.clearFocus(); if (index != -1 && index == position) { holder.paraEdit.requestFocus(); } return convertView; }
/*public HashMap<Integer,Boolean> getIsSelected() { return isSelected; }*/
/*public void setIsSelected(HashMap<Integer,Boolean> isSelected) { this.isSelected = isSelected; }*/ public class ViewHolder { Spinner testitemSpinner; Button editbtn; Button downbtn; EditText numEdit; EditText paraEdit; TextView unit_text; Button upbtn;
} private class ItemClickSelectListener implements OnItemSelectedListener { //Spinner testitemSpinner; ViewHolder Iholder; //EditText Iedit; public ItemClickSelectListener(ViewHolder holder) { //this.testitemSpinner = testitemSpinner; this.Iholder = holder; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //关键代码 selectedtestItem.put(Iholder.testitemSpinner.getPrompt().toString(), position); /*根据所选的测试项目,改变第二参数的不同单位和信息*/ if((position==4)||(position==5)) Iholder.unit_text.setText("hz"); else Iholder.unit_text.setText("un"); if((position==0)||(position==1)||(position==2)||(position==6)||(position==7)||(position==8)) { //Iholder.paraEdit.setText("null"); Iholder.paraEdit.setVisibility(View.INVISIBLE); Iholder.unit_text.setVisibility(View.INVISIBLE); } else { //Iholder.paraEdit.setText("200"); Iholder.paraEdit.setVisibility(View.VISIBLE); Iholder.unit_text.setVisibility(View.VISIBLE); } /***********/ if(position==8) Iholder.editbtn.setVisibility(View.VISIBLE); else Iholder.editbtn.setVisibility(View.INVISIBLE);
//int p = (Integer) testitemSpinner.getTag(); //selectedtestItem.get(p).put("list_item_Name", //position); } @Override public void onNothingSelected(AdapterView<?> arg0) { } } } |
ListView listView = (ListView) findViewById(R.id.list); MyListAdapter mAdapter = new MyListAdapter(this,testItems);//testItems是一个String数组; listView.setAdapter(mAdapter); |