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

时间:2017-07-13 09:18:20

标签: android sqlite android-spinner

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

  

cities表包含以下列:id,en_name,v_name,code。

     

places表包含以下列:reg_id,city_id,reg_name_v,reg_name_en。

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

  

如何从数据库中获取reg_id到按钮点击方法?

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) {

       }
   }

地方城市表格结构

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

1 个答案:

答案 0 :(得分:2)

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

中选择的字符串
    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();