Advertisement
Guest User

Untitled

a guest
May 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package com.example.work.apptesteedeploy;
  2.  
  3. import android.app.LauncherActivity;
  4. import android.app.ProgressDialog;
  5. import android.support.v4.view.MenuItemCompat;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.widget.EditText;
  13. import android.widget.Toolbar;
  14.  
  15. import com.android.volley.Request;
  16. import com.android.volley.RequestQueue;
  17. import com.android.volley.VolleyError;
  18. import com.android.volley.toolbox.StringRequest;
  19. import com.android.volley.toolbox.Volley;
  20.  
  21. import org.json.JSONArray;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28.  
  29. public class MainActivity extends AppCompatActivity{
  30.  
  31. private static final String URL_DATA = "http://wsteste.devedp.com.br/Master/CidadeServico.svc/rest/BuscaTodasCidades";
  32.  
  33. private Toolbar toolbar;
  34. private RecyclerView recyclerView;
  35. private RecyclerView.Adapter adapter;
  36.  
  37. private List<Card> cardList;
  38.  
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_main);
  44.  
  45. //final ListView listView = findViewById(R.id.listView);
  46.  
  47. toolbar = (Toolbar) findViewById(R.id.action_search);
  48.  
  49. recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  50. recyclerView.setHasFixedSize(true);
  51. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  52.  
  53.  
  54. cardList = new ArrayList<>();
  55. loadRecyclerViewData();
  56.  
  57.  
  58. EditText editText = findViewById(R.id.edittext);
  59. editText.addTextChangedListener(new TextWatcher() {
  60. @Override
  61. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  62.  
  63. }
  64.  
  65. @Override
  66. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  67.  
  68. }
  69.  
  70. @Override
  71. public void afterTextChanged(Editable editable) {
  72. filter(editable.toString());
  73. }
  74. });
  75.  
  76.  
  77. }
  78.  
  79. ////////////////////////////////////////////////////////////////////////////////////////////
  80. private void filter(String text){
  81.  
  82. ArrayList<Card> filteredList = new ArrayList<>();
  83.  
  84. for (Card item : cardList){ //List e não ArrayList
  85. if(item.getCidade().toLowerCase().contains(text.toLowerCase())){
  86. filteredList.add(item);
  87. }
  88. }
  89.  
  90. //Adapter.filterList(filteredList); //Here, "filterList" is encountered, but the "non-static method" error occurs.
  91.  
  92. adapter.filterList(filteredList);
  93.  
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////
  96.  
  97.  
  98.  
  99. private void loadRecyclerViewData(){
  100. final ProgressDialog progressDialog = new ProgressDialog(this);
  101. progressDialog.setMessage("Carregando...");
  102. progressDialog.show();
  103.  
  104. StringRequest stringRequest = new StringRequest(Request.Method.GET,
  105. URL_DATA,
  106. new com.android.volley.Response.Listener<String>() {
  107. @Override
  108. public void onResponse(String response) {
  109. progressDialog.dismiss();
  110. try {
  111. // JSONObject jsonObject = new JSONObject(response);
  112. // JSONArray array = jsonObject.getJSONArray("");
  113. JSONArray array = new JSONArray(response);
  114. for(int i = 0; i<array.length(); i++){
  115. JSONObject o = array.getJSONObject(i);
  116. Card item = new Card (
  117. o.getString("Nome"),
  118. o.getString("Estado")
  119. );
  120. cardList.add(item);
  121.  
  122. }
  123. adapter = new Adapter(cardList, getApplicationContext());
  124. recyclerView.setAdapter(adapter);
  125.  
  126. } catch (JSONException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. },
  131. new com.android.volley.Response.ErrorListener() {
  132. @Override
  133. public void onErrorResponse(VolleyError error) {
  134. progressDialog.dismiss();
  135. }
  136. });
  137. RequestQueue requestQueue = Volley.newRequestQueue(this);
  138. requestQueue.add (stringRequest);
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement