Android java从字符串访问URL而不是硬编码

时间:2013-03-30 05:20:14

标签: java android

我正尝试使用以下代码通过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,而不是对其进行硬编码。

1 个答案:

答案 0 :(得分:0)

在初始化setContentView()

之前尝试使用getIntent值
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);