0

I have ListView, populating rows using adapter class. In row i have EditText. Now when i click submit button in activity, i need to check all rows edit texts for empty. If any EditText is empty i want to display toast message. How to achieve this. small code snippet need.

Linh
  • 43,513
  • 18
  • 206
  • 227

4 Answers4

2

I think the simplest way is
- You store the data in the EditText whenever user input data into an Object or List
- Then when Submit Button is clicked, just need to check the Object is null or not

Or

You can find each item (row) in ListView by position

 public View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

Then find the EditText in each row

EditText editText  = getViewByPosition(pos,listView).findViewById(R.id.editext_id);

Finally check it empty or not

if(editText.getText().length == 0){
        // EditText is empty, display toast here
        Toast.makeText("","");
}

Hope this help

Linh
  • 43,513
  • 18
  • 206
  • 227
1

Something like this

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}

This is an example. Of course, you need to get this EditText from your row first.

RexSplode
  • 1,347
  • 1
  • 11
  • 22
0

Loop through the items of the listview, get the TextView by id from the listview item and check the length of its text

You can get the listview item like the following Link

for(int i = 0; i<listview.getCount(); i++){
        TextView tv = ((View)listview.getViewByPosition(i, listview)).findViewById(R.id.textView);
        if(tv.getText().toString().length() == 0){
            tv.setError("This is required");
        }
    }
Community
  • 1
  • 1
Malek Hijazi
  • 3,762
  • 1
  • 23
  • 31
0

This will help you for what you want it will check the empty cell of list:

  private int checkForEditText() {
    for (int i = 0; i < yourListSize; i++) {
        if (TextUtils.isEmpty(youlistSize.get(i))) {
            //return positin of empty cell
            return i;
        }
    }
   //if no cell is empty it retur -1
    return -1;

}
santoXme
  • 734
  • 3
  • 18