immaBeans
immaBeans

Reputation: 3

Calling image in database in ANDROID

Im a beginner, Please help me. Im so confused. I want to call my image using database but i dont know how. please help me. My approach is this. I want to automatically change the image of the imageview when the text view is also changing

        String question = Utility.capitalise(currentQ.getQuestion());
        TextView qImg = (TextView) findViewById(R.id.question);
        qImg.setText(question);
        ImageView yes = (ImageView) findViewById(R.id.imageQuestions);

        if (qImg.equals("aso"))
            // yes = R.drawable.aso;
            yes.setBackgroundResource(R.drawable.aso);

        else if (qImg.equals("Pusa"))
            yes.setBackgroundResource(R.drawable.pusa);
        else if (qImg.equals("Daga"))
            yes.setBackgroundResource(R.drawable.daga);

question is my questions in the database. it is just a text.And when the question is changin the image is also changing.

Upvotes: 0

Views: 146

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82533

Well, I think you want to change the ImageView contents whenever your TextView's text changes. I'm not sure however, so let me know if I got it wrong.

From the code you've posted, it seems that this will run only once. Instead, you should move this into a method like:

public void updateImages() {
    String question = Utility.capitalise(currentQ.getQuestion());
    TextView qImg = (TextView) findViewById(R.id.question);
    qImg.setText(question);
    ImageView yes = (ImageView) findViewById(R.id.imageQuestions);

    if (qImg.equals("aso"))
        yes.setBackgroundResource(R.drawable.aso);
    else if (qImg.equals("Pusa"))
        yes.setBackgroundResource(R.drawable.pusa);
    else if (qImg.equals("Daga"))
        yes.setBackgroundResource(R.drawable.daga);
}

Now simply call updateImages() right after whenever you call setText() in your TextView.

You could also assign a TextWatcher to the TextView:

qImg.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){
        YourActivity.this.updateImages();
    }
}); 

Upvotes: 1

Related Questions