从我的数据库中重新获取数据库值,然后在程序化短信中使用它们?

时间:2016-03-03 02:25:04

标签: android android-studio

我很困惑,无法弄清楚如何使用存储在我的数据库中的值发送短信。

SMS将显示如下:('NAME'... Message content, etc..),然后将使用用户在sqlite数据库上输入的联系号码发送消息。

这是我在注册时用来获取数据的代码。

public class LoginDataBaseAdapter  {
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
        "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text, PASSWORD text, NAME text, C1 integer, C2 integer); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context) {
    context = _context;
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public LoginDataBaseAdapter open() throws SQLException {
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close()
{
    db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public void insertEntry(String userName,String password, String name, String cn1, String cn2) {
    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("USERNAME", userName);
    newValues.put("PASSWORD",password);
    newValues.put("NAME",name);
    newValues.put("C1", cn1);
    newValues.put("C2", cn2);

    // Insert the row into your table
    db.insert("LOGIN", null, newValues);
    // Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}

public int deleteEntry(String UserName) {
    //String id=String.valueOf(ID);
    String where="USERNAME=?";
    int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
    // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
    return numberOFEntriesDeleted;
}

public String getSingleEntry(String userName) {
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) { // username doesn't exist
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
    cursor.close();
    return password;
}

public boolean isExist (String userName) {
    boolean exists;
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
    if (cursor.getCount()>0) { // username exists
        exists = true;
        cursor.close();
        return exists;
    }
    return false;
}

public void updateEntry(String userName,String password) {
    // Define the updated row content.
    ContentValues updatedValues = new ContentValues();
    // Assign values for each row.
    updatedValues.put("USERNAME", userName);
    updatedValues.put("PASSWORD", password);

    String where="USERNAME = ?";
    db.update("LOGIN",updatedValues, where, new String[]{userName});
}}

这是SignUpActivity

public class SignUpActivity extends AppCompatActivity {

Button bSignup;
TextView tvSign;
EditText etUN, etPW, etPW2, etFN, etC1, etC2;
LoginDataBaseAdapter loginDataBaseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    loginDataBaseAdapter = new LoginDataBaseAdapter(this);
    loginDataBaseAdapter = loginDataBaseAdapter.open();

    bSignup = (Button)findViewById(R.id.bSignup);
    tvSign = (TextView)findViewById(R.id.tvSign);
    etUN = (EditText)findViewById(R.id.etUN);
    etPW = (EditText)findViewById(R.id.etPW);
    etPW2 = (EditText)findViewById(R.id.etPW2);
    etFN = (EditText)findViewById(R.id.etFN);
    etC1 = (EditText)findViewById(R.id.etC1);
    etC2 = (EditText)findViewById(R.id.etC2);

    bSignup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String username = etUN.getText().toString();
            String password = etPW.getText().toString();
            String password2 = etPW2.getText().toString();
            String name = etFN.getText().toString();
            String c1 = etC1.getText().toString();
            String c2 = etC2.getText().toString();

            // check if fields are vacant
            if (username.equals("") || password.equals("") || password2.equals("") || name.equals("")
                    || c1.equals("")|| c2.equals("")) {
                Toast.makeText(getApplicationContext(), "Incomplete Data", Toast.LENGTH_SHORT).show();
                return;
            }

            // check if passwords 1 and 2 match
            if (!password.equals(password2)) {
                Toast.makeText(getApplicationContext(), "Passwords don't match. Please try again.", Toast.LENGTH_LONG).show();
                return;
            }

            //check is username is still available for use
            if (loginDataBaseAdapter.isExist(username)){
                Toast.makeText(getApplicationContext(),"Username already taken. Please try again.", Toast.LENGTH_LONG).show();
                return;
            }

            else {
            // allow data to be saved in the database
                loginDataBaseAdapter.insertEntry(username, password, name, c1, c2);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        }
    });

    tvSign.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
        }
    });

}
}

我登录后,如何获取这些值(“即NAME,C1和C2”)并按下按钮发送短信?

更新

我在LoginDataBaseAdapter上使用过它。

public HashMap<String, String> getUserDetails(){
    HashMap <String,String> user = new HashMap <String,String> ();
    String selectQuery = "SELECT  * FROM " + "LOGIN";

    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if(cursor.getCount()>0){
        user.put("USERNAME", cursor.getString(1));
        user.put("PASSWORD", cursor.getString(2));
        user.put("NAME", cursor.getString(3));
        user.put("C1", cursor.getString(4));
        user.put("C2", cursor.getString(5));
    }
    cursor.close();
    db.close();
    // return user
    return user;
}

然后我的HomeActivity上的代码:

 tvHello = (TextView)findViewById(R.id.tvHello);
    HashMap <String, String> details = loginDataBaseAdapter.getUserDetails();
    String name_text = details.get("NAME");
    tvHello.setText("Welcome " + name_text);

它似乎只能获得当前用户的第一个条目而不是当前条目。有什么想法来解决这个问题?非常感谢你。

1 个答案:

答案 0 :(得分:0)

管理以使其正确。所以我会回答我自己的问题。

创建一个editText区域,您可以在其中输入要检索的名称。然后使用此代码检索它

public String getData(String verif) {
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{verif}, null, null, null);
    if(cursor.getCount()<1) {
        cursor.close();
        return "No records exist";
    }
    cursor.moveToFirst();
    String get_name = cursor.getString(cursor.getColumnIndex("NAME"));
    cursor.close();
    return get_name;
}

检索后,在TextView中设置名称。然后将其转换为字符串,如下所示:

String myName = myTextView.getText().toString();

然后:

smsManager.sendTextMessage(number, null, "My name is "+ myName ,null,   
null);

&#39;数&#39;是一个字符串,其中包含您要发送短信的联系号码