Abe February 2016

Validation on EditText from outside BaseAdapter class

I have list box that consist EditBox in each row of listbox. On button Save I have to check is all EditText object filled with data or not.

This is how is constructed adapter for 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;
}

Define one custom method in main class to check for any empty 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(); 
           

Answers


Rohit5k2 February 2016

You need to put a condition to check if any of the EditText is empty.

Change your method like this

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;
}

Post Status

Asked in February 2016
Viewed 2,721 times
Voted 12
Answered 1 times

Search




Leave an answer