我有以下代码以JSON格式从URL中检索数据并更新ListView。代码工作得很好。在xml布局中,我有两个TextView和一个ImageView。
如何以动态方式更新ImageView?
我没有从URL获取图像,我的图像存储在我的项目(res / drawable文件夹)中。 var TAG_ICON的图像名称与我项目中图像的名称完全相同。
示例:
来自JSON的回复:TAG_ICON = lamp01图片名称:lamp01.png
这是我的主要课程:
public class DeviceList extends ListActivity {
private static String url = "http://192.168.10.2/myhome/get_all_devices.php";
// Hashmap ListView
ArrayList<HashMap<String, String>> deviceList = new ArrayList<HashMap<String, String>>();
// class JSON
JSONParser jParser = new JSONParser();
// Criar JSON Nodes
private static final String TAG_DEVICES = "devices";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_ICON = "icon";
// array JSONArray
JSONArray devices = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device_list);
// Loat ListView
new LoadDevices().execute();
ListView lv = getListView();
}
// class LoadDevices
class LoadDevices extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
}
protected String doInBackground(String... args) {
// JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// add to array
devices = json.getJSONArray(TAG_DEVICES);
// Looping trough all results
for(int i = 0; i < devices.length(); i++){
JSONObject c = devices.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_DESCRIPTION);
String icon = c.getString(TAG_ICON);
// creating a HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_ICON, icon);
deviceList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url){
runOnUiThread(new Runnable() {
public void run() {
// update JSON ListView
ListAdapter adapter = new SimpleAdapter(DeviceList.this, deviceList,R.layout.device_row,
new String[]{
TAG_NAME,
TAG_DESCRIPTION,
},
new int[] {
R.id.device_row_TextViewName,
R.id.device_row_TextViewDescription,
});
// update listView
setListAdapter(adapter);
}
});
}
}
}
这是我的单行XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/device_row_RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="@+id/device_row_LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/device_row_ImageViewIcon"
android:contentDescription="@string/app_name"
android:layout_width="60dip"
android:layout_height="60dip"
android:src="@drawable/lamp03" />
</LinearLayout>
<TextView
android:id="@+id/device_row_TextViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/device_row_RelativeLayout"
android:layout_marginTop="5dip"
android:layout_marginLeft="75dip"
android:layout_toRightOf="@+id/device_row_ImageViewIcon"
android:text="Lâmpada do Quarto"
android:textColor="#4169E1"
android:textSize="20dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/device_row_TextViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/device_row_TextViewName"
android:layout_marginBottom="5dip"
android:layout_alignLeft="@+id/device_row_TextViewName"
android:paddingTop="1dip"
android:layout_marginRight="15dip"
android:layout_centerHorizontal="true"
android:text="Usado para controlar a lâmpada do quarto."
android:textColor="#343434"
android:textSize="13dip" />
</RelativeLayout>
答案 0 :(得分:0)
如果图像位于可绘制文件夹中,则需要先使用标题在可绘制文件夹中查找图像。确保文件的标题与从服务器返回的标题标记相同。
int imageId = getResources().getIdentifier("yourpackagename:drawable/" + TAG_TITLE, null, null);
然后找到ImageView并设置图像
ImageView picture = (ImageView)findViewById(R.id.device_row_ImageViewIcon);
picture.setImageResource(imageId);