2015-03-25 57 views
0

我想從用戶的名字和姓氏以及動態輸入數量的變化;同時驗證輸入不起作用。Android在edittext中驗證用戶輸入不適用於適配器?

驗證的代碼是;

public class IndividualAdapter extends ArrayAdapter<Individual> { 
    List<Individual> individualList; 
    LayoutInflater vi; 
    ViewHolder holder; 

    public IndividualAdapter(Context context, List<Individual> objects) { 
     super(context, R.layout.inidividual_list, objects); 
     vi = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     individualList = objects; 
    } 
    static class ViewHolder { 
     public EditText edtfName; 
     public EditText edtlName; 
     private TextView txtType; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     Individual ind = getItem(position); 
     if (v == null) { 
      holder = new ViewHolder(); 
      v = vi.inflate(R.layout.inidividual_list, parent, true); 
      holder.edtfName = (EditText)findViewById(R.id.fst_name); 
      holder.edtlName = (EditText)findViewById(R.id.lst_name); 
      holder.txtType = (TextView)findViewById(R.id.typeValue); 
     } 
     holder.txtType.setText(ind.getType()); 
     String fName = holder.edtfName.getText().toString().trim(); 
     String lName = holder.edtlName.getText().toString().trim(); 
     if (fName.equals(" ")) { 
      holder.edtfName.setError("First Name Can not be empty"); 
      holder.edtfName.setText("First Name Can not be empty"); 
     } 
     if (lName.equals(" ")) { 
      holder.edtlName.setError("Last Name Can not be empty"); 
      holder.edtlName.setText("Last Name Can not be empty"); 
     } 
     return v; 
    } 
} 

只要我在提交文本字段後點擊下一個按鈕,我的應用就會崩潰。
沒有填充字段,它不顯示任何錯誤。

回答

0

爲什麼不是你在活動檢查空例外class.I覺得你在做什麼,是不是coding.However的一個很好的做法,爲什麼不試試這樣下面

if(fName!=null && fName.length()>0) 
+0

謝謝,我想如上面提到的,但所有的解決方案,這是什麼事,我通過這個活動它翔ws錯誤輸入anydata之前編輯文本字段,並在我輸入後需要傳遞給下一個活動,但它不? – 2015-03-26 05:22:38

1
EditText txtUserName = (EditText) findViewById(R.id.txtUsername); 
String strUserName = usernameEditText.getText().toString(); 
if (strUserName.trim().equals("")) { 
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show(); 
    return; 
} 
or use the TextUtils class like this : 

if(TextUtils.isEmpty(strUserName) { 
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show(); 
    return; 
} 
0

那是因爲你修剪文字和檢查if (fName.equals(" "))。嘗試移除空間和檢查這樣if (fName.equals(""))

相關問題