2013-03-28 14 views
0

我是初學者,請幫幫我。我很困惑。 我想用數據庫調用我的圖像,但我不知道如何。請幫幫我。 我的方法是這樣的。當文本視圖也在變化時,我想自動更改圖像視圖的圖像在ANDROID數據庫中調用圖像

 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); 

問題是我在數據庫中的問題。它只是一個文本。而當問題出現時,圖像也在變化。

+3

我不知道你實際上在問,是什麼問題,你的目標是什麼? –

+0

我的問題是圖像犯規當我想改變它時改變。如果文本是「Aso」,則圖像需要是「Aso.png」而不是其他圖像 – immaBeans

回答

1

嗯,我認爲你想改變ImageView的內容,只要你的TextView的文本更改。但我不確定,所以讓我知道如果我錯了。

從您發佈的代碼看來,這隻會運行一次。相反,你應該將它變成類似方法:

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); 
} 

現在只需撥打updateImages()每當你在TextView中調用setText()之後。

你也可以指定一個TextWatcher到的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(); 
    } 
}); 
+0

Sir Raghav,它不適合我。圖像不會改變。 – immaBeans

+0

嘗試用qImg.getText()。toString()替換所有的qImg.equals。等於 –

+0

whooo酷人它工作....我可以稱讚你或什麼。非常感謝。這是一個很大的幫助:) – immaBeans