Android Open Source - EasyVote Answer Question Detail Fragment






From Project

Back to project page EasyVote.

License

The source code is released under:

Apache License

If you think the Android project EasyVote 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 com.llanox.mobile.easyvote;
// www.  jav a 2s.co m
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.backendless.Backendless;
import com.backendless.BackendlessCollection;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.async.callback.BackendlessCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.persistence.BackendlessDataQuery;
import com.llanox.mobile.easyvote.model.AnswerQuestion;
import com.llanox.mobile.easyvote.model.Question;
import com.llanox.mobile.easyvote.model.User;

/**
 * A fragment representing a single AnswerQuestion detail screen. This fragment
 * is either contained in a {@link AnswerQuestionListActivity} in two-pane mode
 * (on tablets) or a {@link AnswerQuestionDetailActivity} on handsets.
 */
public class AnswerQuestionDetailFragment extends Fragment {
  /**
   * The fragment argument representing the item ID that this fragment
   * represents.
   */
  public static final String ARG_ITEM_ID = "item_id";
  public static final String TAG = AnswerQuestionDetailFragment.class.getSimpleName();
  
  private Question mItem;
  private Spinner spAnswer;
  private Button btnSendAnswer;
  private View mRootView;
  
  /**
   * Mandatory empty constructor for the fragment manager to instantiate the
   * fragment (e.g. upon screen orientation changes).
   */
  public AnswerQuestionDetailFragment() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
  
    mRootView = inflater.inflate(R.layout.fragment_answerquestion_detail, container, false);

    Context ctx = this.getActivity().getBaseContext();
    String idItem = getArguments().getString(ARG_ITEM_ID);
    
    validateQuestionStatus(idItem);
  
    btnSendAnswer = (Button) mRootView.findViewById(R.id.btn_send_answer);  
    spAnswer = (Spinner) mRootView.findViewById(R.id.spn_answers);

    
    
    btnSendAnswer.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        sendAnswer();

      }
    });

    return mRootView;
  }

  private void validateQuestionStatus(String idItem) {

    User user = getCurrentUserSession();
    
    
    String whereClause = "id like '"+idItem+"'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause( whereClause );
    
    Backendless.Persistence.of( Question.class ).find( dataQuery, 
                new AsyncCallback<BackendlessCollection<Question>>(){

          @Override
          public void handleFault(BackendlessFault fault) {
          
            Toast.makeText(AnswerQuestionDetailFragment.this.getActivity(),R.string.toast_err_retrieving_data, Toast.LENGTH_LONG).show();
            
          }

          @Override
          public void handleResponse(  BackendlessCollection<Question> response) {
            
            if(response.getData()!= null && response.getData().size()>0){
               foundQuestion(response.getData().get(0));          
            }
      }} );
    
    
    
           

    
    
  
    
  
     
    
  }

  protected void foundQuestion(Question question) {
    
    
    
    
       mItem = question;
      if (mItem != null) {
        ((TextView) mRootView.findViewById(R.id.question_title))
            .setText(mItem.getContent());
      }
      
      User user = getCurrentUserSession();
       String  whereClause = "questionId like '"+question.getObjectId()+"' and voter like '"+user.getUsername()+"'";
        
       BackendlessDataQuery  dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause( whereClause );
        
        Backendless.Persistence.of( AnswerQuestion.class ).find( dataQuery, 
                    new AsyncCallback<BackendlessCollection<AnswerQuestion>>(){

              @Override
              public void handleFault(BackendlessFault fault) {
              
                Toast.makeText(AnswerQuestionDetailFragment.this.getActivity(),R.string.toast_err_retrieving_data, Toast.LENGTH_LONG).show();
                
              }

              @Override
              public void handleResponse(  BackendlessCollection<AnswerQuestion> response) {
                
                if(response.getData()!= null && response.getData().size()>0){
                 
                   foundAnswerQuestion(response.getData().get(0));          
                }
          }} );  
    
  }

  protected void foundAnswerQuestion(AnswerQuestion answerQuestion) {
    
    btnSendAnswer.setEnabled(false);
    
    if (mItem != null) {
      ((TextView) mRootView.findViewById(R.id.question_title))
          .setText(mItem.getContent() );
    }
    
    String answer = answerQuestion.getAnswer();
    
    int count = spAnswer.getAdapter().getCount();
    
    for(int i =0; i<= count;i++){
      
      String iAnswer = spAnswer.getAdapter().getItem(i).toString();
      if(iAnswer.equalsIgnoreCase(answer)){
        spAnswer.setSelection(i,true);
        break;
      }
      
    }
    
    
    
    
  }

  protected void sendAnswer() {
    
    
    String answer = spAnswer.getSelectedItem().toString();

    AnswerQuestion answerQuestion = new AnswerQuestion();
        User user = getCurrentUserSession();
        
    answerQuestion.setVoter(user);
    answerQuestion.setAnswer(answer);
    
  
    
    Backendless.Persistence.save(answerQuestion,
        new BackendlessCallback<AnswerQuestion>() {
          @Override
          public void handleResponse(  AnswerQuestion answerQuestion) {
            Log.i("", "Question answered by "+ answerQuestion.getVoter());
            Toast.makeText(AnswerQuestionDetailFragment.this.getActivity(), R.string.toast_msg_answered_question, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(AnswerQuestionDetailFragment.this.getActivity(),AnswerQuestionListActivity.class);
            
            AnswerQuestionDetailFragment.this.getActivity().startActivity(intent);
            AnswerQuestionDetailFragment.this.getActivity().finish();
            
            
          }
        });
    
  }

  private User getCurrentUserSession() {
    
    User user = new User();
    user.setId(AppSessionManager.getUserID(this.getActivity()));
  
    return user;
  }

}




Java Source Code List

com.llanox.mobile.easyvote.AnswerQuestionDetailActivity.java
com.llanox.mobile.easyvote.AnswerQuestionDetailFragment.java
com.llanox.mobile.easyvote.AnswerQuestionListActivity.java
com.llanox.mobile.easyvote.AnswerQuestionListFragment.java
com.llanox.mobile.easyvote.AppCredentials.java
com.llanox.mobile.easyvote.AppSessionManager.java
com.llanox.mobile.easyvote.AskQuestion.java
com.llanox.mobile.easyvote.ConstantsEasyVote.java
com.llanox.mobile.easyvote.EntryPointActivity.java
com.llanox.mobile.easyvote.PlusBaseActivity.java
com.llanox.mobile.easyvote.QuestionDetailActivity.java
com.llanox.mobile.easyvote.QuestionDetailFragment.java
com.llanox.mobile.easyvote.QuestionListActivity.java
com.llanox.mobile.easyvote.QuestionListFragment.java
com.llanox.mobile.easyvote.data.DataException.java
com.llanox.mobile.easyvote.data.DataLayerManager.java
com.llanox.mobile.easyvote.data.DataSession.java
com.llanox.mobile.easyvote.data.QuestionData.java
com.llanox.mobile.easyvote.data.UserData.java
com.llanox.mobile.easyvote.data.util.DateTimeUtils.java
com.llanox.mobile.easyvote.model.AnswerQuestion.java
com.llanox.mobile.easyvote.model.Question.java
com.llanox.mobile.easyvote.model.User.java