Skip to content

Instantly share code, notes, and snippets.

@bnasim
Created January 7, 2016 22:01
Show Gist options
  • Save bnasim/a284dc907f11be31a97e to your computer and use it in GitHub Desktop.
Save bnasim/a284dc907f11be31a97e to your computer and use it in GitHub Desktop.
package com.mifos.mifosxdroid.online;
import com.mifos.mifosxdroid.R;
import com.mifos.mifosxdroid.adapters.SurveyPagerAdapter;
import com.mifos.objects.client.Client;
import com.mifos.objects.survey.QuestionDatas;
import com.mifos.objects.survey.ResponseDatas;
import com.mifos.objects.survey.Scorecard;
import com.mifos.objects.survey.Survey;
import com.mifos.services.data.ScorecardPayload;
import com.mifos.utils.MifosApplication;
import com.mifos.utils.Constants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.os.Bundle;
import java.util.Vector;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.content.SharedPreferences;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import retrofit.Callback;
import android.view.View;
import retrofit.RetrofitError;
import retrofit.client.Response;
import com.mifos.services.data.SaveSurveyResponse;
import com.mifos.utils.MyPreference;
/**
* Created by Nasim Banu on 15-12-15.
*/
public class SurveyQuestion extends FragmentActivity
{
private ViewPager pager = null;
ActionBarActivity activity;
private PagerAdapter mPagerAdapter = null;
public static final String ANSWERS = "answers";
public static final String ID = "id";
public static int responseId;
public static int responseValue;
public static int qid;
private List<Fragment> fragments = null;
private String qs;
public LinkedHashMap<String,ArrayList<String>> QuestionAndAnswerMap;
private String[] answer;
private String[] ans;
MyPreference myPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_survey_question);
myPreference = new MyPreference();
myPreference.resetFavorite(this);
// Creation de la liste de Fragments que fera d�filer le PagerAdapter
fragments = new Vector<Fragment>();
Intent mIntent = getIntent();
int surveyId = mIntent.getIntExtra("SurveyId", 0);
((MifosApplication)this.getApplication()).api.surveyService.getSurvey(surveyId, new Callback<Survey>() {
@Override
public void success(final Survey survey, Response response) {
if (survey != null) {
String answer[] = new String[7];
ArrayList<String> stockList = new ArrayList<String>();
if (survey.getQuestionDatas() != null && survey.getQuestionDatas().size() > 0) {
for (int i = 0; i < survey.getQuestionDatas().size(); i++) {
qs = survey.getQuestionDatas().get(i).getText();
qid = survey.getQuestionDatas().get(i).getQuestionId();
if (survey.getQuestionDatas().get(i).getResponseDatas().size() > 0) {
for (int j = 0; j < survey.getQuestionDatas().get(i).getResponseDatas().size(); j++) {
answer[j] = survey.getQuestionDatas().get(i).getResponseDatas().get(j).getText();
// newRow = new String[] {answer[j]};
stockList.add(answer[j]);
// QuestionAndAnswerMap.put("What", stockList);
}
}
String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);
fragments.add(SurveyQuestionFragment.newInstance(i, qs, stockArr));
stockList.clear();
}
fragments.add(SurveyLastFragment.newInstance(1, "Thanks for taking the Survey"));
mPagerAdapter.notifyDataSetChanged();
}
}
}
@Override
public void failure(RetrofitError retrofitError) {
Toast.makeText(SurveyQuestion.this, "Survey not found.", Toast.LENGTH_SHORT).show();
}
});
// Cr�ation de l'adapter qui s'occupera de l'affichage de la liste de
// Fragments
this.mPagerAdapter = new SurveyPagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (ViewPager) super.findViewById(R.id.checkInPager);
// Affectation de l'adapter au ViewPager
pager.setAdapter(this.mPagerAdapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// log.i("Displayed Questions page " + position);
// mPagerAdapter.setCurrentPage(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// log.i("Answer captured on page " + position);
// mPagerAdapter.setCurrentPage(position);
}
@Override public void onPageScrollStateChanged(int state) {
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
}
});
Button btnNext = (Button) findViewById(R.id.btnNext );
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int current = pager.getCurrentItem();
if (current < fragments.size())
pager.setCurrentItem(current+1, true );
}});
Button btnPrev = (Button) findViewById(R.id.btnPrevious);
btnPrev.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int current = pager.getCurrentItem();
if (current >= 0)
pager.setCurrentItem(current-1, true );
}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment