Advertisement
Guest User

Untitled

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