0

I have a listview with an EditText in each row. Each EditText has his validation, so, when I click the MenuItem "Finish", I want to make the validation of each EditText and show errors if there were any.

I know how to access my EditText inside my adapter, but inside my adapter, I can't access MenuItem. Outside my adapter, I can access my MenuItem OnClick, but I can't get my edittext values anymore...

Any solution???

Juliatzin
  • 18,455
  • 40
  • 166
  • 325
  • You could set a field in your adapter (like `needValidation`) to `true` when the user click on the "Finish" menu. And then call `notifyDataSetChanged()` on your adapter. So you will have access to your views again. Once the validation is done, set `needValidation` to `false` again. – Gaëtan Dec 16 '14 at 16:04

3 Answers3

3

You can iterate through your listView and do something like:

View v = myList.getAdapter().getView(i, null, null);
EditText et = (EditText) v.findViewById(nameOfTheView);

Check this answer.

Community
  • 1
  • 1
Biniou
  • 133
  • 1
  • 9
  • By using `null` for the `convertView` and `parent` parameters of the `getView(int, View, ViewGroup)` method, you loose the recycling strategy and may end up with the wrong `LayoutParam` for the current view. – Gaëtan Dec 16 '14 at 16:26
  • I can access the values with that, but I can't display errors with setErrors. Do you know how to modify the view inside the adapter? – Juliatzin Dec 16 '14 at 18:54
0

I solved using : ListView item set error Android

The solution was to include an ArrayList of errors in the adapter.

Tx for your help!

Community
  • 1
  • 1
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
-1

One thing you could do is setup a class to hold variables:

public class MyVarHolder {
    public EditText myText;
}

Then you use it into your getView method:

mHolder.myText = (EditText) root.findViewById(R.id.my_edit_text);
convertView.setTag(mHolder);

Last step is get the holder into the onClickListener of your list:

onItemClickListener(AdapterView<?> adapterView, View view, int position, long id) {
    MyVarHolder tag = (MyVarHolder)view.getTag();

   String editText = tag.myText.getText().toString();

   ...Do whatever you want...

}
MineConsulting SRL
  • 2,340
  • 2
  • 17
  • 32