我想向我的android跟踪器应用程序添加搜索功能,以便用户可以在列表视图中搜索特定位置。我的代码已在地图上绘制了所有详细信息,我只想搜索特定地点并获取地震信息。
MainActivity.java
package com.vuvucall.maptest;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import com.google.android.gms.maps.CameraUpdate;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
SupportMapFragment mapFragment;
GoogleMap googleMap;
ArrayList<Item> mItems = new ArrayList<>();
FrameLayout mapsLayout;
Button loadDataButton;
boolean listLoad = true;
LinearLayoutManager linearLayoutManager;
RecyclerView recycleView;
ListDataAdapter listDataAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
linearLayoutManager = new LinearLayoutManager(context);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mapsLayout = (FrameLayout) findViewById(R.id.mapsLayout);
loadDataButton = (Button) findViewById(R.id.loadDataButton);
recycleView = (RecyclerView) findViewById(R.id.recycleView);
recycleView.setLayoutManager(linearLayoutManager);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
loadDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listLoad) {
listLoad = false;
mapsLayout.setVisibility(View.VISIBLE);
recycleView.setVisibility(View.GONE);
loadMapsData(mItems);
loadDataButton.setText(getString(R.string.show_list));
} else {
listLoad = true;
mapsLayout.setVisibility(View.GONE);
recycleView.setVisibility(View.VISIBLE);
loadListData(mItems);
loadDataButton.setText(getString(R.string.show_maps));
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
//
// //noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
new ProcessInBackground().execute();
}
public class ProcessInBackground extends AsyncTask<Integer, Void, ArrayList<Item>> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
Exception exception = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Busy loading rss feed...please wait...");
progressDialog.show();
}
@Override
protected ArrayList<Item> doInBackground(Integer... params) {
try {
URL url = new URL("http://quakes.bgs.ac.uk/feeds/MhSeismology.xml");
//creates new instance of PullParserFactory that can be used to create XML pull parsers
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//Specifies whether the parser produced by this factory will provide support
//for XML namespaces
factory.setNamespaceAware(false);
//creates a new instance of a XML pull parser using the currently configured
//factory features
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
/* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
* We should take into consideration that the rss feed name is also enclosed in a "<title>" tag.
* Every feed begins with these lines: "<channel><title>Feed_Name</title> etc."
* We should skip the "<title>" tag which is a child of "<channel>" tag,
* and take into consideration only the "<title>" tag which is a child of the "<item>" tag
*
* In order to achieve this, we will make use of a boolean variable called "insideItem".
*/
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, START_DOCUMENT, END_DOCUMENT etc..
int eventType = xpp.getEventType(); //loop control variable
Item item = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
//if we are at a START_TAG (opening tag)
if (eventType == XmlPullParser.START_TAG) {
//if the tag is called "item"
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
item = new Item();
}
//if the tag is called "title"
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
// extract the text between <title> and </title>
item.setTitle(xpp.nextText());
}
}
//if the tag is called "link"
else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
// extract the text between <link> and </link>
item.setLink(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("geo:lat")) {
if (insideItem) {
//extract the text between <geo:lat> and </geo:lat>
item.setLat(Double.valueOf(xpp.nextText()));
}
} else if (xpp.getName().equalsIgnoreCase("geo:long")) {
if (insideItem) {
//extract the text between <geo:lat> and </geo:lat>
item.setLon(Double.valueOf(xpp.nextText()));
}
}
}
//if we are at an END_TAG and the END_TAG is called "item"
else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
mItems.add(item);
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
exception = e;
} catch (XmlPullParserException e) {
exception = e;
} catch (IOException e) {
exception = e;
}
return mItems;
}
@Override
protected void onPostExecute(ArrayList<Item> s) {
super.onPostExecute(s);
// ArrayAdapter<Item> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, mItems);
// lvRss.setAdapter(adapter);
progressDialog.dismiss();
loadDataButton.setVisibility(View.VISIBLE);
loadListData(s);
}
}
public InputStream getInputStream(URL url) {
try {
//openConnection() returns instance that represents a connection to the remote object referred to by the URL
//getInputStream() returns a stream that reads from the open connection
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
private void loadMapsData(ArrayList<Item> listData) {
int i = 0;
for (Item item : listData) {
if (i % 5 == 0) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(item.getLat(), item.getLon()))
.title(item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
} else if (i % 5 == 1) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(item.getLat(), item.getLon()))
.title(item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
} else if (i % 5 == 2) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(item.getLat(), item.getLon()))
.title(item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
} else if (i % 5 == 3) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(item.getLat(), item.getLon()))
.title(item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
} else if (i % 5 == 4) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(item.getLat(), item.getLon()))
.title(item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
}
i++;
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//the include method will calculate the min and max bound.
for (Item item : listData) {
builder.include(new LatLng(item.getLat(), item.getLon()));
}
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.10); // offset from edges of the map 10% of screen
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
googleMap.animateCamera(cu);
}
private void loadListData(ArrayList<Item> listData) {
listDataAdapter = new ListDataAdapter(context, listData);
recycleView.setAdapter(listDataAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:visibility="gone"
android:layout_alignParentRight="true"
android:text="@string/show_maps"
android:id="@+id/loadDataButton"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<include android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbar" layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_info" />
</RelativeLayout>
新问题
答案 0 :(得分:1)
执行此操作的方法太多,但是我认为这是执行此操作的最简单方法...
首先在xml文件中创建一个Edittext 然后通过findViewbyid()在您的主要活动中找到它; 之后,在“主要活动”中添加以下行.....
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable)
{
filter(editable.toString());
}
});
现在使用filter方法。...在关闭onCreate方法之后将此方法放入您的活动类中。
private void filter(String text) {
ArrayList<String> filterdNames = new ArrayList<>();
for (String s : yourList) {
if (s.toLowerCase().contains(text.toLowerCase())) {
filterdNames.add(s);
}
}
final ArrayAdapter adap=new ArrayAdapter(this,android.R.layout.simple_list_item_1,filterdNames);
lv.setAdapter(adap);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text=adap.getItem(position).toString();
int position=yourlist.indexOf(text);
}
});
请询问您是否对此有任何疑问: