Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import android.app.ProgressDialog;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextWatcher;
  7. import android.view.Menu;
  8. import android.view.MenuInflater;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.EditText;
  12. import android.widget.ListAdapter;
  13. import android.widget.ListView;
  14. import android.widget.SimpleAdapter;
  15.  
  16. import com.android.volley.Request;
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  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.HashMap;
  27. import java.util.List;
  28.  
  29. public class grabedDb extends AppCompatActivity {
  30. EditText grabResults;
  31. String url = "http://MY_URL_HERE";
  32. ArrayList<HashMap<String, String>> Item_List;
  33. ProgressDialog PD;
  34. ArrayAdapter adapter;
  35.  
  36. // JSON Node names
  37. public static final String ITEM_ID = "name";
  38. public static final String ITEM_NAME = "rate";
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_grabed_db);
  44. grabResults = (EditText) findViewById(R.id.grabed_et_search);
  45. grabResults.addTextChangedListener(new TextWatcher() {
  46. @Override
  47. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  48.  
  49. }
  50.  
  51. @Override
  52. public void onTextChanged(CharSequence s, int start, int before, int count) {
  53. //perform Search
  54. if (s.toString().equals("")){
  55. //Reset List
  56. ReadDataFromDB();
  57. } else {
  58. grabedDb.this.adapter.getFilter().filter(s);
  59. }
  60. }
  61.  
  62. @Override
  63. public void afterTextChanged(Editable s) {
  64.  
  65. }
  66. });
  67.  
  68. Item_List = new ArrayList<HashMap<String, String>>();
  69.  
  70. PD = new ProgressDialog(this);
  71. PD.setMessage("Please Wait...");
  72. PD.setCancelable(false);
  73.  
  74. ReadDataFromDB();
  75. }
  76.  
  77. @Override
  78. public boolean onCreateOptionsMenu(Menu menu) {
  79. MenuInflater inflater = getMenuInflater();
  80. inflater.inflate(R.menu.activity_main_actions, menu);
  81. return super.onCreateOptionsMenu(menu);
  82. }
  83. private void ReadDataFromDB() {
  84. PD.show();
  85. JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
  86. new Response.Listener<JSONObject>() {
  87.  
  88. @Override
  89. public void onResponse(JSONObject response) {
  90. try {
  91. int success = response.getInt("success");
  92.  
  93. if (success == 1) {
  94. JSONArray ja = response.getJSONArray("mydrugs");
  95.  
  96. for (int i = 0; i < ja.length(); i++) {
  97.  
  98. JSONObject jobj = ja.getJSONObject(i);
  99. HashMap<String, String> item = new HashMap<String, String>();
  100. item.put(ITEM_ID, jobj.getString(ITEM_ID));
  101. item.put(ITEM_NAME,
  102. jobj.getString(ITEM_NAME));
  103.  
  104. Item_List.add(item);
  105.  
  106. } // for loop ends
  107.  
  108. String[] from = { ITEM_ID, ITEM_NAME };
  109. int[] to = { R.id.item_name, R.id.item_id };
  110.  
  111. adapter = new SimpleAdapter(
  112. getApplicationContext(), Item_List,
  113. R.layout.list_items, from, to);
  114.  
  115. ListView myList = (ListView) findViewById(R.id.list_list_listView);
  116. myList.setAdapter(adapter);
  117. PD.dismiss();
  118. myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
  119. @Override
  120. public void onItemClick(AdapterView<?> parent, View view, int position,
  121. long id) {
  122.  
  123. Intent modify_intent = new Intent(grabedDb.this,
  124. updatedata.class);
  125.  
  126. modify_intent.putExtra("item", Item_List.get(position));
  127.  
  128. startActivity(modify_intent);
  129.  
  130. }
  131. });
  132. } // if ends
  133.  
  134. } catch (JSONException e) {
  135. e.printStackTrace();
  136. }
  137.  
  138. }
  139. }, new Response.ErrorListener() {
  140.  
  141. @Override
  142. public void onErrorResponse(VolleyError error) {
  143. PD.dismiss();
  144. }
  145. });
  146.  
  147. // Adding request to request queue
  148. MyApplication.getInstance().addToReqQueue(jreq);
  149.  
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement