我已经解析了数据,我需要将解析后的数据本地存储在移动设备上,以便应用可以访问它。我不确定最好的选择是什么,因为每次都需要更新数据我运行该应用程序.....
我打算尝试数据库,但不确定是否适合该问题
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;
ListView listView;
EditText editText;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
linearLayoutManager = new LinearLayoutManager(context);
// EditText editText = findViewById((R.id.search_text));
//
// 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());
// }
// });
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mapsLayout = findViewById(R.id.mapsLayout);
loadDataButton = findViewById(R.id.loadDataButton);
recycleView = 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);
}
}