在項目中要在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());
}
}
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11