Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project.anyPare;
- import java.util.ArrayList;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import static project.anyPare.ProjectConstant.*;
- public class ForumPage extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.forum);
- // Filter spinner
- Spinner filter = (Spinner) findViewById(R.id.filter);
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filter_array,
- android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- filter.setAdapter(adapter);
- // action when selected the filter
- filter.setOnItemSelectedListener(new FilterOnItemSelectedListener());
- ArrayList<SearchResults> searchResults = GetSearchResults();
- final ListView lv1 = (ListView) findViewById(R.id.forumListView);
- lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
- lv1.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> a, View v, int position,
- long id) {
- // if(position == 0){
- // final Intent intent = new Intent().setClass(ForumPage.this, TopicPage.class);
- // startActivity(intent);
- // }
- Object o = lv1.getItemAtPosition(position);
- SearchResults fullObject = (SearchResults) o;
- Toast.makeText(ForumPage.this, "You have chosen item: " + fullObject.getId(), Toast.LENGTH_LONG).show();
- final Intent topic = new Intent().setClass(ForumPage.this, TopicPage.class);
- topic.putExtra("id", fullObject.getId());
- startActivity(topic);
- }
- });
- }
- private ArrayList<SearchResults> GetSearchResults() {
- ArrayList<SearchResults> results = new ArrayList<SearchResults>();
- ArrayList<String> id = new ArrayList<String>();
- ArrayList<String> category = new ArrayList<String>();
- ArrayList<String> title = new ArrayList<String>();
- ArrayList<String> username = new ArrayList<String>();
- ArrayList<String> timestamp = new ArrayList<String>();
- SearchResults sr1 = new SearchResults();
- JSONArray jArray = SQLConnection.getTopicFromMySQL(READ_DB);
- //String[][] result = new String[10][5];
- JSONObject json_data = null;
- try {
- for (int i = 0; i < jArray.length(); i++) {
- json_data = jArray.getJSONObject(i);
- id.add(json_data.getString("id"));
- category.add(json_data.getString("category"));
- title.add(json_data.getString("title"));
- username.add(json_data.getString("username"));
- timestamp.add(json_data.getString("timestamp"));
- // result[i][0] = json_data.getString("id");
- // result[i][1] = json_data.getString("category");
- // result[i][2] = json_data.getString("title");
- // result[i][3] = json_data.getString("username");
- // result[i][4] = json_data.getString("timestamp");
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- for (int i = 0; i < id.size(); i++) {
- sr1 = new SearchResults();
- sr1.setId(id.get(i));
- sr1.setCategory(category.get(i));
- sr1.setTitle(title.get(i));
- sr1.setUser(username.get(i));
- sr1.setTime(timestamp.get(i));
- results.add(sr1);
- }
- // sr1.setCategory("Clothes");
- // sr1.setTitle("Help me choose this 2 shirts");
- // sr1.setUser("Robin");
- // sr1.setTime("14 Aug 2011 12:10");
- // results.add(sr1);
- //
- // sr1 = new SearchResults();
- // sr1.setCategory("Clothes");
- // sr1.setTitle("2 jeans!!");
- // sr1.setUser("Alex");
- // sr1.setTime("10 Aug 2011 15:05");
- // results.add(sr1);
- //
- // sr1 = new SearchResults();
- // sr1.setCategory("Shoes");
- // sr1.setTitle("Nike or Adidas");
- // sr1.setUser("Gobin");
- // sr1.setTime("5 Aug 2011 5:14");
- // results.add(sr1);
- //
- // sr1 = new SearchResults();
- // sr1.setCategory("Accessories");
- // sr1.setTitle("Which one?");
- // sr1.setUser("Thunder");
- // sr1.setTime("3 Aug 2011 21:55");
- // results.add(sr1);
- return results;
- }
- // This is the class that we'll be filling with our data, and loading into an ArrayList.
- public class SearchResults {
- private String id = "";
- private String category = "";
- private String title = "";
- private String user = "";
- private String time = "";
- public void setId(String id) {
- this.id = id;
- }
- public String getId() {
- return id;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- public String getCategory() {
- return category;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getUser() {
- return user;
- }
- public void setTime(String time) {
- this.time = time;
- }
- public String getTime() {
- return time;
- }
- }
- // Custom Adapter
- public class MyCustomBaseAdapter extends BaseAdapter {
- private ArrayList<SearchResults> searchArrayList;
- private LayoutInflater mInflater;
- public MyCustomBaseAdapter(Context context,
- ArrayList<SearchResults> results) {
- searchArrayList = results;
- mInflater = LayoutInflater.from(context);
- }
- public int getCount() {
- return searchArrayList.size();
- }
- public Object getItem(int position) {
- return searchArrayList.get(position);
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.forum_list, null);
- holder = new ViewHolder();
- holder.txtCategory = (TextView) convertView.findViewById(R.id.forumCategory);
- holder.txtTitle = (TextView) convertView.findViewById(R.id.forumTitle);
- holder.txtUser = (TextView) convertView
- .findViewById(R.id.forumByUser);
- holder.txtTime = (TextView) convertView
- .findViewById(R.id.forumTime);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.txtCategory.setText("[" + searchArrayList.get(position).getCategory() + "] ");
- holder.txtTitle.setText(searchArrayList.get(position).getTitle());
- holder.txtUser.setText(searchArrayList.get(position)
- .getUser());
- holder.txtTime.setText(searchArrayList.get(position).getTime());
- return convertView;
- }
- class ViewHolder {
- TextView txtCategory;
- TextView txtTitle;
- TextView txtUser;
- TextView txtTime;
- }
- }
- }
Add Comment
Please, Sign In to add comment