Advertisement
Guest User

Untitled

a guest
May 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. public class search extends AppCompatActivity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_search);
  7. }
  8. public void onClick(View v) {
  9. switch (v.getId()){
  10. case R.id.imageButton2: new StoreUserDataAsyncTask().execute();
  11. break;
  12. }
  13. }
  14. @Override
  15. public boolean onCreateOptionsMenu(Menu menu) {
  16. // Inflate the menu; this adds items to the action bar if it is present.
  17. getMenuInflater().inflate(R.menu.menu_searchh, menu);
  18. return true;
  19. }
  20. @Override
  21. public boolean onOptionsItemSelected(MenuItem item) {
  22. // Handle action bar item clicks here. The action bar will
  23. // automatically handle clicks on the Home/Up button, so long
  24. // as you specify a parent activity in AndroidManifest.xml.
  25. int id = item.getItemId();
  26.  
  27. //noinspection SimplifiableIfStatement
  28. if (id == R.id.action_settings) {
  29. return true;
  30. }
  31.  
  32. return super.onOptionsItemSelected(item);
  33. }
  34.  
  35. ArrayList<User> userlist1=null;
  36.  
  37. public class StoreUserDataAsyncTask extends AsyncTask<Void,Void,ArrayList<User>> {
  38. User user;
  39. GetUserCallback userCallback;
  40.  
  41. ProgressDialog progressDialog;
  42. Spinner s11 = (Spinner) findViewById(R.id.spinner);
  43. Spinner s2 = (Spinner) findViewById(R.id.spinner2);
  44. String bloodd = s11.getSelectedItem().toString();
  45. String loc = s2.getSelectedItem().toString();
  46.  
  47. protected ArrayList<User> doInBackground(Void... params) {
  48.  
  49. User returnedUser=null;
  50. ArrayList<User> userlist=null;
  51. final int CONNECTION_TIMEOUT = 1000 * 15;
  52. final String SERVER_ADDRESS = "http://xxxxx.comli.com/";
  53. ArrayList<NameValuePair> dataToSend = new ArrayList<>();
  54. dataToSend.add(new BasicNameValuePair("bloodtype", bloodd));
  55. dataToSend.add(new BasicNameValuePair("location", loc));
  56.  
  57. HttpParams httpRequestParams = new BasicHttpParams();
  58. HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
  59. HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
  60.  
  61. HttpClient client = new DefaultHttpClient(httpRequestParams);
  62. HttpPost post = new HttpPost(SERVER_ADDRESS + "/Search.php");
  63.  
  64. try {
  65. post.setEntity(new UrlEncodedFormEntity(dataToSend));
  66. HttpResponse httpResponse = client.execute(post);
  67.  
  68. HttpEntity entity = httpResponse.getEntity();
  69. String result = EntityUtils.toString(entity);
  70. JSONObject jObject = new JSONObject(result);
  71.  
  72. if (jObject.length() == 0) {
  73. userlist = null;
  74. Context context = getApplicationContext();
  75. CharSequence text = "no available donor!";
  76. int duration = Toast.LENGTH_SHORT;
  77.  
  78. } else {
  79. String resultt = EntityUtils.toString(entity);
  80. JSONArray jsonArray = new JSONArray(resultt);
  81. for (int i = 0; i < jsonArray.length(); i++) {
  82. JSONObject jsonObject = jsonArray.getJSONObject(i);
  83. String fname = jsonObject.getString("firstname");
  84. String lname = jsonObject.getString("lastname");
  85. String blood = jsonObject.getString("blood");
  86. String location = jsonObject.getString("location");
  87. String email = jsonObject.getString("email");
  88. String age = jsonObject.getString("age");
  89. String password = jsonObject.getString("password");
  90. String phone = jsonObject.getString("phone");
  91. int agee = Integer.parseInt(age);
  92. int phonee = Integer.parseInt(phone);
  93.  
  94. returnedUser = new User(fname, lname, blood, location, email, password, agee, phonee);
  95. userlist.add(returnedUser);
  96. }
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. userlist1=userlist;
  102. return userlist;
  103. }
  104. protected void onPostExecute(ArrayList<User> aVoid) {
  105. progressDialog.dismiss();
  106. displayCountryList();
  107. }
  108. }
  109.  
  110. private void displayCountryList(){
  111. MyCustomAdapter dataAdapter = null;
  112. try {
  113. //create an ArrayAdaptar from the String Array
  114. dataAdapter = new MyCustomAdapter(this,
  115. R.layout.listv, userlist1);
  116. ListView listView = (ListView) findViewById(R.id.listView);
  117. // Assign adapter to ListView
  118. listView.setAdapter(dataAdapter);
  119.  
  120. //enables filtering for the contents of the given ListView
  121. listView.setTextFilterEnabled(true);
  122. }
  123. catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. }
  127.  
  128. class MyCustomAdapter extends ArrayAdapter<User> {
  129.  
  130. private ArrayList<User> userlist;
  131.  
  132. public MyCustomAdapter(Context context, int textViewResourceId,
  133. ArrayList<User> userlist) {
  134. super(context, textViewResourceId, userlist);
  135. this.userlist = new ArrayList<User>();
  136. this.userlist.addAll(userlist);
  137. }
  138.  
  139. private class ViewHolder {
  140. TextView name;
  141. TextView phone;
  142. TextView email;
  143. }
  144.  
  145. @Override
  146. public View getView(int position, View convertView, ViewGroup parent) {
  147.  
  148. ViewHolder holder = null;
  149. Log.v("ConvertView", String.valueOf(position));
  150. if (convertView == null) {
  151.  
  152. LayoutInflater vi = (LayoutInflater)getSystemService(
  153. Context.LAYOUT_INFLATER_SERVICE);
  154. convertView = vi.inflate(R.layout.listv, null);
  155.  
  156. holder = new ViewHolder();
  157. holder.name = (TextView) convertView.findViewById(R.id.textView31);
  158. holder.phone = (TextView) convertView.findViewById(R.id.textView33);
  159. holder.email = (TextView) convertView.findViewById(R.id.textView34);
  160.  
  161. convertView.setTag(holder);
  162.  
  163. } else {
  164. holder = (ViewHolder) convertView.getTag();
  165. }
  166.  
  167. User user = userlist.get(position);
  168. holder.name.setText(user.getFirstname());
  169. holder.phone.setText(user.getPhone());
  170. holder.email.setText(user.getEmail());
  171. return convertView;
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement