Android Open Source - openaccessbutton Questions Activity






From Project

Back to project page openaccessbutton.

License

The source code is released under:

MIT License

If you think the Android project openaccessbutton listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openaccessbutton.openaccessbutton.advocacy;
//from   ww  w. j  a v  a2s  .  c o  m
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.goebl.david.Webb;
import com.parse.ParseInstallation;

import org.json.JSONArray;
import org.json.JSONObject;
import org.openaccessbutton.openaccessbutton.R;
import org.openaccessbutton.openaccessbutton.about.AboutActivity;
import org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity;
import org.w3c.dom.Text;

import java.util.ArrayList;

public class QuestionsActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LoadQuestionsTask task = new LoadQuestionsTask(this);
        task.execute();

    }

    protected class QuestionsArrayAdapter extends ArrayAdapter<Question> {
        private final Context context;
        private final Question[] questions;

        public QuestionsArrayAdapter(Context context, Question[] questions) {
            super(context, R.layout.question_list_item, questions);
            this.context = context;
            this.questions = questions;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.question_list_item, parent, false);

            TextView questionView = (TextView) rowView.findViewById(R.id.question_question);
            TextView answerView = (TextView) rowView.findViewById(R.id.question_answer);
            questionView.setText(questions[position].question());
            answerView.setText(questions[position].answer(getResources().getString(R.string.answerPlaceholder)));

            return rowView;
        }
    }

    protected class LoadQuestionsTask extends AsyncTask<Void, Void, Question[]> {
        private Context mContext;
        private ProgressDialog mProgress;

        public LoadQuestionsTask(Context context) {
            mContext = context;
        }

        @Override
        protected void onPreExecute() {
            // Show progress dialog
            mProgress = new ProgressDialog(mContext);
            mProgress.setTitle("Loading");
            mProgress.setMessage("Please wait...");
            mProgress.show();
        }

        @Override
        protected Question[] doInBackground(Void... params) {
            // User ID from Parse so we can tie back questions to this specific installation
            String user_id;
            ParseInstallation installation = ParseInstallation.getCurrentInstallation();
            if (installation == null) {
                user_id = "";
            } else {
                user_id = installation.getObjectId();
            }

            Webb webb = Webb.create();
            JSONObject result = webb
                    .get("http://oabuttonquestions.herokuapp.com/questions.json")
                    .param("user_id", user_id)
                    .ensureSuccess()
                    .asJsonObject()
                    .getBody();


            Question[] questions;

            try {
                JSONArray questionsJson = result.getJSONArray("questions");
                questions = new Question[questionsJson.length()];
                for(int i=0; i<questionsJson.length(); i++) {
                    JSONObject questionJson = questionsJson.getJSONObject(i);
                    Question question = new Question(questionJson.getString("question"), questionJson.getString("answer"));
                    questions[i] = question;
                }

            } catch (Exception e) {
                questions = null;
                e.printStackTrace();
                // TODO Do something here
            }

            return questions;
        }

        @Override
        protected void onPostExecute(Question[] questions) {
            mProgress.dismiss();

            if (questions == null || questions.length == 0) {
                questions = new Question[] {new Question(getResources().getString(R.string.noQuestionsTitle), getResources().getString(R.string.noQuestionsDescription)) };
            }

            QuestionsArrayAdapter adapter = new QuestionsArrayAdapter(mContext, questions);
            setListAdapter(adapter);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.questions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            // Open up AppPreferencesActivity
            Intent k = new Intent(this, AppPreferencesActivity.class);
            startActivity(k);
            return true;
        } else if (item.getItemId() == R.id.action_about) {
            // Open up AboutActivity
            Intent k = new Intent(this, AboutActivity.class);
            startActivity(k);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




Java Source Code List

com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.openaccessbutton.openaccessbutton.ApplicationTest.java
org.openaccessbutton.openaccessbutton.MainActivity.java
org.openaccessbutton.openaccessbutton.NavigationItem.java
org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
org.openaccessbutton.openaccessbutton.OnFragmentNeededListener.java
org.openaccessbutton.openaccessbutton.OnShareIntentInterface.java
org.openaccessbutton.openaccessbutton.about.AboutActivity.java
org.openaccessbutton.openaccessbutton.advocacy.AdvocacyFragment.java
org.openaccessbutton.openaccessbutton.advocacy.FaqView.java
org.openaccessbutton.openaccessbutton.advocacy.Question.java
org.openaccessbutton.openaccessbutton.advocacy.QuestionsActivity.java
org.openaccessbutton.openaccessbutton.advocacy.XmlParser.java
org.openaccessbutton.openaccessbutton.api.API.java
org.openaccessbutton.openaccessbutton.blog.BlogAdapter.java
org.openaccessbutton.openaccessbutton.blog.BlogDetailsFragment.java
org.openaccessbutton.openaccessbutton.blog.BlogFragment.java
org.openaccessbutton.openaccessbutton.blog.DownloadTask.java
org.openaccessbutton.openaccessbutton.blog.Post.java
org.openaccessbutton.openaccessbutton.blog.RssParser.java
org.openaccessbutton.openaccessbutton.browser.BrowserFragment.java
org.openaccessbutton.openaccessbutton.browser.ScrollingWebView.java
org.openaccessbutton.openaccessbutton.button.ButtonSubmitActivity.java
org.openaccessbutton.openaccessbutton.intro.ForgotPasswordActivity.java
org.openaccessbutton.openaccessbutton.intro.IntroActivity.java
org.openaccessbutton.openaccessbutton.intro.LaunchActivity.java
org.openaccessbutton.openaccessbutton.intro.ScreenSlidePageFragment.java
org.openaccessbutton.openaccessbutton.intro.SigninActivity.java
org.openaccessbutton.openaccessbutton.intro.SigninButtonClickListener.java
org.openaccessbutton.openaccessbutton.intro.SignupActivity.java
org.openaccessbutton.openaccessbutton.intro.SignupEmailButtonClickListener.java
org.openaccessbutton.openaccessbutton.map.Item.java
org.openaccessbutton.openaccessbutton.map.MapFragment.java
org.openaccessbutton.openaccessbutton.menu.MenuActivity.java
org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity.java
org.openaccessbutton.openaccessbutton.push.PushReceiver.java
org.openaccessbutton.openaccessbutton.push.Push.java