Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. package rjj.tutorial_jsonandlistview;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15. import java.net.HttpURLConnection;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18.  
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22. String Json_STRING;
  23.  
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. new BackgroundTask().execute();
  30. //new line
  31.  
  32.  
  33. }
  34.  
  35.  
  36. class BackgroundTask extends AsyncTask<Void, Void, String> {
  37.  
  38. String json_url;
  39. String JSON_STRING;
  40.  
  41. @Override
  42. protected void onPreExecute() {
  43.  
  44. json_url = "http://10.0.2.2/rizal22.php";
  45. }
  46.  
  47. @Override
  48. protected String doInBackground(Void... params) {
  49. try {
  50. URL url = new URL(json_url);
  51. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  52. InputStream inputStream = httpURLConnection.getInputStream();
  53. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  54. StringBuilder stringBuilder = new StringBuilder();
  55. while ((JSON_STRING = bufferedReader.readLine()) != null) {
  56.  
  57. stringBuilder.append(JSON_STRING + "n");
  58. }
  59.  
  60. bufferedReader.close();
  61. inputStream.close();
  62. httpURLConnection.disconnect();
  63. return stringBuilder.toString().trim();
  64. } catch (MalformedURLException e) {
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69.  
  70. return null;
  71. }
  72.  
  73. @Override
  74. protected void onProgressUpdate(Void... values) {
  75. super.onProgressUpdate(values);
  76. }
  77.  
  78. @Override
  79. protected void onPostExecute(String result) {
  80. TextView textView = (TextView)findViewById(R.id.textView2);
  81. textView.setText(result);
  82. Json_STRING = result;
  83. }
  84.  
  85. }
  86. //List view time!
  87. public void parseJSON(View view) {
  88. if(Json_STRING == null){
  89. Toast.makeText(getApplicationContext(), "First Get JSON", Toast.LENGTH_SHORT).show();
  90. }else{
  91. Intent intent = new Intent(this, DisplayListView.class);
  92. intent.putExtra("json_data", Json_STRING);
  93. startActivity(intent);
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. package rjj.tutorial_jsonandlistview;
  101.  
  102. import android.content.Intent;
  103. import android.support.v7.app.AppCompatActivity;
  104. import android.os.Bundle;
  105.  
  106.  
  107. import android.view.View;
  108. import android.widget.AdapterView;
  109. import android.widget.ListView;
  110. import android.widget.SearchView;
  111. import android.widget.TextView;
  112.  
  113. import org.json.JSONArray;
  114. import org.json.JSONException;
  115. import org.json.JSONObject;
  116.  
  117.  
  118.  
  119. public class DisplayListView extends AppCompatActivity {
  120.  
  121. String json_string;
  122. JSONObject jsonObject;
  123. JSONArray jsonArray;
  124. ContactAdapter contactAdapter;
  125. ListView listView;
  126. SearchView sv;
  127.  
  128. @Override
  129. protected void onCreate(Bundle savedInstanceState) {
  130. super.onCreate(savedInstanceState);
  131. setContentView(R.layout.display_listview_layout);
  132. listView = (ListView)findViewById(R.id.listview);
  133.  
  134. contactAdapter = new ContactAdapter(this,R.layout.row_layout);
  135. listView.setAdapter(contactAdapter);
  136.  
  137. json_string = getIntent().getExtras().getString("json_data");
  138. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  139. @Override
  140. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  141.  
  142.  
  143.  
  144. TextView textView = (TextView) view.findViewById(R.id.tx_id);
  145.  
  146. String text = textView.getText().toString();
  147. Intent intent = new Intent(DisplayListView.this, Update.class);
  148. intent.putExtra("id", text);
  149. //new line
  150. intent.putExtra("json_data", json_string);
  151. startActivity(intent);
  152. }
  153.  
  154. });
  155. //Searchview
  156. sv = (SearchView)findViewById(R.id.search);
  157.  
  158. sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  159. @Override
  160. public boolean onQueryTextSubmit(String query) {
  161. return false;
  162. }
  163.  
  164. @Override
  165. public boolean onQueryTextChange(String newText) {
  166. contactAdapter.getFilter(
  167.  
  168. ).filter(newText);
  169. listView.setFilterText(newText);
  170. return true;
  171. }
  172. });
  173. //End of Searchview
  174.  
  175. try {
  176. jsonObject = new JSONObject(json_string);
  177. jsonArray = jsonObject.getJSONArray("server_response");
  178. int count = 0;
  179. String id ,firstname , surname, age , username, password;
  180.  
  181.  
  182. while(count<jsonArray.length()){
  183. JSONObject JO = jsonArray.getJSONObject(count);
  184. id = JO.getString("id");
  185. firstname = JO.getString("PLATE_NUM");
  186. surname = JO.getString("PUV_TYPE");
  187. Contacts contact = new Contacts(id, firstname, surname);
  188. contactAdapter.add(contact);
  189.  
  190.  
  191. count++;
  192.  
  193. }
  194. } catch (JSONException e) {
  195. e.printStackTrace();
  196. }
  197.  
  198. }
  199.  
  200. }
  201.  
  202. package rjj.tutorial_jsonandlistview;
  203.  
  204. /**
  205. * Created by Julian on 7/20/2017.
  206. */
  207.  
  208. public class Contacts {
  209.  
  210. private String id,firstname,surname,age,username,password;
  211.  
  212. public Contacts(String id, String firstname, String surname){
  213.  
  214.  
  215. this.setId(id);
  216. this.setFirstname(firstname);
  217. this.setSurname(surname);
  218.  
  219.  
  220.  
  221.  
  222.  
  223. }
  224. public String getId() {
  225. return id;
  226. }
  227.  
  228. public void setId(String id) {
  229. this.id = id;
  230. }
  231.  
  232. public String getFirstname() {
  233. return firstname;
  234. }
  235.  
  236. public void setFirstname(String firstname) {
  237. this.firstname = firstname;
  238. }
  239.  
  240. public String getSurname() {
  241. return surname;
  242. }
  243.  
  244. public void setSurname(String surname) {
  245. this.surname = surname;
  246. }
  247.  
  248.  
  249. }
  250.  
  251. package rjj.tutorial_jsonandlistview;
  252.  
  253. import android.content.Context;
  254. import android.support.annotation.LayoutRes;
  255. import android.support.annotation.NonNull;
  256. import android.support.annotation.Nullable;
  257. import android.view.LayoutInflater;
  258. import android.view.View;
  259. import android.view.ViewGroup;
  260. import android.widget.ArrayAdapter;
  261. import android.widget.TextView;
  262.  
  263. import java.util.ArrayList;
  264. import java.util.List;
  265.  
  266. /**
  267. * Created by Julian on 7/20/2017.
  268. */
  269.  
  270. public class ContactAdapter extends ArrayAdapter {
  271. List list = new ArrayList();
  272.  
  273. public ContactAdapter(@NonNull Context context, @LayoutRes int resource) {
  274. super(context, resource);
  275.  
  276.  
  277. }
  278.  
  279.  
  280. public void add(Contacts object) {
  281. super.add(object);
  282. list.add(object);
  283. }
  284.  
  285. @Override
  286. public int getCount() {
  287. return list.size();
  288. }
  289.  
  290. @Nullable
  291. @Override
  292. public Object getItem(int position) {
  293. return list.get(position);
  294. }
  295.  
  296. @NonNull
  297. @Override
  298. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  299.  
  300. View row;
  301. row = convertView;
  302. ContactHolder contactHolder;
  303. if(row == null){
  304. LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  305. row = layoutInflater.inflate(R.layout.row_layout,parent,false);
  306. contactHolder = new ContactHolder();
  307. contactHolder.tx_id = (TextView)row.findViewById(R.id.tx_id);
  308. contactHolder.tx_firstname = (TextView)row.findViewById(R.id.tx_firstname);
  309. contactHolder.tx_surname = (TextView)row.findViewById(R.id.tx_surname);
  310.  
  311. row.setTag(contactHolder);
  312.  
  313. } else{
  314. contactHolder = (ContactHolder)row.getTag();
  315. }
  316. Contacts contacts = (Contacts)this.getItem(position);
  317. contactHolder.tx_id.setText(contacts.getId());
  318. contactHolder.tx_firstname.setText(contacts.getFirstname());
  319. contactHolder.tx_surname.setText(contacts.getSurname());
  320.  
  321. return row;
  322. }
  323.  
  324. static class ContactHolder{
  325. TextView tx_id, tx_firstname, tx_surname;
  326. }
  327.  
  328. }
  329.  
  330. <?php
  331. $host='localhost';
  332. $user='root';
  333. $password='';
  334. $db='employee101';
  335. //Old id $_id = $_POST['id'];
  336. $sql = "select * from employee_data;";
  337. //Old line $sql = "select * from employee_data where id = $_id;";
  338.  
  339. $con = mysqli_connect($host,$user,$password,$db);
  340.  
  341. $result = mysqli_query($con, $sql);
  342.  
  343. $response = array();
  344.  
  345. while($row = mysqli_fetch_array($result)){
  346.  
  347. array_push($response,array("id"=>$row[0],"PLATE_NUM"=>$row[1],"PUV_TYPE"=>$row[2]));
  348.  
  349. }
  350.  
  351. echo json_encode(array("server_response"=>$response));
  352.  
  353. mysqli_close($con);
  354.  
  355.  
  356.  
  357.  
  358. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement