EditText结果基于Spinner选择Sqlite(双向选择)

时间:2018-03-18 14:04:57

标签: android android-edittext spinner android-spinner

更改微调器项目时,

EditText值未显示。

示例:编辑文本应显示" 9045"如果Spinner值是Boat,那么它应该反过来我的意思是如果它是Edit Text值是9045那么我的微调器值应该是Boat。

在许多方面尝试过,但它一直在失败。

主要活动代码是



package com.bar.example.myapplication;
public class MainActivity extends AppCompatActivity {
  public static final String BARCODE_KEY = "BARCODE";
  EditText date;
  DatePickerDialog datePickerDialog;
  Spinner s1, s2, s3;
  Button btnAdd;
  Button send;
  Button ok;
  Button ok1;
  private TextView result1;
  Button btn_send_mail;
  private Button button3;

  private Button btnexport;
  EditText inputLabel, bResult;
  TextView tex, tex1;
  DatabaseHandler dbhndlr;

  Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr;
  SimpleCursorAdapter sca, sca2, sca3, sca4;
  long spinner1_selected = 0;
  CheckBox ck1, ck2, ck3, ck4, ck5, ck6, ck7, ck8;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    s1 = (Spinner) findViewById(R.id.spinner1);
    s2 = (Spinner) findViewById(R.id.spinner2);
    s3 = (Spinner) findViewById(R.id.spinner5);
    bResult = (EditText) findViewById(R.id.barcodeResult);
    btnAdd = (Button) findViewById(R.id.btn_add);
    inputLabel = (EditText) findViewById(R.id.input_label);
    dbhndlr = new DatabaseHandler(this);

           if (DatabaseUtils.queryNumEntries(
        dbhndlr.getWritableDatabase(),
        DatabaseHandler.TABLE_LABELS) < 1) {
      dbhndlr.insertlabel("Ships", "12344", "9133");
      dbhndlr.insertlabel("Boat", "93993", "9045");
    }

    
    spinner1csr = dbhndlr.getAllLabelsAsCursor();
   
    sca = new SimpleCursorAdapter(this,
      android.R.layout.simple_list_item_1, spinner1csr,
      new String[] {
        DatabaseHandler.KEY_ID
      },
      new int[] {
        android.R.id.text1
      },
      0
    );

    s1.setAdapter(sca);
    // Set Spinner1 OnSelectedItemListener
    s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
        //  bResult.setText(s1.getSelectedItem().toString());
        spinner1_selected = id;
        spinner2csr = dbhndlr.getByRowid(spinner1_selected);
        spinner3csr = dbhndlr.getByRowid(spinner1_selected);
        spinner4csr = dbhndlr.getByRowid(spinner1_selected);

        sca2.swapCursor(spinner2csr);
        sca3.swapCursor(spinner3csr);
        sca4.swapCursor(spinner4csr);

      }

      @Override
      public void onNothingSelected(AdapterView << ? > parent) {}
    });


    spinner2csr = dbhndlr.getByRowid(spinner1_selected);
    sca2 = new SimpleCursorAdapter(this,
      android.R.layout.simple_list_item_1,
      spinner2csr,
      new String[] {
        DatabaseHandler.KEY_NAME
      },
      new int[] {
        android.R.id.text1
      },
      0
    );
    s2.setAdapter(sca2);
          spinner3csr = dbhndlr.getByRowid(spinner1_selected);
    sca3 = new SimpleCursorAdapter(this,
      android.R.layout.simple_list_item_1,
      spinner3csr,
      new String[] {
        DatabaseHandler.KEY_ID1
      },
      new int[] {
        android.R.id.text1
      },
      0
    );
    s3.setAdapter(sca3);

    spinner4csr = dbhndlr.getByRowid(spinner1_selected);
    sca4 = new SimpleCursorAdapter(this,
      android.R.layout.simple_list_item_1,
      spinner4csr,
      new String[] {
        DatabaseHandler.KEY_ID1
      },

      new int[] {
        android.R.id.text1
      },

      0

    );
    bResult.setAdapter(sca4);
         btnAdd.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View arg0) {
        String label = inputLabel.getText().toString();

        if (label.trim().length() > 0) {
                
          inputLabel.setText("");
          bResult.setText("");
        
          InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
         
          spinner1csr = dbhndlr.getAllLabelsAsCursor();
          spinner2csr = dbhndlr.getByRowid(spinner1_selected);
          spinner3csr = dbhndlr.getByRowid(spinner1_selected);
          spinner4csr = dbhndlr.getByRowid(spinner1_selected);
          sca.swapCursor(spinner1csr);
          sca2.swapCursor(spinner2csr);
          sca3.swapCursor(spinner3csr);
          sca4.swapCursor(spinner4csr);
        } else {
          Toast.makeText(getApplicationContext(), "Please enter label name",
            Toast.LENGTH_SHORT).show();
        }
      }
    });
  }

  @Override
  public void onDestroy() {
    spinner1csr.close();
    spinner2csr.close();
    spinner3csr.close();
    spinner4csr.close();
    super.onDestroy();
  }
}
&#13;
&#13;
&#13;

Databasehandler代码是。

&#13;
&#13;
package com.bar.example.myapplication;

public class DatabaseHandler extends SQLiteOpenHelper {
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "spinnerExample";

  public static final String TABLE_LABELS = "labels";

  public static final String KEY_ID = "id";
  public static final String KEY_NAME = "name";
  public static final String KEY_ID1 = "barcode";

  public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    // Category table create query
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "(" +
      KEY_ID + " TEXT," + KEY_NAME + " TEXT," + KEY_ID1 + " TEXT)";
    db.execSQL(CREATE_CATEGORIES_TABLE);
  }


  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

    onCreate(db);
  }

  public void insertlabel(String id, String label, String label1) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(KEY_ID, id);
    cv.put(KEY_NAME, label);
    cv.put(KEY_ID1, label1);
    db.insert(TABLE_LABELS, null, cv);
    db.close();
  }

  public List < String > getAllLabels() {
    List < String > labels = new ArrayList < String > ();


    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
      do {
        labels.add(cursor.getString(0));
      } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    return labels;
  }

  public Cursor getAllLabelsAsCursor() {
    String[] columns = new String[] {
      "rowid AS _id, *"
    };
    return this.getWritableDatabase().query(TABLE_LABELS, columns, null, null, null, null, null);
  }

  public Cursor getAllLabelsExceptedSelected(long selected) {
    String[] columns = new String[] {
      "rowid AS _id, *"
    };
    String whereclause = "rowid <> ?";
    String[] whereargs = new String[] {
      String.valueOf(selected)
    };
    return this.getWritableDatabase().query(TABLE_LABELS,
      columns,
      whereclause,
      whereargs,

      null,
      null,
      null

    );
  }

  public Cursor getByRowid(long id) {
    String[] columns = new String[] {
      "rowid AS _id, *"
    };
    return this.getWritableDatabase().query(
      TABLE_LABELS,
      columns,
      "rowid=?",
      new String[] {
        String.valueOf(id)
      },
      null, null, null
    );

  }

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

从我们的会员MikeT帖子中找到答案。

假设TextView是bresult,那么在onItemSelected方法中只需添加bresult.setText(spinner1csr.getString(spinner1csr.getColumnIndex(DatabaseHandler.KEY_NAME)));