Skip to content

Commit 6d2d27f

Browse files
committed
final commit
1 parent 1649691 commit 6d2d27f

13 files changed

+680
-33
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
android:roundIcon="@mipmap/ic_launcher_round"
1313
android:supportsRtl="true"
1414
android:theme="@style/AppTheme">
15-
<activity android:name=".MainActivity">
15+
<activity android:name=".ListActivity">
1616
<intent-filter>
1717
<action android:name="android.intent.action.MAIN" />
1818

1919
<category android:name="android.intent.category.LAUNCHER" />
2020
</intent-filter>
2121
</activity>
22+
<activity android:name=".CreateActivity" />
23+
<activity android:name=".UpdateActivity"></activity>
2224
</application>
2325

2426
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.deepagi.androidtodolist;
2+
3+
public class App {
4+
5+
public static final String URL_CREATE="http://demo.revivalx.com/todolist-api/create_task.php";
6+
public static final String URL_GET_ALL = "http://demo.revivalx.com/todolist-api/get_all_tasks.php";
7+
public static final String URL_GET_DETAIL = "http://demo.revivalx.com/todolist-api/get_task_details.php?taskId=";
8+
public static final String URL_UPDATE = "http://demo.revivalx.com/todolist-api/update_task.php?taskId=";
9+
public static final String URL_DELETE = "http://demo.revivalx.com/todolist-api/delete_task.php?taskId=";
10+
11+
public static final String KEY_ID = "taskId";
12+
public static final String KEY_NAME = "name";
13+
public static final String KEY_DESCRIPTION = "description";
14+
15+
public static final String TAG_JSON_ARRAY="tasks";
16+
public static final String TAG_JSON_DETAIL="task";
17+
public static final String TAG_ID = "taskId";
18+
public static final String TAG_NAME = "name";
19+
public static final String TAG_DESCRIPTION = "description";
20+
21+
public static final String ID = "taskId";
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.deepagi.androidtodolist;
2+
3+
import android.app.ProgressDialog;
4+
import android.content.Intent;
5+
import android.os.AsyncTask;
6+
import android.os.Bundle;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.view.View;
9+
import android.widget.Button;
10+
import android.widget.EditText;
11+
import android.widget.Toast;
12+
13+
import java.util.HashMap;
14+
15+
public class CreateActivity extends AppCompatActivity implements View.OnClickListener{
16+
17+
//Defining views
18+
private EditText editTextName;
19+
private EditText editTextDesription;
20+
21+
private Button buttonAdd;
22+
private Button buttonView;
23+
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
setContentView(R.layout.activity_create);
28+
29+
//Initializing views
30+
editTextName = (EditText) findViewById(R.id.txtName);
31+
editTextDesription = (EditText) findViewById(R.id.txtDescription);
32+
33+
buttonAdd = (Button) findViewById(R.id.btnCreate);
34+
buttonView = (Button) findViewById(R.id.btnBack);
35+
36+
//Setting listeners to button
37+
buttonAdd.setOnClickListener(this);
38+
buttonView.setOnClickListener(this);
39+
}
40+
41+
42+
//Adding a task
43+
private void addTask(){
44+
45+
final String name = editTextName.getText().toString().trim();
46+
final String description = editTextDesription.getText().toString().trim();
47+
48+
class AddTask extends AsyncTask<Void,Void,String> {
49+
50+
ProgressDialog loading;
51+
52+
@Override
53+
protected void onPreExecute() {
54+
super.onPreExecute();
55+
loading = ProgressDialog.show(CreateActivity.this,"Adding...","Wait...",false,false);
56+
}
57+
58+
@Override
59+
protected void onPostExecute(String s) {
60+
super.onPostExecute(s);
61+
loading.dismiss();
62+
Toast.makeText(CreateActivity.this,s,Toast.LENGTH_LONG).show();
63+
}
64+
65+
@Override
66+
protected String doInBackground(Void... v) {
67+
HashMap<String,String> params = new HashMap<>();
68+
params.put(App.KEY_NAME,name);
69+
params.put(App.KEY_DESCRIPTION,description);
70+
71+
RequestHandler rh = new RequestHandler();
72+
String res = rh.sendPostRequest(App.URL_CREATE, params);
73+
return res;
74+
}
75+
}
76+
77+
AddTask ae = new AddTask();
78+
ae.execute();
79+
}
80+
81+
@Override
82+
public void onClick(View v) {
83+
if(v == buttonAdd){
84+
addTask();
85+
startActivity(new Intent(CreateActivity.this,ListActivity.class));
86+
}
87+
88+
if(v == buttonView){
89+
startActivity(new Intent(this,ListActivity.class));
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.deepagi.androidtodolist;
2+
3+
import android.app.ProgressDialog;
4+
import android.content.Intent;
5+
import android.os.AsyncTask;
6+
import android.os.Bundle;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.view.View;
9+
import android.widget.AdapterView;
10+
import android.widget.Button;
11+
import android.widget.ListAdapter;
12+
import android.widget.ListView;
13+
import android.widget.SimpleAdapter;
14+
15+
import org.json.JSONArray;
16+
import org.json.JSONException;
17+
import org.json.JSONObject;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
22+
public class ListActivity extends AppCompatActivity implements ListView.OnItemClickListener {
23+
24+
private ListView listView;
25+
private String JSON_STRING;
26+
private Button buttonAdd;
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
setContentView(R.layout.activity_list);
32+
listView = (ListView) findViewById(R.id.lv_task);
33+
listView.setOnItemClickListener(this);
34+
getJSON();
35+
36+
buttonAdd = (Button) findViewById(R.id.btnCreate);
37+
buttonAdd.setOnClickListener(new View.OnClickListener() {
38+
39+
public void onClick(View arg0) {
40+
41+
Intent createActivity = new Intent(getApplicationContext(), CreateActivity.class);
42+
startActivity(createActivity);
43+
44+
}
45+
});
46+
}
47+
48+
49+
private void showTask(){
50+
JSONObject jsonObject = null;
51+
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
52+
try {
53+
jsonObject = new JSONObject(JSON_STRING);
54+
JSONArray result = jsonObject.getJSONArray(App.TAG_JSON_ARRAY);
55+
56+
for(int i = 0; i<result.length(); i++){
57+
JSONObject jo = result.getJSONObject(i);
58+
String id = jo.getString(App.TAG_ID);
59+
String name = jo.getString(App.TAG_NAME);
60+
61+
HashMap<String,String> tasks = new HashMap<>();
62+
tasks.put(App.TAG_ID,id);
63+
tasks.put(App.TAG_NAME,name);
64+
list.add(tasks);
65+
}
66+
67+
} catch (JSONException e) {
68+
e.printStackTrace();
69+
}
70+
71+
ListAdapter adapter = new SimpleAdapter(
72+
ListActivity.this, list, R.layout.list_item,
73+
new String[]{App.TAG_NAME},
74+
new int[]{R.id.name});
75+
76+
listView.setAdapter(adapter);
77+
}
78+
79+
private void getJSON(){
80+
class GetJSON extends AsyncTask<Void,Void,String> {
81+
82+
ProgressDialog loading;
83+
@Override
84+
protected void onPreExecute() {
85+
super.onPreExecute();
86+
loading = ProgressDialog.show(ListActivity.this,"Fetching Data","Wait...",false,false);
87+
}
88+
89+
@Override
90+
protected void onPostExecute(String s) {
91+
super.onPostExecute(s);
92+
loading.dismiss();
93+
JSON_STRING = s;
94+
showTask();
95+
}
96+
97+
@Override
98+
protected String doInBackground(Void... params) {
99+
RequestHandler rh = new RequestHandler();
100+
String s = rh.sendGetRequest(App.URL_GET_ALL);
101+
return s;
102+
}
103+
}
104+
GetJSON gj = new GetJSON();
105+
gj.execute();
106+
}
107+
108+
@Override
109+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
110+
Intent intent = new Intent(this, UpdateActivity.class);
111+
HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
112+
String taskId = map.get(App.TAG_ID).toString();
113+
intent.putExtra(App.ID,taskId);
114+
startActivity(intent);
115+
}
116+
}

app/src/main/java/com/deepagi/androidtodolist/MainActivity.java

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.deepagi.androidtodolist;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStream;
7+
import java.io.OutputStreamWriter;
8+
import java.io.UnsupportedEncodingException;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.net.URLEncoder;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import javax.net.ssl.HttpsURLConnection;
16+
17+
public class RequestHandler {
18+
19+
//Method to send httpPostRequest
20+
//This method is taking two arguments
21+
//First argument is the URL of the script to which we will send the request
22+
//Other is an HashMap with name value pairs containing the data to be send with the request
23+
public String sendPostRequest(String requestURL,
24+
HashMap<String, String> postDataParams) {
25+
URL url;
26+
27+
//StringBuilder object to store the message retrieved from the server
28+
StringBuilder sb = new StringBuilder();
29+
try {
30+
//Initializing Url
31+
url = new URL(requestURL);
32+
33+
//Creating an httmlurl connection
34+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
35+
36+
//Configuring connection properties
37+
conn.setReadTimeout(15000);
38+
conn.setConnectTimeout(15000);
39+
conn.setRequestMethod("POST");
40+
conn.setDoInput(true);
41+
conn.setDoOutput(true);
42+
43+
//Creating an output stream
44+
OutputStream os = conn.getOutputStream();
45+
46+
//Writing parameters to the request
47+
//We are using a method getPostDataString which is defined below
48+
BufferedWriter writer = new BufferedWriter(
49+
new OutputStreamWriter(os, "UTF-8"));
50+
writer.write(getPostDataString(postDataParams));
51+
52+
writer.flush();
53+
writer.close();
54+
os.close();
55+
int responseCode = conn.getResponseCode();
56+
57+
if (responseCode == HttpsURLConnection.HTTP_OK) {
58+
59+
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
60+
sb = new StringBuilder();
61+
String response;
62+
//Reading server response
63+
while ((response = br.readLine()) != null){
64+
sb.append(response);
65+
}
66+
}
67+
68+
} catch (Exception e) {
69+
e.printStackTrace();
70+
}
71+
return sb.toString();
72+
}
73+
74+
public String sendGetRequest(String requestURL){
75+
StringBuilder sb =new StringBuilder();
76+
try {
77+
URL url = new URL(requestURL);
78+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
79+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
80+
81+
String s;
82+
while((s=bufferedReader.readLine())!=null){
83+
sb.append(s+"\n");
84+
}
85+
}catch(Exception e){
86+
}
87+
return sb.toString();
88+
}
89+
90+
public String sendGetRequestParam(String requestURL, String id){
91+
StringBuilder sb =new StringBuilder();
92+
try {
93+
URL url = new URL(requestURL+id);
94+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
95+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
96+
97+
String s;
98+
while((s=bufferedReader.readLine())!=null){
99+
sb.append(s+"\n");
100+
}
101+
}catch(Exception e){
102+
}
103+
return sb.toString();
104+
}
105+
106+
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
107+
StringBuilder result = new StringBuilder();
108+
boolean first = true;
109+
for (Map.Entry<String, String>entry : params.entrySet()) {
110+
if (first)
111+
first = false;
112+
else
113+
result.append("&");
114+
115+
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
116+
result.append("=");
117+
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
118+
}
119+
120+
return result.toString();
121+
}
122+
}

0 commit comments

Comments
 (0)