2013-03-30 19 views
1

我試圖通過使用下面的代碼URL來訪問圖像:Android的Java的字符串中的訪問URL,而不是硬編碼

public class SingleListItem extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.details_list); 

    TextView txtProduct = (TextView) findViewById(R.id.name); 
    TextView txtLdesc = (TextView) findViewById(R.id.longdesc); 
    SmartImageView imgPreview = (SmartImageView) findViewById(R.id.preview); 

    imgPreview.setImageUrl(lpreview); 
    Intent i = getIntent(); 

    // getting attached intent data 
    String product = i.getStringExtra("title"); 
    String ldesc = i.getStringExtra("longdesc"); 
    String lpreview = i.getStringExtra("preview"); 

    // displaying selected product name 
    txtProduct.setText(product); 
    txtLdesc.setText(ldesc); 

我從另一個活動通過「預覽」的價值。這包含我想要抓取圖像的URL,但它與前一活動中的用戶選擇不同。在imgPreview.setImageUrl中,我想將其設置爲字符串lpreview,而不是對其進行硬編碼。

+0

我看錯上面的代碼中的唯一的事情是,你需要充分利用額外的字符串之後調用setImageUrl。什麼不使用它? –

+0

你已經從上一個活動中獲得了這個權利嗎?它根本沒有硬編碼。只需要將'imgPreview.setImageUrl(lpreview);'移動到這行代碼的下面:String lpreview = i.getStringExtra(「preview」);'。 – SudoRahul

+0

感謝您的答案,我現在就試試。我嘗試了早些時候的重組,並沒有奏效。我會再嘗試。那麼,工作!感謝大家!不知道我以前出錯的地方。哦,謝謝你幫助新手! – nmiltner

回答

0

試試這個,getIntent值初始化之前您的setContentView()

public class SingleListItem extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Intent i = getIntent(); 

// getting attached intent data 
String product = i.getStringExtra("title"); 
String ldesc = i.getStringExtra("longdesc"); 
String lpreview = i.getStringExtra("preview"); 
this.setContentView(R.layout.details_list); 

TextView txtProduct = (TextView) findViewById(R.id.name); 
TextView txtLdesc = (TextView) findViewById(R.id.longdesc); 
SmartImageView imgPreview = (SmartImageView) findViewById(R.id.preview); 

imgPreview.setImageUrl(lpreview); 


// displaying selected product name 
txtProduct.setText(product); 
txtLdesc.setText(ldesc); 
+0

這工作得很好。謝謝! – nmiltner

相關問題