如何使用多个搜索项和一个按钮在sqLiteDatabase中搜索数据?

时间:2018-01-28 12:49:06

标签: android string cursor android-sqlite

我已经在Android Studio中安装了一个应用程序,但我想更新它。更新将搜索两种不同类型的数据。

该应用程序将让用户通过其车牌和/或底盘编号添加汽车,该车将被放置到停车场。下一步是找回汽车,为此用户可以(现在)仅通过车牌搜索汽车。我想改变它,以便用户可以通过它的车牌或底盘号搜索汽车。请参阅下面的代码,了解我已有的内容。

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SearchCarData extends AppCompatActivity {

TextView DisplayParkinglot, DisplayLicensePlate, DisplayChassisNumber;
EditText SearchLicensePlateTxt;
MyDBHelper myDBHelper;
SQLiteDatabase sqLiteDatabase;
String license_plate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_car_data_layout);
    SearchLicensePlateTxt = (EditText) 
findViewById(R.id.searchLicensePlateTxt);
    DisplayParkinglot = (TextView) findViewById(R.id.displayParkingLot);
    DisplayLicensePlate = (TextView) findViewById(R.id.displayLicencePlate);
    DisplayChassisNumber = (TextView) 
findViewById(R.id.displayChassisNumber);

    DisplayParkinglot.setVisibility(View.GONE);
    DisplayLicensePlate.setVisibility(View.GONE);
    DisplayChassisNumber.setVisibility(View.GONE);
}

public void searchCar(View view) {
    license_plate = SearchLicensePlateTxt.getText().toString();
    myDBHelper = new MyDBHelper(getApplicationContext());
    sqLiteDatabase = myDBHelper.getReadableDatabase();
    Cursor cursor = myDBHelper.getCarInfo(license_plate, sqLiteDatabase);
    if(cursor.moveToFirst()) {
        String PARKINGLOT = cursor.getString(0);
        String LICENSEPLATE = cursor.getString(1);
        String CHASSISNUMBER = cursor.getString(2);

        DisplayParkinglot.setText(PARKINGLOT);
        DisplayLicensePlate.setText(LICENSEPLATE);
        DisplayChassisNumber.setText(CHASSISNUMBER);

        DisplayParkinglot.setVisibility(View.VISIBLE);
        DisplayLicensePlate.setVisibility(View.VISIBLE);
        DisplayChassisNumber.setVisibility(View.VISIBLE);
    }
}

所以我想更改代码,以便可以搜索车牌号码或底盘号码。有人提示或想法我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

从db中查询数据包括两个字段。如果您使用内容提供程序访问数据库,那么您的代码可能看起来像这样。

Cursor cursor = getContext().getContentResolver().query(CONTENT_URI, projection, selection, selectionargs, sortOrder);

CONTENT_URI 中指向您的表格,投影定义您的表格列,选择 - 您拥有数据的列, selectionargs - 数据

希望它可以帮到你

答案 1 :(得分:0)

我已经解决了,我已经尝试添加一个新的光标并且它有效。

public class SearchCarData extends AppCompatActivity {

TextView DisplayParkinglot, DisplayLicensePlate, DisplayChassisNumber;
EditText SearchTxt;
MyDBHelper myDBHelper;
SQLiteDatabase sqLiteDatabase;
String license_plate, chassis_number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_car_data_layout);
    SearchTxt = (EditText) findViewById(R.id.searchTxt);
    DisplayParkinglot = (TextView) findViewById(R.id.displayParkingLot);
    DisplayLicensePlate = (TextView) findViewById(R.id.displayLicencePlate);
    DisplayChassisNumber = (TextView) findViewById(R.id.displayChassisNumber);

    DisplayParkinglot.setVisibility(View.GONE);
    DisplayLicensePlate.setVisibility(View.GONE);
    DisplayChassisNumber.setVisibility(View.GONE);
}

public void searchCar(View view) {
    license_plate = SearchTxt.getText().toString();
    chassis_number = SearchTxt.getText().toString();
    myDBHelper = new MyDBHelper(getApplicationContext());
    sqLiteDatabase = myDBHelper.getReadableDatabase();
    Cursor cursor = myDBHelper.getCarInfo(license_plate, sqLiteDatabase);
    Cursor cursor1 = myDBHelper.getCarInfo2(chassis_number, sqLiteDatabase);
    //Search by License Plate
    if(cursor.moveToFirst()) {
        String PARKINGLOT = cursor.getString(0);
        String LICENSEPLATE = cursor.getString(1);
        String CHASSISNUMBER = cursor.getString(2);

        DisplayParkinglot.setText(PARKINGLOT);
        DisplayLicensePlate.setText(LICENSEPLATE);
        DisplayChassisNumber.setText(CHASSISNUMBER);

        DisplayParkinglot.setVisibility(View.VISIBLE);
        DisplayLicensePlate.setVisibility(View.VISIBLE);
        DisplayChassisNumber.setVisibility(View.VISIBLE);

    }
    //Search by Chassis Number
     else if (cursor1.moveToFirst()) {
         String PARKINGLOT = cursor1.getString(0);
         String LICENSEPLATE = cursor1.getString(1);
         String CHASSISNUMBER = cursor1.getString(2);

         DisplayParkinglot.setText(PARKINGLOT);
         DisplayLicensePlate.setText(LICENSEPLATE);
         DisplayChassisNumber.setText(CHASSISNUMBER);

         DisplayParkinglot.setVisibility(View.VISIBLE);
         DisplayLicensePlate.setVisibility(View.VISIBLE);
         DisplayChassisNumber.setVisibility(View.VISIBLE);
     }
}

谢谢Suja的帮助,这有助于我更好地搜索。