【问题标题】:Change ImageView Android:src with a string使用字符串更改 ImageView Android:src
【发布时间】:2016-10-03 18:42:44
【问题描述】:

我有一个正在返回的字符串,它是图像的位置,因为它会出现在 xml 布局文件中,所以“@drawable/image”。

我的问题是可以从代码中更改 android:src="@drawable/image" 中的字符串吗?

编辑:

        <ImageView
        android:id="@+id/imageuni"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/no_image"/> <!--Change this part in code-->

我正在使用带有 CustomBinder 的 SimpleAdapter:

        SimpleAdapter adapter = new SimpleAdapter( SearchActivity.this,leaderList, R.layout.search_entry, new String[] {
                "rankId","name","location","image"}, new int[] {R.id.rankId,R.id.name,R.id.location,R.id.imageuni});
        adapter.setViewBinder(new CustomViewBinder());
        setListAdapter(adapter);

绑定代码:

class CustomViewBinder implements SimpleAdapter.ViewBinder {
    public boolean setViewValue(View view, Object inputData, String textRepresentation) {
    int id = view.getId();
    String data = (String) inputData;
    switch (id) {
        case R.id.imageuni:
            populateImage(view, data);
            break;

        case R.id.location:
            populateLocation(view, data);
            break;

        case R.id.name:
            populateName(view, data);
            break;

    }
    return true;
}
public void populateImage(View view, String imageData) {
    ImageView uniImage = (ImageView) view.findViewById(R.id.imageuni);
    uniImage.setImageDrawable(Drawable.createFromPath(imageData));
}

public void populateLocation(View view, String data) {
    TextView locationTxt = (TextView) view.findViewById(R.id.location);
    locationTxt.setText(data);
}

public void populateName(View view, String data) {
    TextView dateTxt = (TextView) view.findViewById(R.id.name);
    dateTxt.setText(data);
}

}

我正在获取行项目的 ID 并构建一个字符串来获取图像的名称,例如第一张图像名为 u1.jpg,它位于我的可绘制 res 文件夹中。

【问题讨论】:

标签: android


【解决方案1】:

你在找这个吗?

ImageView img = (ImageView) findViewById(R.id.img);
img.setImageResource(R.drawable.u1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多