Android Studio从sqlite获取数据

时间:2016-01-31 12:11:37

标签: android sqlite

我有一些关于从sqlite不同数据库获取数据的问题。一个保存用户名和手机的数据库,他们可以随意更新。另一个是房屋信息,但也包含用户的姓名和电话。 我的问题是我只是意识到用户数据库中更新的手机与之前在家数据库中存储的手机不一样。所以我想从用户数据库中拨打用户的电话,用于电话呼叫功能和消息功能。但它不起作用。我试图将用户数据库中的全名与house数据库匹配,以便调用手机数据,但仍然无法使用。

这是我的电话和留言功能

public class HouseDetail extends AppCompatActivity {
EditText etAddress,etDetail,etPhone;
TextView txType,txProperty,txPrice,txState,txTitle,txOther,txSize,txFullname;
ImageButton bPhone,bMessage;
String type,property,price,state,address,title,other,size,detail,fullnames,fullname,phone;
HousesDB db = new HousesDB(this);
Houses houses;
DatabaseOperations Udb = new DatabaseOperations(this);
PersonalData profileInfo;

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

    txType = (TextView) findViewById(R.id.txTypes);
    txProperty = (TextView) findViewById(R.id.txProperty);
    txPrice = (TextView) findViewById(R.id.txPrice);
    txState = (TextView) findViewById(R.id.txState);
    etAddress = (EditText) findViewById(R.id.etAddress);
    txTitle = (TextView) findViewById(R.id.txTitle);
    txOther = (TextView) findViewById(R.id.txOther);
    txSize = (TextView) findViewById(R.id.txSize);
    txFullname = (TextView) findViewById(R.id.txFullname);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etDetail = (EditText) findViewById(R.id.etDetail);
    bPhone = (ImageButton) findViewById(R.id.bPhone);
    bMessage = (ImageButton) findViewById(R.id.bMessage);

    fullnames = txFullname.getText().toString();
    type = txType.getText().toString();
    property = txProperty.getText().toString();
    price = txPrice.getText().toString();
    state = txState.getText().toString();
    address = etAddress.getText().toString();
    title = txTitle.getText().toString();
    other = txOther.getText().toString();
    size = txSize.getText().toString();
    detail = etDetail.getText().toString();
    phone = etPhone.getText().toString();


    Intent i = getIntent();
    property = i.getStringExtra("house_property");
    txProperty.setText(property);

    houses = db.getInfo(property);

    txType.setText(houses.getTypes());
    txFullname.setText(houses.getFullname());
    txPrice.setText(houses.getPrice());
    txState.setText(houses.getState());
    etAddress.setText(houses.getAddress());
    txTitle.setText(houses.getTitle());
    txOther.setText(houses.getOther());
    txSize.setText(houses.getSize());
    etDetail.setText(houses.getDetail());


    this.fullnames = fullname;
    profileInfo = Udb.getNumber(fullname);
    etPhone.setText(profileInfo.get_phone());


    bPhone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent callIntent =new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + etPhone));
            startActivity(callIntent);

        }
    });

    bMessage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + etPhone)));
        }
    });
}

以下是我的用户数据库,其中包含电话号码

  public PersonalData getNumber(String fullname)
{
    SQLiteDatabase SQ = this.getWritableDatabase();
    String query = "select FullName,Phone from "+ Table_NAME;
    Cursor CR = SQ.rawQuery(query,null);
    String a;
    PersonalData info = new PersonalData();

    if (CR.moveToFirst())
    {
        do {
            a = CR.getString(0);
            if (a.equals(fullname)) {
                info._phone = CR.getString(1);
                break;
            }
        }while (CR.moveToNext());
    }
    return info;
}

2 个答案:

答案 0 :(得分:0)

替换

        a = CR.getString(0);
        if (a.equals(fullname)) {
            info._phone = CR.getString(1);
            break;
        }

        a = CR.getString(CR.getColumnIndex("Fullname"));
        if (a.equals("FullName")) {
            info._phone = CR.getString(CR.getColumnIndex("Phone"));
            break;
        }

query获取的数据可能不是您想要的,在您的情况下,结果可能不在“Fullname,Phone”中,因此您应该使用getColumnIndex()来获取数据每一栏。

答案 1 :(得分:0)

试试这个:

public PersonalData getNumber(String fullName) {
    SQLiteDatabase database = this.getReadableDatabase();
    String table = "YOUR TABLE NAME";
    String[] columns = {
        "FullName" ,
        "Phone"
    };
    Cursor cursor = database.query(table , columns , null , null , null , null , null);
    cursor.moveToPosition(-1);
    PersonalData info = new PersonalData();
    while (cursor.moveToNext()) {
        String name = cursor.getString(0);
        if (name.equals(fullName)) {
            info._phone = cursor.getString(1);
            break;
        }
    }
    cursor.close();
    return info;
}