I had added a Spinner in my application. Now I want to add a "recent searches" button on clicking upon which it will display the previous choices made by user from Spinner.I had searched on many websites including android developers,stack overflow and many more but what only I found was saving history for search widget. How can I do this? These are the links I searched through.I dont know how to apply it to Spinners.
How to show search view recent search history
Create "history" to SearchView on ActionBar
This was my main activity class
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView bus_selection, bus_selection2;
Button bus_go;
String bus_stationf, bus_stationt;
int bus_posf, bus_post;
TextView selection, selection2;
Button ac_go;
String stationf, stationt;
int posf, post;
DatePicker datePicker,datePicker1;
String stand[] = {"Durg Railway Station", "Durg Bus Station", "power house", "Tatibandh", "Pachdedinaka", "Telibandha", "Raipur Airport"};
String stand1[] = {"Durg Railway Station", "Durg Bus Station", "power house", "Tatibandh", "Pachdedinaka", "Telibandha", "Raipur Airport"};
@TargetApi(11)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
datePicker=(DatePicker)findViewById(R.id.datePicker1);
datePicker1=(DatePicker)findViewById(R.id.datePicker2);
datePicker.setSpinnersShown(false);
datePicker1.setSpinnersShown(false);
TabHost th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabHost.TabSpec spec = th.newTabSpec("tag2");
spec.setContent(R.id.tab1);
spec.setIndicator(" AC bus ");
th.addTab(spec);
spec = th.newTabSpec("tag1");
spec.setContent(R.id.tab2);
spec.setIndicator("City bus ");
th.addTab(spec);
bus_selection = (TextView) findViewById(R.id.tv1);
Spinner bus_spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> cc = new ArrayAdapter<String>(this, R.layout.row,R.id.station,stand);
bus_spin.setAdapter(cc);
bus_spin.setOnItemSelectedListener(new busFromSpinner());
bus_selection2 = (TextView) findViewById(R.id.tv2);
Spinner bus_spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> dd = new ArrayAdapter<String>(this, R.layout.row,R.id.station,stand1);
bus_spin2.setAdapter(dd);
bus_spin2.setOnItemSelectedListener(new busToSpinner());
bus_go = (Button) findViewById(R.id.b1);
bus_go.setOnClickListener(this);
selection = (TextView) findViewById(R.id.tv3);
Spinner spin = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row,R.id.station,stand);
spin.setAdapter(aa);
spin.setOnItemSelectedListener(new ac_busFromSpinner());
selection2 = (TextView) findViewById(R.id.tv4);
Spinner spin2 = (Spinner) findViewById(R.id.spinner4);
ArrayAdapter<String> bb = new ArrayAdapter<String>(this, R.layout.row,R.id.station,stand1);
spin2.setAdapter(bb);
spin2.setOnItemSelectedListener(new ac_busToSpinner());
ac_go = (Button) findViewById(R.id.b2);
ac_go.setOnClickListener(this);
}
class busFromSpinner implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(v.getContext(), "You choose :" + stand[position], Toast.LENGTH_SHORT).show();
bus_stationf = stand[position];
bus_posf = position;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
class busToSpinner implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(v.getContext(), "You choose :" + stand1[position], Toast.LENGTH_SHORT).show();
bus_stationt = stand1[position];
bus_post = position;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
class ac_busFromSpinner implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(v.getContext(), "You choose :" + stand[position], Toast.LENGTH_SHORT).show();
stationf = stand[position];
posf = position;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
class ac_busToSpinner implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(v.getContext(), "You choose :" + stand1[position], Toast.LENGTH_SHORT).show();
stationt = stand1[position];
post = position;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.b1:
Intent busintent = new Intent(MainActivity.this, bus.class);
busintent.putExtra("busposf", bus_posf);
busintent.putExtra("buspost", bus_post);
busintent.putExtra("busstationf", bus_stationf);
busintent.putExtra("busstationt", bus_stationt);
startActivity(busintent);
break;
case R.id.b2:
Intent acbusintent = new Intent(MainActivity.this, acBus.class);
acbusintent.putExtra("acbusposf", posf);
acbusintent.putExtra("acbuspost", post);
acbusintent.putExtra("acbusstationf", stationf);
acbusintent.putExtra("acbusstationt", stationt);
startActivity(acbusintent);
break;
}
}
}
and this is the layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabhost"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@android:id/tabs"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv1"
android:text="From:"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/spinner1"
android:layout_below="@+id/tv1"/>
<TextView
android:id="@+id/tv2"
android:text="To:"
android:layout_below="@id/spinner1"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/spinner2"
android:layout_below="@id/tv2" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_below="@+id/spinner2"
android:datePickerMode="calendar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/datePicker1"
android:paddingLeft="140dp"
android:id="@+id/b1"
android:text="Search"
android:layout_gravity="center"
android:drawableLeft="@drawable/bus_icon"
android:gravity="center_vertical"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv3"
android:text="From:"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/spinner3"
android:layout_below="@+id/tv3"/>
<TextView
android:id="@+id/tv4"
android:text="To:"
android:layout_below="@id/spinner3"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/spinner4"
android:layout_below="@id/tv4" />
<DatePicker
android:id="@+id/datePicker2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_below="@+id/spinner4"
android:datePickerMode="calendar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp" />
<Button
android:text="Search"
android:id="@+id/b2"
android:layout_below="@id/datePicker2"
android:layout_gravity="center"
android:drawableLeft="@drawable/bus_icon"
android:paddingLeft="140dp"
android:gravity="left|center_vertical"
android:layout_width="match_parent"
android:layout_height="50dp" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
now i want to add a button above search button which should display recent choices made from spinners.