1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| package admin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jordan.map.R;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import JSON.JSONParser;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AdminCommentFragment extends Fragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> commentsList;
// url to get all comments list
private static String url_all_comments = "http://10.0.2.2/get_all_comments.php";
private static String url_comments_reported = "http://10.0.2.2/get_comments_reported.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COMMENTS = "comments";
private static final String TAG_ID = "id";
private static final String TAG_CONTENT = "content";
private static final String TAG_LOGIN = "login";
// comments JSONArray
JSONArray comment = null;
private Button comments = null, reportedComments = null;
private TextView managementOfComments = null;
private View rootView;
private ListView lv;
public AdminCommentFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = inflater.inflate(R.layout.admin_comment, container, false);
comments = (Button) rootView.findViewById(R.id.comments);
comments.setText(" Comments ");
comments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new LoadAllComments().execute();
}
});
reportedComments = (Button) rootView
.findViewById(R.id.reportedcomments);
reportedComments.setText(" Reported comments ");
reportedComments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new LoadReportedComments().execute();
}
});
lv = (ListView) rootView.findViewById(android.R.id.list);
managementOfComments = (TextView) rootView
.findViewById(R.id.managementofcomments);
managementOfComments.setText("Management of comments");
return rootView;
}
/**
* Background Async Task to Load all comments by making HTTP Request
* */
class LoadAllComments extends AsyncTask<String, String, String> {
/**
* 1 Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
commentsList.clear();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading comments. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All comments from url
* */
protected String doInBackground(String... args) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_comments, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All comments: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// comments found
// Getting Array of Comments
comment = json.getJSONArray(TAG_COMMENTS);
// looping through All Comments
for (int i = 0; i < comment.length(); i++) {
JSONObject c = comment.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String content = c.getString(TAG_CONTENT);
String login = c.getString(TAG_LOGIN);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_CONTENT, content);
map.put(TAG_LOGIN, login);
// adding HashList to ArrayList
commentsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(getActivity(),
commentsList, R.layout.list_comment, new String[] {
TAG_ID, TAG_CONTENT, TAG_LOGIN },
new int[] { R.id.id, R.id.content, R.id.login });
lv.setAdapter(adapter);
}
});
}
}
/**
* Background Async Task to Load all comments by making HTTP Request
* */
class LoadReportedComments extends AsyncTask<String, String, String> {
/**
* 1 Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
commentsList.clear();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading comments. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All comments from url
* */
protected String doInBackground(String... args) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_comments_reported,
"GET", params);
// Check your log cat for JSON reponse
Log.d("Reported comments: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// comments found
// Getting Array of Comments
comment = json.getJSONArray(TAG_COMMENTS);
// looping through All Comments
for (int i = 0; i < comment.length(); i++) {
JSONObject c = comment.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String content = c.getString(TAG_CONTENT);
String login = c.getString(TAG_LOGIN);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_CONTENT, content);
map.put(TAG_LOGIN, login);
// adding HashList to ArrayList
commentsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(getActivity(),
commentsList, R.layout.list_comment, new String[] {
TAG_ID, TAG_CONTENT, TAG_LOGIN },
new int[] { R.id.id, R.id.content, R.id.login });
lv.setAdapter(adapter);
}
});
}
}
} |
Partager