I am trying to send a string accessed from a spinner, to a data helper class to run a query and insert the value in another spinner which is running on another activity.
NewTask.java:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// On selecting a spinner item
label = spinnerComp.getSelectedItem().toString(); //storing value of spinner in string label
System.out.println("NewTask label="+label);
toRetDeptString(); //it will call return string method
// Showing selected spinner item
Toast.makeText(spinnerComp.getContext(), "You selected: " + label, Toast.LENGTH_LONG).show();
}
The return string method is also declared in NewTask.java:
public String toRetDeptString() {
System.out.println("NewTask String label="+label);
return label;
}
I am trying to access the value of a label string dataHelper class which runs the query on the basis of the value in the string label.
DataHelper.java in DatabaseHelper class:
public List<String> getAllLabelsDept(){
NewTask a = new NewTask();
String n1= a.toRetDeptString(); //this one doesn't worked
// String n1 = a.label; //this one doesn't worked
// String n1="umbrella"; //this method worked by directly assiging string value
System.out.println("a="+n1);
List<String> labels = new ArrayList<String>();
// Query
String selectQuery = "select " + Dept + " from "+TABLE_NAME +" where " + Comp + " = '" + n1 +"'";
SQLiteDatabase db1 = this.getReadableDatabase();
Cursor cursor = db1.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(cursor.getColumnIndex(Dept)));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db1.close();
// returning labels
return labels;
}
Select_Dept.java:
public class Select_Dept extends Activity implements OnItemSelectedListener{
Spinner spinnerDept;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select__dept);
spinnerDept = (Spinner) findViewById(R.id.spinnerDept);
spinnerDept.setOnItemSelectedListener(this);
loadSpinnerDataDept();
}
private void loadSpinnerDataDept() {
// database handler
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllLabelsDept();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerDept.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// On selecting a spinner item
String label = spinnerDept.getSelectedItem().toString();
// Showing selected spinner item
Toast.makeText(spinnerDept.getContext(), "You selected: " + label, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.select__dept, menu);
return true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Logcat:
- 06-05 12:30:13.369: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.EzyCloud.ezytask/.MainActivity }
- 06-05 12:30:14.160: I/ActivityManager(59): Displayed activity com.EzyCloud.ezytask/.MainActivity: 562 ms (total 562 ms)
- 06-05 12:30:16.139: I/ActivityManager(59): Starting activity: Intent { cmp=com.EzyCloud.ezytask/.NewTask }
- 06-05 12:30:16.299: I/System.out(280): NewTask label=umbrella
- 06-05 12:30:16.299: I/System.out(280): NewTask String label=umbrella
- 06-05 12:30:16.559: I/ActivityManager(59): Displayed activity com.EzyCloud.ezytask/.NewTask: 406 ms (total 406 ms)
- 06-05 12:30:19.140: I/ActivityManager(59): Starting activity: Intent { cmp=com.EzyCloud.ezytask/.Select_Dept }
- 06-05 12:30:19.339: I/System.out(280): NewTask String label= //no value accesed
- 06-05 12:30:19.339: I/System.out(280): a= **//no value accesed**
- 06-05 12:30:19.699: I/ActivityManager(59): Displayed activity - com.EzyCloud.ezytask/.Select_Dept: 508 ms (total 508 ms)
- 06-05 12:30:24.849: D/dalvikvm(128): GC_EXPLICIT freed 202 objects / 9800 bytes in 145ms
But the string is not accessed.
First, when you get
System.out(280): NewTask String label= //no value accesed
the NewTask instance didn't in stack top.
Second, the variable label is a private variable, so it is possible to not exist.
If you want to get label, you could use static. Example: public static label;, but I don't suggest for you to do like this, because this is dangerous.
Finally, you could build a customizable Application class and private String label; to record the label (overall) or pass the label through intent.