1 year ago

#346481

test-img

Latife Turan

How to set an question image to ImageView from Firebase in QuizApp

I am working on a quiz app basically. In my app there are many questions getting datas from Firebase Database with those areas (question, optionA,optionB,optionC,optionD and correctANS) but I want to improve my project. I want to add image questions instead of text questions. For this I added an area(urlImage) to my database on Firebase. Normally I can get data from Firebase except urlImage but I didn't see Image in ImageView in my app. What should i do? Please someone help me?

those are my QuestionsActivity codes:

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        question=findViewById(R.id.question);
        urlImage=findViewById(R.id.urlImage);
        noIndicator=findViewById(R.id.no_indicator);
        bookmarkBtn=findViewById(R.id.bookmark_btn);
        optionsContainer=findViewById(R.id.option_container);
        shareBtn=findViewById(R.id.share_btn);
        nextBtn=findViewById(R.id.next_btn);

        preferences=getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor=preferences.edit();
        gson=new Gson();

        getBookmarks();
        bookmarkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (modelMatch()){
                    bookmarkslist.remove(matchedQuestionPosition);
                    bookmarkBtn.setImageDrawable(getDrawable(R.drawable.bookmark_border));
                }else{
                    bookmarkslist.add(list.get(position));
                    bookmarkBtn.setImageDrawable(getDrawable(R.drawable.bookmark));
                }
            }
        });


        setId=getIntent().getStringExtra("setId");

        loadingDialog=new Dialog(this);
        loadingDialog.setContentView(R.layout.loading);
        loadingDialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.rounded_corner2));
        loadingDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        loadingDialog.setCancelable(false);

        list=new ArrayList<>();
        loadingDialog.show();
        myRef.child("SETS").child(setId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
                    String id=dataSnapshot1.getKey();
                    String question= dataSnapshot1.child("question").getValue().toString();
                    String a= dataSnapshot1.child("optionA").getValue().toString();
                    String b= dataSnapshot1.child("optionB").getValue().toString();
                    String c= dataSnapshot1.child("optionC").getValue().toString();
                    String d= dataSnapshot1.child("optionD").getValue().toString();
                    String correctANS= dataSnapshot1.child("correctANS").getValue().toString();
                    String urlImage= dataSnapshot1.child("urlImage").getValue().toString();

                    list.add(new QuestionModel(id,question,a,b,c,d,correctANS,urlImage,setId));

                }
                if (list.size() > 0){
                    for (int i=0;i<4;i++){
                        optionsContainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                checkhanswer(((Button)view));
                            }
                        });
                    }
                    playAnim(question,0,list.get(position).getQuestion());
                    
                    nextBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            nextBtn.setEnabled(false);
                            nextBtn.setAlpha(0.7f);
                            enableOption(true);
                            position++;
                            if (position==list.size()){
                                Intent scoreIntent=new Intent(QuestionsActivity.this,ScoreActivity.class);
                                scoreIntent.putExtra("score",score);
                                scoreIntent.putExtra("total",list.size());
                                startActivity(scoreIntent);
                                finish();
                                return;
                            }
                            count=0;
                            playAnim(question,0,list.get(position).getQuestion());
                        }
                    });
                }else{
                    finish();
                    Toast.makeText(QuestionsActivity.this, "There isn't any question.", Toast.LENGTH_SHORT).show();
                }
                loadingDialog.dismiss();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(QuestionsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                loadingDialog.dismiss();
                finish();
            }
        });
//        playAnim(question,0,list.get(position).getQuestion());

    }

    @Override
    protected void onPause() {
        super.onPause();
        storeBookmarks();
    }

    private void playAnim(final View view, final int value, final String data){
//        for (int i=0;i<4;i++){
//            optionsContainer.getChildAt(i).setBackgroundTintList(null);
//        }
        view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500).setStartDelay(100)
                .setInterpolator(new DecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                if (value == 0 && count<4){
                    String option="";
                    if (count==0){
                        option=list.get(position).getA();
                    }
                    else if(count==1){
                        option=list.get(position).getB();
                    }
                    else if(count==2){
                        option=list.get(position).getC();
                    }
                    else if(count==3){
                        option=list.get(position).getD();
                    }
                    playAnim(optionsContainer.getChildAt(count),0,option);

                    count++;
                }
            }
            @Override
            public void onAnimationEnd(Animator animator) {
                ((TextView)view).setText(data);

                if (value==0){
                    try {
                        ((TextView)view).setText(data);
                        noIndicator.setText(position+1+"/"+list.size());
                        if (modelMatch()){

                            bookmarkBtn.setImageDrawable(getDrawable(R.drawable.bookmark));
                        }else{

                            bookmarkBtn.setImageDrawable(getDrawable(R.drawable.bookmark_border));
                        }
                    }catch (ClassCastException e){
                        ((Button)view).setText(data);
                    }
                    view.setTag(data);
                    playAnim(view,1,data);
//                    playAnim(urlResim,0,list.get(position).getUrlResim());
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

    }







those are my QuestionModel codes:

public class QuestionModel {
    private String id,question,A,B,C,D,answer,set,urlImage;

    public QuestionModel(String id, String question, String a, String b, String c, String d, String answer,
                         String set,String urlImage) {
        this.id = id;
        this.question = question;
        A = a;
        B = b;
        C = c;
        D = d;
        this.answer = answer;
        this.set = set;
        this.urlImage=urlImage;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getA() {
        return A;
    }

    public void setA(String a) {
        A = a;
    }

    public String getB() {
        return B;
    }

    public void setB(String b) {
        B = b;
    }

    public String getC() {
        return C;
    }

    public void setC(String c) {
        C = c;
    }

    public String getD() {
        return D;
    }

    public void setD(String d) {
        D = d;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getSet() {
        return set;
    }

    public void setSet(String set) {
        this.set = set;
    }

    public String getUrlImage() {
        return urlImage;
    }

    public void setUrlImage(String urlResim) {
        this.urlImage = urlImage;
    }


}

java

android

firebase

firebase-realtime-database

imageview

0 Answers

Your Answer

Accepted video resources