· 9 years ago · Dec 13, 2016, 05:00 AM
1import android.app.ProgressDialog;
2import android.content.Context;
3import android.database.Cursor;
4import android.database.sqlite.SQLiteDatabase;
5import android.net.Uri;
6import android.support.v7.app.AppCompatActivity;
7import android.os.Bundle;
8import android.widget.ListView;
9import android.widget.TextView;
10import android.widget.Toast;
11
12import com.android.volley.Request;
13import com.android.volley.Response;
14import com.android.volley.VolleyError;
15import com.android.volley.toolbox.JsonArrayRequest;
16import com.google.android.gms.appindexing.Action;
17import com.google.android.gms.appindexing.AppIndex;
18import com.google.android.gms.common.api.GoogleApiClient;
19
20import org.json.JSONArray;
21import org.json.JSONObject;
22
23import java.util.ArrayList;
24import java.util.List;
25
26public class MainActivity extends AppCompatActivity {
27 private ListView list;
28 private SQLiteDatabase db;
29 private ProgressDialog dialog;
30
31
32 /**
33 * ATTENTION: This was auto-generated to implement the App Indexing API.
34 * See https://g.co/AppIndexing/AndroidStudio for more information.
35 */
36 private GoogleApiClient client;
37
38
39 @Override
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.activity_main);
43 list = (ListView) findViewById(R.id.list);
44 createDatabase();
45 dialog = new ProgressDialog(this);
46 ar = new ArrayList<Contact>();
47 asytask();
48 Adapter adapter = new Adapter(this, R.layout.item,ar );
49 list.setAdapter(adapter);
50
51
52 // ATTENTION: This was auto-generated to implement the App Indexing API.
53 // See https://g.co/AppIndexing/AndroidStudio for more information.
54 client = new `enter code here`GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
55 }
56
57 public void asytask() {
58 dialog.setMessage("Please Wait...");
59 dialog.show();
60 String url = "http://anshul-kothari.000webhostapp.com/sendtosqlite.php";
61 JsonArrayRequest objectRequest = new JsonArrayRequest(Request.Method.GET, url,
62 new Response.Listener<JSONArray>() {
63 @Override
64 public void onResponse(JSONArray response) {
65
66 dialog.dismiss();
67
68 for (int i = 0; i < response.length(); i++) {
69 try {
70 JSONObject obj = response.getJSONObject(i);
71 Contact temp = new Contact();
72 temp.setName(obj.getString("name"));
73 temp.setFname(obj.getString("fname"));
74 temp.setDob(obj.getString("dob"));
75 temp.setBlood(obj.getString("blood"));
76 temp.setInterest(obj.getString("interest"));
77 temp.setWork(obj.getString("work"));
78 temp.setCompany(obj.getString("company"));
79 temp.setComapnyadd(obj.getString("companyadd"));
80 temp.setHomeaddress(obj.getString("homeaddress"));
81 temp.setPassword(obj.getString("password"));
82 insert(temp);
83
84
85
86
87
88 } catch (Exception e) {
89 e.printStackTrace();
90 }
91
92 }
93
94 }
95 },
96 new Response.ErrorListener() {
97 @Override
98 public void onErrorResponse(VolleyError error) {
99 dialog.dismiss();
100 Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
101 error.printStackTrace();
102
103 }
104 });
105 mySingleton.getInstance(this).addToRequestQueue(objectRequest);
106 }
107 public void createDatabase(){
108 db = openOrCreateDatabase("contactDB", Context.MODE_PRIVATE, null);
109 db.execSQL(" CREATE TABLE IF NOT EXISTS member(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,fname VARCHAR, dob VARCHAR,blood VARCHAR, interest VARCHAR, company VARCHAR,companyadd VARCHAR, homeaddress VARCHAR,password VARCHAR);");
110 }
111 public void insert(Contact contact){
112 String name = contact.getName();
113 String fname = contact.getFname();
114 String dob = contact.getDob();
115 String blood = contact.getBlood();
116 String interest = contact.getInterest();
117 String work = contact.getWork();
118 String company = contact.getCompany();
119 String companyadd = contact.getComapnyadd();
120 String homeaddress = contact.getHomeaddress();
121 String password = contact.getPassword();
122 String query = "INSERT INTO member(name,fname,dob,blood,interest,work,company,companyadd,homeaddress,password) VALUES('"+name+"' , '"+fname+"' , '"+dob+"' , '"+blood+"' , '"+interest+"' , '"+work+"' , '"+company+"' , '"+ companyadd+"' , '"+homeaddress+"' , '"+password+"');";
123 db.execSQL(query);
124 }
125 public List<Contact> getContact(){
126 List<Contact> con = new ArrayList<Contact>();
127 String query = "select * from member";
128 Cursor cursor = db.rawQuery(query,null);
129 cursor.moveToFirst();
130 while(!cursor.isAfterLast()){
131 Contact temp = new Contact();
132 temp.setName(cursor.getString(1));
133 temp.setFname(cursor.getString(2));
134 temp.setDob(cursor.getString(3));
135 temp.setBlood(cursor.getString(4));
136 temp.setInterest(cursor.getString(5));
137 temp.setWork(cursor.getString(6));
138
139 temp.setCompany(cursor.getString(7));
140 temp.setComapnyadd(cursor.getString(8));
141 temp.setHomeaddress(cursor.getString(9));
142 temp.setPassword(cursor.getString(10));
143 con.add(temp);
144 cursor.moveToNext();
145
146 }
147 return con;
148 }
149 @Override
150 public void onStart() {
151 super.onStart();
152
153 // ATTENTION: This was auto-generated to implement the App Indexing API.
154 // See https://g.co/AppIndexing/AndroidStudio for more information.
155 client.connect();
156 Action viewAction = Action.newAction(
157 Action.TYPE_VIEW, // TODO: choose an action type.
158 "Main Page", // TODO: Define a title for the content shown.
159 // TODO: If you have web page content that matches this app activity's content,
160 // make sure this auto-generated web page URL is correct.
161 // Otherwise, set the URL to null.
162 Uri.parse("http://host/path"),
163 // TODO: Make sure this auto-generated app URL is correct.
164 Uri.parse("android-app://com.example.anshulkothari.memberlist/http/host/path")
165 );
166 AppIndex.AppIndexApi.start(client, viewAction);
167 }
168
169 @Override
170 public void onStop() {
171 super.onStop();
172
173 // ATTENTION: This was auto-generated to implement the App Indexing API.
174 // See https://g.co/AppIndexing/AndroidStudio for more information.
175 Action viewAction = Action.newAction(
176 Action.TYPE_VIEW, // TODO: choose an action type.
177 "Main Page", // TODO: Define a title for the content shown.
178 // TODO: If you have web page content that matches this app activity's content,
179 // make sure this auto-generated web page URL is correct.
180 // Otherwise, set the URL to null.
181 Uri.parse("http://host/path"),
182 // TODO: Make sure this auto-generated app URL is correct.
183 Uri.parse("android-app://com.example.anshulkothari.memberlist/http/host/path")
184 );
185 AppIndex.AppIndexApi.end(client, viewAction);
186 client.disconnect();
187 }
188}