Guest User

Untitled

a guest
Dec 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. package project.anyPare;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.os.Bundle;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.AdapterView;
  17. import android.widget.AdapterView.OnItemClickListener;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.BaseAdapter;
  20. import android.widget.ListView;
  21. import android.widget.Spinner;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24. import static project.anyPare.ProjectConstant.*;
  25.  
  26. public class ForumPage extends Activity {
  27.  
  28.  
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.forum);
  33.  
  34. // Filter spinner
  35. Spinner filter = (Spinner) findViewById(R.id.filter);
  36. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filter_array,
  37. android.R.layout.simple_spinner_item);
  38. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  39. filter.setAdapter(adapter);
  40. // action when selected the filter
  41. filter.setOnItemSelectedListener(new FilterOnItemSelectedListener());
  42.  
  43. ArrayList<SearchResults> searchResults = GetSearchResults();
  44.  
  45. final ListView lv1 = (ListView) findViewById(R.id.forumListView);
  46. lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
  47.  
  48. lv1.setOnItemClickListener(new OnItemClickListener() {
  49. @Override
  50. public void onItemClick(AdapterView<?> a, View v, int position,
  51. long id) {
  52. // if(position == 0){
  53. // final Intent intent = new Intent().setClass(ForumPage.this, TopicPage.class);
  54. // startActivity(intent);
  55. // }
  56. Object o = lv1.getItemAtPosition(position);
  57. SearchResults fullObject = (SearchResults) o;
  58. Toast.makeText(ForumPage.this, "You have chosen item: " + fullObject.getId(), Toast.LENGTH_LONG).show();
  59. final Intent topic = new Intent().setClass(ForumPage.this, TopicPage.class);
  60. topic.putExtra("id", fullObject.getId());
  61. startActivity(topic);
  62. }
  63. });
  64. }
  65.  
  66. private ArrayList<SearchResults> GetSearchResults() {
  67. ArrayList<SearchResults> results = new ArrayList<SearchResults>();
  68. ArrayList<String> id = new ArrayList<String>();
  69. ArrayList<String> category = new ArrayList<String>();
  70. ArrayList<String> title = new ArrayList<String>();
  71. ArrayList<String> username = new ArrayList<String>();
  72. ArrayList<String> timestamp = new ArrayList<String>();
  73.  
  74. SearchResults sr1 = new SearchResults();
  75. JSONArray jArray = SQLConnection.getTopicFromMySQL(READ_DB);
  76. //String[][] result = new String[10][5];
  77. JSONObject json_data = null;
  78. try {
  79. for (int i = 0; i < jArray.length(); i++) {
  80. json_data = jArray.getJSONObject(i);
  81. id.add(json_data.getString("id"));
  82. category.add(json_data.getString("category"));
  83. title.add(json_data.getString("title"));
  84. username.add(json_data.getString("username"));
  85. timestamp.add(json_data.getString("timestamp"));
  86. // result[i][0] = json_data.getString("id");
  87. // result[i][1] = json_data.getString("category");
  88. // result[i][2] = json_data.getString("title");
  89. // result[i][3] = json_data.getString("username");
  90. // result[i][4] = json_data.getString("timestamp");
  91. }
  92. } catch (JSONException e) {
  93. // TODO Auto-generated catch block
  94. e.printStackTrace();
  95. }
  96. for (int i = 0; i < id.size(); i++) {
  97. sr1 = new SearchResults();
  98. sr1.setId(id.get(i));
  99. sr1.setCategory(category.get(i));
  100. sr1.setTitle(title.get(i));
  101. sr1.setUser(username.get(i));
  102. sr1.setTime(timestamp.get(i));
  103. results.add(sr1);
  104. }
  105.  
  106. // sr1.setCategory("Clothes");
  107. // sr1.setTitle("Help me choose this 2 shirts");
  108. // sr1.setUser("Robin");
  109. // sr1.setTime("14 Aug 2011 12:10");
  110. // results.add(sr1);
  111. //
  112. // sr1 = new SearchResults();
  113. // sr1.setCategory("Clothes");
  114. // sr1.setTitle("2 jeans!!");
  115. // sr1.setUser("Alex");
  116. // sr1.setTime("10 Aug 2011 15:05");
  117. // results.add(sr1);
  118. //
  119. // sr1 = new SearchResults();
  120. // sr1.setCategory("Shoes");
  121. // sr1.setTitle("Nike or Adidas");
  122. // sr1.setUser("Gobin");
  123. // sr1.setTime("5 Aug 2011 5:14");
  124. // results.add(sr1);
  125. //
  126. // sr1 = new SearchResults();
  127. // sr1.setCategory("Accessories");
  128. // sr1.setTitle("Which one?");
  129. // sr1.setUser("Thunder");
  130. // sr1.setTime("3 Aug 2011 21:55");
  131. // results.add(sr1);
  132.  
  133. return results;
  134. }
  135.  
  136. // This is the class that we'll be filling with our data, and loading into an ArrayList.
  137. public class SearchResults {
  138. private String id = "";
  139. private String category = "";
  140. private String title = "";
  141. private String user = "";
  142. private String time = "";
  143.  
  144. public void setId(String id) {
  145. this.id = id;
  146. }
  147.  
  148. public String getId() {
  149. return id;
  150. }
  151.  
  152. public void setCategory(String category) {
  153. this.category = category;
  154. }
  155.  
  156. public String getCategory() {
  157. return category;
  158. }
  159.  
  160. public void setTitle(String title) {
  161. this.title = title;
  162. }
  163.  
  164. public String getTitle() {
  165. return title;
  166. }
  167.  
  168. public void setUser(String user) {
  169. this.user = user;
  170. }
  171.  
  172. public String getUser() {
  173. return user;
  174. }
  175.  
  176. public void setTime(String time) {
  177. this.time = time;
  178. }
  179.  
  180. public String getTime() {
  181. return time;
  182. }
  183. }
  184.  
  185. // Custom Adapter
  186. public class MyCustomBaseAdapter extends BaseAdapter {
  187. private ArrayList<SearchResults> searchArrayList;
  188.  
  189. private LayoutInflater mInflater;
  190.  
  191. public MyCustomBaseAdapter(Context context,
  192. ArrayList<SearchResults> results) {
  193. searchArrayList = results;
  194. mInflater = LayoutInflater.from(context);
  195. }
  196.  
  197. public int getCount() {
  198. return searchArrayList.size();
  199. }
  200.  
  201. public Object getItem(int position) {
  202. return searchArrayList.get(position);
  203. }
  204.  
  205. public long getItemId(int position) {
  206. return position;
  207. }
  208.  
  209. public View getView(int position, View convertView, ViewGroup parent) {
  210. ViewHolder holder;
  211. if (convertView == null) {
  212. convertView = mInflater.inflate(R.layout.forum_list, null);
  213. holder = new ViewHolder();
  214. holder.txtCategory = (TextView) convertView.findViewById(R.id.forumCategory);
  215. holder.txtTitle = (TextView) convertView.findViewById(R.id.forumTitle);
  216. holder.txtUser = (TextView) convertView
  217. .findViewById(R.id.forumByUser);
  218. holder.txtTime = (TextView) convertView
  219. .findViewById(R.id.forumTime);
  220.  
  221. convertView.setTag(holder);
  222. } else {
  223. holder = (ViewHolder) convertView.getTag();
  224. }
  225.  
  226. holder.txtCategory.setText("[" + searchArrayList.get(position).getCategory() + "] ");
  227. holder.txtTitle.setText(searchArrayList.get(position).getTitle());
  228. holder.txtUser.setText(searchArrayList.get(position)
  229. .getUser());
  230. holder.txtTime.setText(searchArrayList.get(position).getTime());
  231.  
  232. return convertView;
  233. }
  234.  
  235. class ViewHolder {
  236. TextView txtCategory;
  237. TextView txtTitle;
  238. TextView txtUser;
  239. TextView txtTime;
  240. }
  241. }
  242. }
Add Comment
Please, Sign In to add comment