如何根据Parse数据库检查用户记录的输入

时间:2015-06-23 19:38:45

标签: android parse-platform

我是Android编程的新手,我的开发已经遇到了障碍。我正在学习如何解析数据库中的信息,但却陷入了困境。我试图找到数据库中是否存在用户名我开始一个新的意图,如果它不存在我想要提供一些消息不存在所有这一切都是在用户点击按钮后完成的“登录”,但我的登录按钮没有做任何事情。事实上当我尝试将一些方法分配给按钮“使用onlick”时,只显示了两种方法。 setContentView & setListFooter 我需要一些有关此问题的指导,任何人都可以帮助我吗?

  public class Login extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    final EditText username= (EditText) findViewById(R.id.userloginname);
    final EditText password = (EditText) findViewById(R.id.userpassword);


    TextView neighbourView = new TextView(this);

    Button button_test;
    button_test = (Button) findViewById(R.id.btnLogin);

    button_test.setOnClickListener(new Button.OnClickListener() {


            public void onClick(View v) {

                String usersname = username.getText().toString();
                String passwoord = password.getText().toString();

                ParseObject parkingobject = new ParseObject("Parking");
                parkingobject.put("username",usersname);
                //Toast.makeText(getApplicationContext(), "password is : " + passwoord +" Username is  " + usersname + "  ", Toast.LENGTH_LONG).show();
                // System.out.println(username + "" + password);
                //Log.i(usersname + usersname, passwoord + passwoord);
                parkingobject.saveInBackground();



                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("username", usersname);

                query.countInBackground(new CountCallback() {

                    @Override
                    public void done(int count, ParseException e) {
                        // TODO Auto-generated method stub
                        if (e == null) {
                            if (count == 0) {

                                //Username doesnt exit

                                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                                startActivity(browserIntent);
                            }
                            else{
                                Toast.makeText(getApplicationContext(), "user name already exists", Toast.LENGTH_LONG).show();

                            }
                        }
                    }

                });

            }

这是我的XML

     <!--  Username Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="@string/username"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="20dip"
        android:singleLine="true"
        android:id="@+id/userloginname"
        android:inputType="text" />
    <!--  Password Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="@string/password"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:password="true"
        android:inputType="textPassword"
        android:id="@+id/userpassword" />
    <!-- Login button -->
    <Button android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/loginButton"/>

2 个答案:

答案 0 :(得分:0)

试试这个:

  ParseQuery<ParseObject> query = ParseQuery.getQuery("Parking");
        query.whereEqualTo("username", usersname);
         query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> List, ParseException e) {
            if (e == null) {
                if(List!=null){
                    if(List.size()>0){
                        Toast.makeText(getApplicationContext(), "user name already exists", Toast.LENGTH_LONG).show();
                      }
                   else{
                      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                                startActivity(browserIntent);  
                      }
                 }
            } else {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                                startActivity(browserIntent);
            }
        }
else{
Log.d("Exception",e.getMessage());  
}
    });

答案 1 :(得分:0)

我认为既然您正在寻找数量,那么最好使用原始代码并进行少量修改,如下所示。

     ParseObject parkingobject = new ParseObject("Parking");
                    parkingobject.put("username",usersname);
                                 parkingobject.saveInBackground(new SaveCallback() {
       public void done(ParseException e) {
         if (e == null) {
             ParseQuery<ParseUser> query = ParseUser.getQuery();
                    query.whereEqualTo("username", usersname);

                    query.countInBackground(new CountCallback() {

                    @Override
                    public void done(int count, ParseException e) {
                        // TODO Auto-generated method stub
                        if (e == null) {
                            if (count == 0) {

                                //Username doesnt exit

                                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                                startActivity(browserIntent);
                            }
                            else{
                                Toast.makeText(getApplicationContext(), "user name already exists", Toast.LENGTH_LONG).show();

                            }
                        }
                    }

                });
     } else {
       //myObjectSaveDidNotSucceed();
     }
   }
 });