I have problem in validating Edittext in listview in onFocusChangeListner.
My UI looks like this
MeterName Previous CurrentReading
Meter1 100 Here i want to type current reading
When I type the current Reading it will compare with previous reading. Current Reading can’t be less than Previous. If it is less than previous then I want to alert the user and focus is on same EditBox.
My code is here:
public void onFocusChange(View v, boolean hasFocus) {
try {
Previous_Reading = getArray_Meter_Reading.getJSONObject(holder.ref)
.getString("PrevMeterReading").toString();
Cumulative = getArray_Meter_Reading
.getJSONObject(holder.ref).getString("Cumulative")
.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!hasFocus){
int Current=Integer.valueOf(holder.adp_Current.getText().toString());
Previous=Integer.parseInt(Previous_Reading);
if(Current< Previous ){
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("WARNING");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setMessage("Please Enter UserName");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Caption.requestFocus();
}
});
AlertDialog diag = builder.create();
diag.show();
}
}
} });
With this code, when I start typing in EditBox alert is shown for all the characters, what I type like if previous is 600 and I type current reading like 6 it shows me alertdialog then
5 again alertdialog till it is greater than previous value.
When I click on EditText hasFocus is true and when I type any number say 5 focus changes to false.
Can any one tell me how to do validation for that by giving sample code?
Have a look at this . Then, extending that concept to your problem, I would suggest: