我有一个包含以下布局的列表:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/outName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/outSurname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/outAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/outPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/outPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我使用异步任务从JSON格式的BD中检索我的字段,并在onPostExecute函数中填充列表,如下所示:
// Updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
MyActivity.this, MyList,
R.layout.my_layout, new String[] { “name”, “surname”, “address”, "phone" },
new int[] { R.id.outName, R.id.outSurname, R.id.outAddress, R.id.outPhone });
// Updating ListView
setListAdapter(adapter);
}
});
}
}
其中MyList是一个ArrayList:
ArrayList<HashMap<String, String>> MyList;
MyList = new ArrayList<HashMap<String, String>>();
我的结果的每个字段JSONArray由“姓名”,“姓氏”,“地址”,&#34;电话&#34;和&#34;照片&#34;组成。 &#34;相片&#34;是一个具有4个可能值(0,1,2,3)的数字字段,其中每个值都指我在可绘制文件夹上的特定图像。 如何根据此值设置ImageView的图像?
答案 0 :(得分:0)
您需要编写适配器。根据这个做出自己的想法。
public class ListAdapter extends BaseAdapter{
Context context;
ArrayList<HashMap<String, String>> myList;
public BankAdapter(Context context,ArrayList<HashMap<String, String>> myList){
this.context = context;
this.myList=myList;
}
static class ViewHolder {
// Include Number of reqd views.
private TextView item;
private ImageView pic;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return items[pos];
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
String id = items[pos].getId();
return Long.parseLong(id);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder mViewHolder = null;
if(view == null){
mViewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Activity.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listrow,parent,false);
mViewHolder.item = (TextView)view.findViewById(R.id.items_txt);
mViewHolder.pic=(ImageView)view.findViewById(R.id.pics_img)
view.setTag(mViewHolder);
}
else{
mViewHolder = (ViewHolder)view.getTag();
}
mViewHolder.item.setText(myList.get(position).getKey());
if(condition){
//update Image with correct item.
mViewHolder.pic.setDrawable(//getitemfromList);
}
return view;
}
}