转载

Android ListView中使用EditText

自从5月份开始Go的开发和学习以来,已经大半年没有写Android了。然而这周重新打开Android Studio帮公司写了一个很小的项目。然后在项目中要在Listview嵌套EditText使用。

EditText 在android ListView中不像在Activity或者Fragments中那么方便。Google了很久没有找到解决方案。

最终在朋友的帮助下,填平了这个坑。Talk is cheap Show me the code。

/**  * Created by xiequan on 12/1/15.  */ public class CooperationAdapter extends BaseAdapter {     private Context context;     private List<InfoList> dataList;     private LayoutInflater layoutInflater;      public CooperationAdapter(Context context, List<InfoList> dataList) {         this.context = context;         this.dataList = dataList;         layoutInflater = LayoutInflater.from(context);     }     public List<InfoList> getDataList(){         return dataList;     }       @Override     public int getCount() {         return dataList.size();     }      @Override     public Object getItem(int i) {         return dataList.get(i);     }      @Override     public long getItemId(int i) {         return i;     }        @Override     public View getView(int position, View convertView, ViewGroup viewGroup) {         ViewHolder viewHolder;         if (convertView == null || convertView.getTag() == null) {             convertView = layoutInflater.inflate(R.layout.cooperatonlistview, null);             viewHolder = new ViewHolder(convertView);             convertView.setTag(viewHolder);         } else {             viewHolder = (ViewHolder) convertView.getTag();         }         viewHolder.refreshData(dataList.get(position));         return convertView;     }      private class ViewHolder {         TextView projectname;         EditText projectprogrress;         EditText projectanalyze;         InfoList mInfo;          ViewHolder(View parent) {             initViewHolder(parent);         }          public void initViewHolder(View parent) {             projectname = (TextView) parent.findViewById(R.id.projectname);             projectprogrress = (EditText) parent.findViewById(R.id.projectprogrress);             projectanalyze = (EditText) parent.findViewById(R.id.projectanalyze);             projectprogrress.addTextChangedListener(new TextWatcher() {                 @Override                 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {                  }                  @Override                 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {                     mInfo.setWsifieldvalue(charSequence.toString());                 }                  @Override                 public void afterTextChanged(Editable editable) {                  }             });             projectanalyze.addTextChangedListener(new TextWatcher() {                 @Override                 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {                  }                  @Override                 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {                      mInfo.setMark(charSequence.toString());                 }                  @Override                 public void afterTextChanged(Editable editable) {                  }             });         }          public void refreshData(InfoList info) {             mInfo = info;             projectname.setText(info.getWsifname());             projectprogrress.setText(info.getWsifieldvalue());             projectanalyze.setText(info.getMark());          }        } }
/**  * Created by xiequan on 12/1/15.  */ publicclassCooperationAdapterextendsBaseAdapter{     privateContextcontext;     privateList<InfoList>dataList;     privateLayoutInflaterlayoutInflater;       publicCooperationAdapter(Contextcontext,List<InfoList>dataList){         this.context=context;         this.dataList=dataList;         layoutInflater=LayoutInflater.from(context);     }     publicList<InfoList>getDataList(){         returndataList;     }         @Override     publicintgetCount(){         returndataList.size();     }       @Override     publicObjectgetItem(inti){         returndataList.get(i);     }       @Override     publiclonggetItemId(inti){         returni;     }           @Override     publicViewgetView(intposition,ViewconvertView,ViewGroupviewGroup){         ViewHolderviewHolder;         if(convertView==null||convertView.getTag()==null){             convertView=layoutInflater.inflate(R.layout.cooperatonlistview,null);             viewHolder=newViewHolder(convertView);             convertView.setTag(viewHolder);         }else{             viewHolder=(ViewHolder)convertView.getTag();         }         viewHolder.refreshData(dataList.get(position));         returnconvertView;     }       privateclassViewHolder{         TextViewprojectname;         EditTextprojectprogrress;         EditTextprojectanalyze;         InfoListmInfo;           ViewHolder(Viewparent){             initViewHolder(parent);         }           publicvoidinitViewHolder(Viewparent){             projectname=(TextView)parent.findViewById(R.id.projectname);             projectprogrress=(EditText)parent.findViewById(R.id.projectprogrress);             projectanalyze=(EditText)parent.findViewById(R.id.projectanalyze);             projectprogrress.addTextChangedListener(newTextWatcher(){                 @Override                 publicvoidbeforeTextChanged(CharSequencecharSequence,inti,inti1,inti2){                   }                   @Override                 publicvoidonTextChanged(CharSequencecharSequence,inti,inti1,inti2){                     mInfo.setWsifieldvalue(charSequence.toString());                 }                   @Override                 publicvoidafterTextChanged(Editableeditable){                   }             });             projectanalyze.addTextChangedListener(newTextWatcher(){                 @Override                 publicvoidbeforeTextChanged(CharSequencecharSequence,inti,inti1,inti2){                   }                   @Override                 publicvoidonTextChanged(CharSequencecharSequence,inti,inti1,inti2){                       mInfo.setMark(charSequence.toString());                 }                   @Override                 publicvoidafterTextChanged(Editableeditable){                   }             });         }           publicvoidrefreshData(InfoListinfo){             mInfo=info;             projectname.setText(info.getWsifname());             projectprogrress.setText(info.getWsifieldvalue());             projectanalyze.setText(info.getMark());           }           } } 

Android ListView中使用EditText

正文到此结束
Loading...