从BaseAdapter类外部对EditText进行验证

时间:2016-02-08 13:58:21

标签: android listview baseadapter

我的列表框包含列表框每行的EditBox。在按钮保存我必须检查是否所有EditText对象填充数据。

这是如何为ListView构建适配器:

public class LvAdapter extends BaseAdapter {

    Activity activity;
    Context context;
    ArrayList<PList> arrayPList;

    public LvAdapter(Context mContext, ArrayList<PList> array) {
         context = mContext;
         activity = (Activity) context;
         arrayPreStart = array;
    }       

    @Override
    public int getCount() {
        return arrayPList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arrayPList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ViewHolder viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row_prestartcheck_list, null);
        viewHolder.edComment = (EditText) convertView.findViewById(R.id.edComment);
        viewHolder.edComment.setTag(position);
        return convertView;
    }
   }

public class ViewHolder {
        EditText edComment;
}

在主类中定义一个自定义方法以检查任何空的EditText: MainClass.java:

ListView lvQuestion;
ArrayList<PList> arrayPList = new ArrayList<>();
PList pList = new PList();
btnEnter.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            boolean isAnyEmpty=true;
            for (int i=0; i<arrayPList.size(); i++) {
                 View view =  lvQuestion.getAdapter().getView(i, null, null);
                 EditText quantity = (EditText) view.findViewById(R.id.edComment);
                quantity.requestFocus(); 
                quantity.setError("asdasda");
                isAnyEmpty=false;
                break;
            }
        });
}

我已经使用setText测试了该对象并且对我工作正常但只有requestfocus和setError无法正常工作,请帮助弄清楚这一点或给我替代解决方案

1 个答案:

答案 0 :(得分:0)

您需要设置一个条件来检查EditText是否为空。

像这样改变你的方法

public boolean isEmpty(){
    boolean isAnyEmpty=false;
    for (int i=0;i<quantityCheckList.size();i++){
        View view =  lvQuestion.getAdapter().getView(i, null, null);
        EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
        if(quantity.getText().toString().isEmpty())
        {
            quantity.requestFocus(); 
            quantity.setError("asdasda");
            isAnyEmpty=true;
            break;
        }
   }
   return isAnyEmpty;
}