Android ListView中使用EditText

在项目中要在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());

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7a60ed69d4e3cf17397eaf212ba2f3bc.html