我想根据Spinner
这是我的MainActviity
:
package com.maasin.map.ret1;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.maasin.map.ret1.m_FireBase.FirebaseHelper;
import com.maasin.map.ret1.m_Model.Spacecraft;
import com.maasin.map.ret1.m_Model.*;
import com.maasin.map.ret1.m_UI.MyAdapter;
import java.util.ArrayList;
import static com.maasin.map.ret1.R.layout.map;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinnerDropdown;
// DatabaseReference db;
Boolean saved = null;
ArrayList < Spacecraft > spacecrafts = new ArrayList < > ();
// private FirebaseDatabase database;
DatabaseReference db;
FirebaseHelper helper;
MyAdapter adapter;
RecyclerView rv;
EditText nameEditTxt, propTxt, descTxt;
String filter = "";
String route = "";
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter spinneradapter = ArrayAdapter.createFromResource(this, R.array.spinner_options, android.R.layout.simple_spinner_item);
spinnerDropdown = (Spinner) findViewById(R.id.spinner);
spinnerDropdown.setAdapter(spinneradapter);
spinnerDropdown.setOnItemSelectedListener(MainActivity.this);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(" The MasTrans APP");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String size = spinner.getSelectedItem().toString();
//SETUP RECYCLER
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//INITIALIZE FIREBASE DB
db = FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(db);
//ADAPTER
adapter = new MyAdapter(this, helper.retrieve());
rv.setAdapter(adapter);
Button fab = (Button) findViewById(R.id.btnSearch);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter = new MyAdapter(MainActivity.this, retrieve());
rv.setAdapter(adapter);
}
;
});
helper.s(size);
}
public Boolean save(Spacecraft spacecraft) {
if (spacecraft == null) {
saved = false;
} else {
try {
db.child("Spacecraft").push().setValue(spacecraft);
saved = true;
} catch (DatabaseException e) {
e.printStackTrace();
saved = false;
}
}
return saved;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()) {
Spacecraft spacecraft = ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
}
public ArrayList < Spacecraft > retrieve() {
database = FirebaseDatabase.getInstance();
final DatabaseReference establishments = database.getReference("vehicles");
establishments.orderByChild(route).equalTo(filter).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
fetchData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
@Override
public void onItemSelected(AdapterView < ? > parent, View view, int position, long id) {
TextView spinnerDialogText = (TextView) view;
filter = spinnerDialogText.getText().toString();
Intent i = new Intent(MainActivity.this, MapsActivity.class);
i.putExtra("filter", filter);
if (filter.equals("Bato")) {
route = "Route001";
} else if (filter.equals("Hilongos")) {
route = "Route002";
} else if (filter.equals("Baybay")) {
route = "Route003";
} else if (filter.equals("Ormoc")) {
route = "Route004";
} else if (filter.equals("Mahaplag")) {
route = "Route005";
} else if (filter.equals("Abuyog")) {
route = "Route006";
} else if (filter.equals("Palo")) {
route = "Route007";
} else if (filter.equals("Tacloban")) {
route = "Route008";
} else if (filter.equals("Macrohon")) {
route = "Route009";
} else if (filter.equals("Padre Burgos")) {
route = "Route010";
} else if (filter.equals("Malitbog")) {
route = "Route011";
} else if (filter.equals("Sogod")) {
route = "Route012";
} else if (filter.equals("Himay-angan")) {
route = "Route13";
} else if (filter.equals("Liloan")) {
route = "Route014";
} else if (filter.equals("San Ricardo")) {
route = "Route015";
} else if (filter.equals("Benit")) {
route = "Route016";
} else if (filter.equals("Saint Bernard")) {
route = "Route017";
} else if (filter.equals("San Juan")) {
route = "Route018";
} else if (filter.equals("Hinunangan")) {
route = "Route019";
} else if (filter.equals("Silago")) {
route = "Route020";
}
}
@Override
public void onNothingSelected(AdapterView < ? > parent) {
}
}
以下是MapsActivity
的代码:
package com.maasin.map.ret1;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String routes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = this.getIntent();
routes = i.getStringExtra("filter");
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng Maasin = new LatLng(10.132188, 124.834808);
mMap.addMarker(new MarkerOptions().position(Maasin).title("Maasin City Bus Terminal"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Maasin));
LatLng Bato = new LatLng(10.340038, 124.849840);
if (routes.equals("Bato")) {
mMap.addMarker(new MarkerOptions().position(Bato).title("Bato"));
}
}
}