0
public class MainActivity extends Activity {

    private String md5(String in) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.reset();
            digest.update(in.getBytes());
            byte[] a = digest.digest();
            int len = a.length;
            StringBuilder sb = new StringBuilder(len << 1);
            for (int i = 0; i < len; i++) {
                sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
                sb.append(Character.forDigit(a[i] & 0x0f, 16));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText text = (EditText) findViewById(R.id.editText1);
                EditText text1 = (EditText) findViewById(R.id.editText2);
                String userid = text.getText().toString();
                String pass = text1.getText().toString();

                Toast.makeText(
                        MainActivity.this,
                        "Entered " + userid + " and password entered is "
                                + pass, Toast.LENGTH_LONG).show();

                pass = md5(pass + "@string/salt");

                Toast.makeText(
                        MainActivity.this,
                        "Password after adding a salt and md5 hashing is now equal to "
                                + pass, Toast.LENGTH_LONG).show();

                /* This Line */DBAdapter db = new DBAdapter(this); // This Line.
                db.open();
                Cursor c = db.getPasswordForUserName(userid);
                if (c.moveToFirst()) {
                    if (c.getString(1) == pass) {
                        Toast.makeText(MainActivity.this,
                                "Authentication Succeded", Toast.LENGTH_SHORT)
                                .show();
                        // proceed
                    } else {
                        Toast.makeText(MainActivity.this, "@string/AuthFail",
                                Toast.LENGTH_SHORT).show();
                        // AuthFailure
                    }
                } else {
                    Toast.makeText(MainActivity.this, "@string/UserNotFound",
                            Toast.LENGTH_SHORT).show();
                    // where to from here
                }
            }
        });
    }
}

I am very new to Android programming. So I have taken code snippets from a lot of places and then combined them.

Basically what all I want to do is to query a local SQLite DB for existing value of UserName and Password + salt(with md5 hash).

So I copied some code to create a database helper class (DBAdapter), which had pretty basic methods to deal with the database.

The problem now is that in the MainActivity.java file. The line marked "this line" in the code gives the error The constructor DBAdapter(new View.OnClickListener(){}) is undefined. I do not know what this means. What should I do to make it work?

4

2 に答える 2

2

私は Try MainActivity.this を推測することしかできません..

DBAdapter db = new DBAdapter(MainActivity.this);
于 2013-03-07T23:22:39.047 に答える
1

Androidの仕組みについてもっと読むことをお勧めします。この特定のケースでは、 Contextsについて知る必要がありますが、ここから始めます

既に提案したように、MainActivity.thisを使用して DBAdapter に正しいアプリケーション コンテキストを提供できます。

于 2013-03-07T23:30:36.257 に答える