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

时间:2018-11-01 09:06:03

标签: android database sqlite android-edittext

我要验证手机号码,我要输入数据库的编辑文本值搜索,我要获取错误已经为验证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);

}

}

@Table(name = "ryotMasterDB", database = AppDatabase.class)
RyotMasterDb extends Model implements Serializable {
@PrimaryKey
public Long id;

@Column(name = "mobile_1")
public String mobile_1;
@Column(name = "mobile_2")
public String mobile_2;
@Column(name = "zone_group_id")
public String zone_group_id;
@Column(name = "bank_id")
public String bank_id;
@Column(name = "branch_id")
public String branch_id;
@Column(name = "sb_ac_no")
public String sb_ac_no;
@Column(name = "state")
public String state;
@Column(name = "remoteID")
public String remoteID;

public RyotMasterDb() {

}

public RyotMasterDb(String remoteId, String imageData, String ryotNo, String catId, String name, String fatherName, String divId, String secID, String villID, String address, String address1, String add2, String add3, String iiSourceID, String irrType, String totalArea, String suitableArea, String pCode, String disID, String talukID, String firID, String factID, String pontentialRyot, String idProof, String idNo, String mobile1, String mobil2m,String passbook_image, String id_proof_image, String id_proof_back_side_image) {

    this.remoteID = remoteId;
    this.id = Long.getLong(remoteId);

    this.mobile_1 = mobile1;
    this.mobile_2 = mobil2m;
    this.zone_group_id = zoneGroup;
    this.bank_id = bankID;
    this.branch_id = branchID;
    this.sb_ac_no = sbAccNO;
    this.state = state;


}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


public String getMobile_1() {
    return mobile_1;
}

public void setMobile_1(String mobile_1) {
    this.mobile_1 = mobile_1;
}

public String getMobile_2() {
    return mobile_2;
}

public void setMobile_2(String mobile_2) {
    this.mobile_2 = mobile_2;
}

public String getZone_group_id() {
    return zone_group_id;
}

public void setZone_group_id(String zone_group_id) {
    this.zone_group_id = zone_group_id;
}

public String getBank_id() {
    return bank_id;
}

public void setBank_id(String bank_id) {
    this.bank_id = bank_id;
}

public String getBranch_id() {
    return branch_id;
}

public void setBranch_id(String branch_id) {
    this.branch_id = branch_id;
}

public String getSb_ac_no() {
    return sb_ac_no;
}

public void setSb_ac_no(String sb_ac_no) {
    this.sb_ac_no = sb_ac_no;
}

}

public class AppUtils {

static Fragment mFragment;

public static List<RyotMasterDb> getRyotMasterList() {
    return Select.from(RyotMasterDb.class).fetch();
}

}

2 个答案:

答案 0 :(得分:1)

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

可以通过按返回键随时删除该下拉菜单,如果下拉菜单中未选择任何项目,则可以按Enter / dpad中心键。

建议列表是从数据适配器获得的,并且仅在由the threshold定义的给定数目的字符之后出现。

代码示例:

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"
   };
}

您可以参考this example以获得详细说明。

答案 1 :(得分:0)

您可以为EditText设置keyListener

"\"If you like your doctor, you can keep your doctor.\" -  I don't know
if {Michelle } noticed, but I am not White & I am Not Male.  being
honored by for his rap battle win against"

通过这种方法检查在编辑文本中输入的电话是否在数据库中

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;
        }
    });