Android 如何搜索从数据库输入edittext值

Android 如何搜索从数据库输入edittext值,android,database,sqlite,android-edittext,Android,Database,Sqlite,Android Edittext,我要验证手机号码,输入编辑文本值搜索和数据库我要获取错误已注册您的号码进行验证setError或Toast消息,添加Main类、Model类和AppUtil类请帮助我,我附上下面给出的代码。请告诉我设置了什么验证 public class CreateRyotManagerFragment extends Fragment { EditText mRyotName, mRyotFatherName, mAddress, mIdNo, mMobile1; Spinner mDivision; Li

我要验证手机号码,输入编辑文本值搜索和数据库我要获取错误已注册您的号码进行验证setError或Toast消息,添加Main类、Model类和AppUtil类请帮助我,我附上下面给出的代码。请告诉我设置了什么验证

public class CreateRyotManagerFragment extends Fragment {
EditText mRyotName, mRyotFatherName, mAddress, mIdNo, mMobile1;
Spinner mDivision;
List<RyotMasterDb> mRyotList;
List<DivitionMasterDb> mDistrickarray;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.layout_new_ryot, container, false);

    mMobile1 = view.findViewById(R.id.mobile);
    mDivision = view.findViewById(R.id.divition);
    mRyotList = AppUtils.getRyotMasterList();
    mDistrickarray = AppUtils.getDistrictMasterLIst();

    SpinnerAfdapterDistric divisionAdapter = new SpinnerAfdapterDistric(getActivity().getApplicationContext(), mDistrickarray);
    mDivision.setAdapter(divisionAdapter);

}
}

公共类应用程序{
静态碎片;
公共静态列表getRyotMasterList(){
返回Select.from(RyotMasterDb.class).fetch();
}

}

您可以为编辑文本设置键侦听器

editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_ENTER){
                // do your search in here    
                String mobileno=mmobile.getText().toString();
                //search it  
                boolean isInList = checkIsPhoneNumberInList(mRyotList,mobileno);
                Log.i("CheckResult","is my phone number "+mobileno+" in mRyotList: "+isInList);
                if(isInList){
                   Toast.makeText(getActivity(),"your phone number is already registered",Toast.Toast.LENGTH_LONG).show(); 
                }
            }
            return false;
        }
    });
用于通过此方法检查您在edittext中输入的手机是否在数据库中

private boolean checkIsPhoneNumberInList(List<RyotMasterDb> mRyotList,String phoneNumber){
   if (mRyotList == null || phoneNumber == null) {
       return false;
   }
   boolean isIn = false;
   for (RyotMasterDb rm: mRyotList) {
       if (phoneNumber.equals(rm.getMobile_1())||phoneNumber.equals(rm.getMobile_2())){
           isIn = true;
       }
   }
   return isIn;
}
private boolean checkIsPhoneNumberInList(列表mRyotList,字符串phoneNumber){
if(mRyotList==null | | phoneNumber==null){
返回false;
}
布尔isIn=false;
用于(RyotMasterDb rm:mRyotList){
if(phoneNumber.equals(rm.getMobile_1())| | phoneNumber.equals(rm.getMobile_2()){
isIn=真;
}
}
返回isIn;
}

我建议您使用
AutoCompleteTextView
。一个可编辑的文本视图,在用户键入时自动显示完成建议。建议列表显示在下拉菜单中,用户可以从中选择要替换编辑框内容的项目

可随时按后退键取消下拉列表,如果下拉列表中未选择任何项目,则按enter/dpad center键取消下拉列表

建议列表是从数据适配器获得的,仅在定义的给定字符数之后显示

代码示例:

public class CountriesActivity extends Activity {
   protected void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setContentView(R.layout.countries);

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, COUNTRIES);
       AutoCompleteTextView textView = (AutoCompleteTextView)
             findViewById(R.id.countries_list);
       textView.setAdapter(adapter);
   }

   private static final String[] COUNTRIES = new String[] {
       "Belgium", "France", "Italy", "Germany", "Spain"
   };
}
公共类CountriesActivity扩展活动{
创建时受保护的空隙(捆绑冰柱){
超级冰柱;
setContentView(R.layout.countries);
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_下拉列表_项目_1行,国家/地区);
AutoCompleteTextView文本视图=(AutoCompleteTextView)
findViewById(R.id.国家/地区列表);
setAdapter(适配器);
}
私有静态最终字符串[]国家/地区=新字符串[]{
“比利时”、“法国”、“意大利”、“德国”、“西班牙”
};
}

有关详细说明,请参阅。

mRyotList
其类型是什么?
private boolean checkIsPhoneNumberInList(List<RyotMasterDb> mRyotList,String phoneNumber){
   if (mRyotList == null || phoneNumber == null) {
       return false;
   }
   boolean isIn = false;
   for (RyotMasterDb rm: mRyotList) {
       if (phoneNumber.equals(rm.getMobile_1())||phoneNumber.equals(rm.getMobile_2())){
           isIn = true;
       }
   }
   return isIn;
}
public class CountriesActivity extends Activity {
   protected void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setContentView(R.layout.countries);

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, COUNTRIES);
       AutoCompleteTextView textView = (AutoCompleteTextView)
             findViewById(R.id.countries_list);
       textView.setAdapter(adapter);
   }

   private static final String[] COUNTRIES = new String[] {
       "Belgium", "France", "Italy", "Germany", "Spain"
   };
}