Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio从sqlite获取数据_Android_Sqlite - Fatal编程技术网

Android Studio从sqlite获取数据

Android Studio从sqlite获取数据,android,sqlite,Android,Sqlite,我在从sqlite不同的数据库获取数据时遇到了一些问题。一个保存用户名和电话的数据库,他们可以根据自己的喜好进行更新。另一个是房屋信息,但也包含用户名和电话。 我的问题是,我刚刚意识到,在用户数据库中更新的手机与之前存储在内部数据库中的手机不同。所以我想从用户数据库中调用用户的电话,并用于电话呼叫功能和消息功能。但它不起作用。我试图将用户数据库中的全名与房屋数据库中的全名进行匹配,以调用电话数据,但仍然不能 这是我的通话和留言功能 public class HouseDetail extends

我在从sqlite不同的数据库获取数据时遇到了一些问题。一个保存用户名和电话的数据库,他们可以根据自己的喜好进行更新。另一个是房屋信息,但也包含用户名和电话。 我的问题是,我刚刚意识到,在用户数据库中更新的手机与之前存储在内部数据库中的手机不同。所以我想从用户数据库中调用用户的电话,并用于电话呼叫功能和消息功能。但它不起作用。我试图将用户数据库中的全名与房屋数据库中的全名进行匹配,以调用电话数据,但仍然不能

这是我的通话和留言功能

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;
}
替换

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

通过
query
获取的数据可能没有按您想要的顺序排列,也就是说,结果可能不在“全名,电话”中,因此您应该使用
getColumnIndex()
获取每列的数据。

尝试以下操作:

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

插入
if(a.equals(fullname))
条件,该条件位于
getNumber()
方法使用
if(a.equalsIgnoreCase(fullname))
方法使用
getColumnIndex()
参数,在这种情况下该参数为
fullname“
。参数是列字符串的名称,我想您希望将数据库中的fullname设置为a,因此它是“
fullname
。我不确定您的意思,但我正在谈论第二个代码块中的第一行。调用那里的
getColumnIndex()
没有参数。
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;
}