Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivityFragment extends Fragment {
- ArrayList<HashMap<String,String>> city_list;
- LinearLayout progress;
- public MainActivityFragment() {
- }
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setHasOptionsMenu(true);
- ReadJson json = new ReadJson();
- json.execute();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- final View view= inflater.inflate(R.layout.fragment_main, container, false);
- final EditText search= (EditText) view.findViewById(R.id.search_view);
- search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (actionId == EditorInfo.IME_ACTION_SEARCH ||
- actionId == EditorInfo.IME_ACTION_DONE ||
- actionId == EditorInfo.IME_ACTION_GO ||
- event.getAction() == KeyEvent.ACTION_DOWN &&
- event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
- boolean found = false;
- String inserted = v.getText().toString();
- for(int i=0;i<city_list.size();i++){
- if(inserted.equalsIgnoreCase(city_list.get(i).get("name").toString())){
- found=true;
- TextView id = (TextView) view.findViewById(R.id.city_id);
- id.setText(city_list.get(i).get("id").toString());
- TextView lat = (TextView) view.findViewById(R.id.city_lat);
- lat.setText(city_list.get(i).get("lat").toString());
- TextView lon = (TextView) view.findViewById(R.id.city_long);
- lon.setText(city_list.get(i).get("lon").toString());
- }
- }
- if(!found) {
- Toast.makeText(getContext(), "City not found", Toast.LENGTH_SHORT).show();
- }
- InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
- return true; // consume.
- }
- return false; // pass on to other listeners.
- }
- });
- return view;
- }
- public String loadJsonFromAsset(){
- String json = null;
- try{
- InputStream is = getActivity().getAssets().open("city.list.json");
- int size = is.available();
- byte[] buffer = new byte[size];
- is.read(buffer);
- is.close();
- json = new String(buffer,"UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- return json;
- }
- public class ReadJson extends AsyncTask<Void,Void,Void>{
- protected void onPreExecute(){
- getActivity().setContentView(R.layout.progressbar_layout);
- progress = (LinearLayout) getActivity().findViewById(R.id.progressbar_view);
- progress.setVisibility(View.VISIBLE);
- }
- protected void onPostExecute(Void result){
- progress.setVisibility(View.GONE);
- }
- @Override
- protected Void doInBackground(Void... params) {
- try{
- JSONObject obj = new JSONObject(loadJsonFromAsset());
- JSONArray cities = obj.getJSONArray("cities");
- city_list= new ArrayList<>();
- HashMap<String,String> m_li;
- for(int i=0;i<cities.length();i++){
- JSONObject jo_inside = cities.getJSONObject(i);
- String name = jo_inside.getString("name");
- Long id = jo_inside.getLong("_id");
- Double lat = jo_inside.getJSONObject("coord").getDouble("lat");
- Double lon = jo_inside.getJSONObject("coord").getDouble("lon");
- String s_id = id.toString();
- String s_lat = lat.toString();
- String s_lon = lon.toString();
- m_li = new HashMap<String, String>();
- m_li.put("name", name);
- m_li.put("id", s_id);
- m_li.put("lat", s_lat);
- m_li.put("lon", s_lon);
- city_list.add(m_li);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/progressbar_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal" >
- <ProgressBar
- style="?android:attr/progressBarStyle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical|center_horizontal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical|center_horizontal"
- android:text="Loading data..." />
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="1dp"
- android:background="#C0C0C0" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement