public class LocationListActivity extends AppCompatActivity {
private static final String TAG = LocationListActivity.class.getSimpleName();
private DatabaseQuery query;
private List<LocationObject> allLocations;
private LocationMapObject locationMapObject;
private RequestQueue queue;
private LocationListAdapter locationListAdapter;
private List<LocationListModel> allData;
private RecyclerView locationRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_location_list);
setTitle(R.string.location_list_title);
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
queue = Volley.newRequestQueue(LocationListActivity.this);
query = new DatabaseQuery(LocationListActivity.this);
allData = new ArrayList<>();
//arama terimini burada alıyoruz.
handleIntent(getIntent());
allLocations = query.getStoredDataLocations();
if(allLocations != null){
for(int i = 0; i < allLocations.size(); i++){
// make volley network call here
requestJsonObject(allLocations.get(i));
}
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LocationListActivity.this);
locationRecyclerView = findViewById(R.id.rv_location_list);
locationRecyclerView.setLayoutManager(linearLayoutManager);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
DatabaseQuery query = new DatabaseQuery(LocationListActivity.this);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String queryWord = intent.getStringExtra(SearchManager.QUERY);
int numOfLocationsStored = query.countAllStoredLocations();
//gelen arama terimini db ye kaydet
if(numOfLocationsStored <= 10 ){
DatabaseQuery dbQuery = new DatabaseQuery(LocationListActivity.this);
dbQuery.insertNewLocation(queryWord);
}
else
Toast.makeText(LocationListActivity.this, R.string.location_list_message, Toast.LENGTH_SHORT).show();
}
}
private void requestJsonObject(final LocationObject paramValue){
String url ="http://api.openweathermap.org/data/2.5/weather?q="+paramValue.getLocation()+"&lang=tr&APPID="+ Helper.API_KEY+"&units=metric";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
locationMapObject = gson.fromJson(response, LocationMapObject.class);
if (null == locationMapObject) {
Toast.makeText(getApplicationContext(), "Nothing was returned", Toast.LENGTH_LONG).show();
} else {
int rowId = paramValue.getId();
Long tempVal = Math.round(Math.floor(Double.parseDouble(locationMapObject.getMain().getTemp())));
String city = locationMapObject.getName() + ", " + locationMapObject.getSys().getCountry();
String weatherInfo = String.valueOf(tempVal) + "<sup>o</sup>, " + Helper.capitalizeFirstLetter(locationMapObject.getWeather().get(0).getDescription());
allData.add(new LocationListModel(rowId, city, weatherInfo));
locationListAdapter = new LocationListAdapter(LocationListActivity.this, allData);
locationRecyclerView.setAdapter(locationListAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);
}
}
Konumları gösteren activity nin kodları bu şekilde.