【发布时间】:2018-06-11 20:52:24
【问题描述】:
我已将纬度和经度值存储在 Firebase 中,并通过搜索功能在回收视图列表中检索。我希望它根据每个文本视图的纬度和经度值在每个项目上单击按钮地图时显示谷歌地图中的位置。如何将值传递给地图活动以在谷歌地图上显示位置标记。
SearchActivity.java
公共类 SearchActivity 扩展 AppCompatActivity 实现 View.OnClickListener {
EditText EditTextZone;
RecyclerView recyclerView;
DatabaseReference databaseReference;
FirebaseUser firebaseUser;
ArrayList<String> studentNameList;
ArrayList<String> studentMatrikList;
ArrayList<String> studentPhoneList;
ArrayList<String> studentAddressList;
ArrayList<String> studentLatitudeList;
ArrayList<String> studentLongitudeList;
SearchAdapter searchAdapter;
Button buttonCancel;
Button buttonGo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
EditTextZone = (EditText) findViewById(R.id.EditTextZone);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
buttonCancel = (Button) findViewById(R.id.buttonCancel);
buttonGo = (Button) findViewById(R.id.buttonGo);
buttonCancel.setOnClickListener(this);
buttonGo.setOnClickListener(this);
databaseReference = FirebaseDatabase.getInstance().getReference();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
studentNameList = new ArrayList<>();
studentMatrikList = new ArrayList<>();
studentPhoneList = new ArrayList<>();
studentAddressList = new ArrayList<>();
studentLatitudeList = new ArrayList<>();
studentLongitudeList = new ArrayList<>();
EditTextZone.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) {
if(!s.toString().isEmpty()){
setAdapter(s.toString());
} else {
studentNameList.clear();
studentMatrikList.clear();
studentPhoneList.clear();
studentAddressList.clear();
studentLongitudeList.clear();
studentLatitudeList.clear();
recyclerView.removeAllViews();
}
}
});
}
private void setAdapter(final String searchedString) {
databaseReference.child("student").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
studentNameList.clear();
studentMatrikList.clear();
studentPhoneList.clear();
studentAddressList.clear();
studentLongitudeList.clear();
studentLatitudeList.clear();
recyclerView.removeAllViews();
int counter = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String id = snapshot.getKey();
String studentName = snapshot.child("studentName").getValue(String.class);
String studentMatrik = snapshot.child("studentMatrik").getValue(String.class);
String studentPhone = snapshot.child("studentPhone").getValue(String.class);
String studentAddress = snapshot.child("studentAddress").getValue(String.class);
String studentLongitude = snapshot.child("studentLongitude").getValue(String.class);
String studentLatitude = snapshot.child("studentLatitude").getValue(String.class);
if (studentAddress.toLowerCase().contains(searchedString.toLowerCase())) {
studentNameList.add(studentName);
studentMatrikList.add(studentMatrik);
studentPhoneList.add(studentPhone);
studentAddressList.add(studentAddress);
studentLatitudeList.add(studentLatitude);
studentLongitudeList.add(studentLongitude)
counter++;
} else if (studentName.toLowerCase().contains(searchedString.toLowerCase())) {
studentNameList.add(studentName);
studentMatrikList.add(studentMatrik);
studentPhoneList.add(studentPhone);
studentAddressList.add(studentAddress);
studentLatitudeList.add(studentLatitude);
studentLongitudeList.add(studentLongitude);
counter++;
}
if (counter == 15)
break;
}
searchAdapter = new SearchAdapter(SearchActivity.this, studentNameList, studentMatrikList, studentPhoneList, studentAddressList, studentLongitudeList, studentLatitudeList);
recyclerView.setAdapter(searchAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onClick(View v) {
if (v == buttonCancel)
{
finish();
startActivity(new Intent(this, UserAreaActivity.class));
}
if (v == buttonGo)
{
startActivity(new Intent(this, MapsActivity.class));
}
}
}
SearchAdapter.java
公共类 SearchAdapter 扩展 RecyclerView.Adapter {
Context context;
ArrayList<String> studentNameList;
ArrayList<String> studentMatrikList;
ArrayList<String> studentPhoneList;
ArrayList<String> studentAddressList;
ArrayList<String> studentLatitudeList;
ArrayList<String> studentLongitudeList;
class SearchViewHolder extends RecyclerView.ViewHolder{
TextView studentName, studentMatrik, studentPhone, studentAddress, studentLongitude, studentLatitude;
CheckBox checkBox;
Button buttonMap;
public SearchViewHolder(View itemView) {
super(itemView);
studentName = (TextView) itemView.findViewById(R.id.studentName);
studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
studentLongitude = (TextView) itemView.findViewById(R.id.studentLongitude);
studentLatitude = (TextView) itemView.findViewById(R.id.studentLatitude);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
Toast.makeText(SearchAdapter.this.context,
"Selected student is " + studentName.getText(),Toast.LENGTH_LONG).show();
} else {
}
}
});
}
}
public SearchAdapter(Context context, ArrayList<String> studentNameList, ArrayList<String> studentMatrikList, ArrayList<String> studentPhoneList, ArrayList<String> studentAddressList, ArrayList<String> studentLongitudeList, ArrayList<String> studentLatitudeList) {
this.context = context;
this.studentNameList = studentNameList;
this.studentMatrikList = studentMatrikList;
this.studentPhoneList = studentPhoneList;
this.studentAddressList = studentAddressList;
this.studentLongitudeList = studentLongitudeList;
this.studentLatitudeList = studentLatitudeList;
}
@Override
public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
return new SearchAdapter.SearchViewHolder(view);
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
holder.studentName.setText(studentNameList.get(position));
holder.studentMatrik.setText(studentMatrikList.get(position));
holder.studentPhone.setText(studentPhoneList.get(position));
holder.studentAddress.setText(studentAddressList.get(position));
holder.studentLatitude.setText(studentLatitudeList.get(position));
holder.studentLongitude.setText(studentLongitudeList.get(position));
holder.buttonMap.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(context,MapViewActivity.class);
intent.putExtra("Latitude", studentLatitudeList);
intent.putExtra("Longitude", studentLongitudeList);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return studentNameList.size();
}
地图活动
public class MapViewActivity extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Bundle bundle = getIntent().getExtras();
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
【问题讨论】:
-
您想将数据从一个活动传递到第二个活动吗?
-
你在哪里使用了 searchadapter
-
@Aditya Sonel 是的,我想将搜索活动中的经纬度传递给地图活动
-
@JavaGuy 检查我的答案。
标签: android google-maps android-studio android-intent