I would like to change my spinner value based on textview change. Spinner value should data fetch from database against textview change. At the moment there is no change if i select textview.
Based on below code spinner is not changing if textview is changing. Googled but unsuccessful result. Could you assist.
MainActivity.java
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, s6;
TextView tex, tex1, bResult;
DatabaseHandler dbhndlr;
Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr, search;
SimpleCursorAdapter sca, sca2, sca3, sca4, sca6;
long spinner1_selected = 0;
CheckBox ck1, ck2, ck3, ck4, ck5, ck6, ck7, ck8;
private SimpleDateFormat mSimpleDateFormat;
private PeriodFormatter mPeriodFormat;
private Date startDate;
private Date endDate;
private Date birthDate;
private TextView tvStartDate, tvEndDate, tvDifferenceStandard, tvDifferenceCustom, tvBirthDate, tvAgeStandard, tvAgeCustom;
@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);
final TextView bResult = (TextView) findViewById(R.id.barcodeResult);
final EditText s6 = (EditText) findViewById(R.id.editText5);
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);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
spinner1_selected = id;
spinner2csr = dbhndlr.getByRowid(spinner1_selected);
spinner3csr = dbhndlr.getByRowid(spinner1_selected);
spinner4csr = dbhndlr.getByRowid(spinner1_selected);
bResult.setText(spinner1csr.getString(spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID1)));
sca2.swapCursor(spinner2csr);
sca3.swapCursor(spinner3csr);
sca4.swapCursor(spinner4csr);
}
@Override
public void onNothingSelected(AdapterView << ? > parent) {}
});
s6.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
{
}
}
@Override
public void afterTextChanged(Editable s) {
try {
int nPosition = Integer.parseInt(s.toString());
if (nPosition >= 1 && nPosition <= 12)
s1.setSelection(nPosition - 1);
} catch (NumberFormatException nfe) {}
}
});
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
);
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();
}
}
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"; //<<<< Made public
public static final String KEY_ID = "id"; //<<<< Made public
public static final String KEY_NAME = "name";
public static final String KEY_ID1 = "barcode"; //<<<< made public
public static final String KEY_ID2 = "barcode1";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "(" +
KEY_ID + " TEXT," + KEY_NAME + " TEXT," + KEY_ID1 + " TEXT," + KEY_ID2 + " 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());
}
cursor.close();
db.close();
return labels;
}
public Cursor getAllLabelsAsCursor() {
String[] columns = new String[] {
"rowid AS _id, *"
}; // Need _id column for SimpleCursorAdapter
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
);
}
}
I have a got workaround for the same after spending more time in googling:)
But this answer will insert the edittext value in to spinner upon clicking the button.
But anyone knows how to crack the same with textwatcher. Like spinner should fed the value from edittext without clicking the button??
public class MainActivity extends Activity {
List < String > li;
Spinner sp1, sp2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li = new ArrayList < String > ();
li.add("Data 1");
li.add("Data 2");
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
Button b = (Button) findViewById(R.id.button1);
final EditText et = (EditText) findViewById(R.id.editText1);
call();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
call();
}
});
}
public void call() {
// TODO Auto-generated method stub
ArrayAdapter < String > adp = new ArrayAdapter < String > (this,
android.R.layout.simple_dropdown_item_1line, li);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp);
sp2.setAdapter(adp);
sp1.setSelection((li.size() - 1));
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView << ? > arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
sp2.setSelection(arg2);
}
@Override
public void onNothingSelected(AdapterView << ? > arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="34dp" android:layout_marginTop="74dp" />
<Spinner android:id="@+id/spinner2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="74dp" android:layout_marginRight="34dp"
/>
<EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="19dp" android:ems="10" android:inputType="textPersonName"
/>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/editText1" android:layout_alignParentRight="true" android:text="Button" />
</RelativeLayout>