从第二个微调器数据库中获取 ID

Get ID from second spinner database

我有两个微调器和一个按钮。 微调器从 sqlite 数据库中获取数据。 第一个包含城市名称,第二个包含城市中的地点。

The cities table contains those columns :id, en_name, v_name, code.

The places table contains those columns :reg_id, city_id, reg_name_v, reg_name_en.

我在第一个微调器中显示 v_name,在第二个微调器中显示 reg_name_v,但我需要获取 reg_id,以便在用户触摸按钮时显示地点商店。 因为我需要它来获得 json.

How to get the reg_id from the database to the button click method?


Shop.java片段

public class Shops extends Fragment implements AdapterView.OnItemSelectedListener {
               DatabaseHelper db;
               MaterialSpinner materialSpinner;
               MaterialSpinner materialSpinner2;
               String getPlacesURL = "http://*/get_places/";// + cityId
               String getShopsURL = "http://*/get_shops/"; // + cityId / + placeId
               String getShopsFullURL;
               String cityID;
           
               @Override
               public void onCreate(@Nullable Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
               }
           
               public boolean isNetworkConnectionAvailable() {
                   ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                   NetworkInfo info = cm.getActiveNetworkInfo();
                   if (info == null) return false;
                   NetworkInfo.State network = info.getState();
                   return (network == NetworkInfo.State.CONNECTED || network == NetworkInfo.State.CONNECTING);
               }
           
               @Override
               public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                   super.onActivityCreated(savedInstanceState);
               }
           
               @Override
               public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           
                   View myView = inflater.inflate(R.layout.shop_fragment, container, false);
        db = new DatabaseHelper(getContext());

       materialSpinner = (MaterialSpinner) myView.findViewById(R.id.shop_city_MS);
               materialSpinner2 = (MaterialSpinner) myView.findViewById(R.id.shop_place_MS);
               Button submitBtn = (Button) myView.findViewById(R.id.Shops_submit_btn);

               ArrayList citiesList = db.getRecords(DatabaseHelper.getTableCities(), DatabaseHelper.getCitiesColumnVName());
               ArrayAdapter a = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, citiesList);
               materialSpinner.setAdapter(a);
               materialSpinner.setOnItemSelectedListener(this);
       
               if (isNetworkConnectionAvailable()) {
                   ArrayList<String> citiesIDsList = db.getRecords(DatabaseHelper.getTableCities(), DatabaseHelper.getCitiesColumnId());
               } else {
                   //snackbar to check connection
               }
    @Override
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
           if (position == -1) {
               materialSpinner2.setClickable(false);
           } else {
               String databaseID = db.getValueFromColumn(position, DatabaseHelper.getTableCities(), DatabaseHelper.getCitiesColumnId());
               cityID = databaseID;
               ArrayList placesList = db.getListOfValuesFromColumn(DatabaseHelper.getTablePlaces(), DatabaseHelper.getPlacesCityId(), databaseID, DatabaseHelper.getPlacesRegNameV());
               if (placesList.size() == 0)
                   // empty list
               materialSpinner2.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, placesList));
               String merged = getShopsURL + databaseID;
               getShopsFullURL = merged + "/";
               materialSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                   @Override
                   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                       if (position == -1) {
                           //snackbar enter required place
                       } else {
                          // what to do here?
                       }
                   }
   
                   @Override
                   public void onNothingSelected(AdapterView<?> parent) {
   
                   }
               });
           }
       }
   
       @Override
       public void onNothingSelected(AdapterView<?> parent) {
   
       }
   }

Places Cities 表结构:

       public class DatabaseHelper extends SQLiteOpenHelper {
 private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "*.db";
    private static final String COLUMN_ID = "_id";
    private static final String TABLE_CITIES = "Cities";
        private static final String CITIES_COLUMN_ID = "id";
        private static final String CITIES_COLUMN_EN_NAME = "en_name";
        private static final String CITIES_COLUMN_AR_NAME = "ar_name";
        private static final String CITIES_COLUMN_CODE = "code";
    
            private static final String TABLE_PLACES = "Places";
            private static final String PLACES_REG_ID = "reg_id";
            private static final String PLACES_CITY_ID = "city_id";
            private static final String PLACES_REG_NAME_V = "reg_name_v";
            private static final String PLACES_REG_NAME_EN = "reg_name_en";
        
    String citiesQuery = "CREATE TABLE IF NOT EXISTS " + TABLE_CITIES + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
                CITIES_COLUMN_ID + " TEXT ," +
                CITIES_COLUMN_EN_NAME + " TEXT ," +
                CITIES_COLUMN_AR_NAME + " TEXT  ," +
                CITIES_COLUMN_CODE + " TEXT " +
                ");";
    
         String placesQuery = "CREATE TABLE IF NOT EXISTS " + TABLE_PLACES + "(" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
                    PLACES_REG_ID + " TEXT ," +
                    PLACES_CITY_ID + " TEXT  ," +
                    PLACES_REG_NAME_V + " TEXT ," +
                    PLACES_REG_NAME_EN + " TEXT " +
                    ");";
        
public static String getColumnId() {
        return COLUMN_ID;
    }
     public static String getTableCities() {
            return TABLE_CITIES;
        }
    
        public static String getCitiesColumnId() {
            return CITIES_COLUMN_ID;
        }
    
        public static String getCitiesColumnEnName() {
            return CITIES_COLUMN_EN_NAME;
        }
    
        public static String getCitiesColumnArName() {
            return CITIES_COLUMN_AR_NAME;
        }
    
        public static String getCitiesColumnCode() {
            return CITIES_COLUMN_CODE;
        }
    
        public static String getTablePlaces() {
                return TABLE_PLACES;
            }
        
            public static String getPlacesRegId() {
                return PLACES_REG_ID;
            }
        
            public static String getPlacesCityId() {
                return PLACES_CITY_ID;
            }
        
            public static String getPlacesRegNameAr() {
                return PLACES_REG_NAME_AR;
            }
        
            public static String getPlacesRegNameEn() {
                return PLACES_REG_NAME_EN;
            }
         public void onCreate(SQLiteDatabase db) {
    db.execSQL(citiesQuery);
        db.execSQL(placesQuery);
        }

要检索您的注册 ID,您需要一个简单的 查询,如下所示。假设 regName 是从 spinner

中选择的字符串
    String regName = "your selected spinner item";

    // The table to query
    String from = DatabaseHelper.getTablePlaces();

    // The columns to return
    String[] projection = {
            DatabaseHelper.getPlacesRegId()
    };

    // The columns for the WHERE clause 
    String selection = DatabaseHelper.getPlacesRegNameAr() + " = ?";  //or .getPlacesRegNameEn() ?

    // The values for the WHERE clause
    String[] selectionArgs = { regName };

    Cursor cursor = db.getReadableDatabase().query(
            from,          
            projection,                               
            selection,                                
            selectionArgs,                            
            null,                                    
            null,                                    
            null                                     
    );

    while(cursor.moveToNext()){
        // Get ID
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseHelper.getPlacesRegId()));
    }

    cursor.close();