我尝试用java编写一个Android应用程序。
我有一个hashmaplist(1),保存在列表中(2)。 要显示list(2)entrys(那些保存在hashmap(1)中的人),我使用ListAdapter对象。
一切正常,但以下情况除外: 其中一个保存在hashmaplist(1)中的是“country”,如'at','de','gb',... 现在我想要更改在布局上创建的图片源,取决于国家/地区条目。
这里有一些代码..
使用ListAdapter对象保存entrys并显示它:
for (int i = 0; i < itemArray.length(); ++i) {
JSONObject rec = itemArray.getJSONObject(i);
//save entrys in hashmap
map = new HashMap<String, String>();
map.put("name", rec.getString("name"));
map.put("country", rec.getString("country"));
//save hashmap entry in list
mylist.add(map);
}
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listitem,
new String[] {"name"}, new int[] {R.id.area});
listview.setAdapter(mSchedule);
现在布局XML:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/be">
</ImageView>
<TextView
android:id="@+id/area"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
有什么想法吗?
答案 0 :(得分:3)
您必须创建自己的视图绑定器,如
class CustomViewBinder extends SimplerAdapter.ViewBinder
{
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
int id=view.getId();
String country=(String)data;
switch(id)
{
case R.id.country:
if(country.equals("us")
setYourImage();
.....
}
}
}
并在您的活动中 使用simpleAdapter作为
SimplerAdapter sa=new SimpleAdapter(this, mylist, R.layout.listitem,
new String[] {"name","country"}, new int[] {R.id.area,r.id.country});
sa.setViewBinder(new CustomViewBinder());
希望这会对你有所帮助
答案 1 :(得分:2)
所以我让它像这样工作。
这是简单的适配器:
SimpleAdapter sa = new SimpleAdapter(ctx, activityList, R.layout.activity_list_item, new String[] { TAG_TITLE,
TAG_LAT, TAG_DATE, TAG_IMAGE_DATA }, new int[] { R.id.activity_title, R.id.location_title,
R.id.date_title, R.id.activity_picture });
sa.setViewBinder(new CustomViewBinder());
setListAdapter(sa);
这是活页夹:
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.activity_picture:
populateImage(view, data);
break;
case R.id.location_title:
populateLocation(view, data);
break;
case R.id.date_title:
populateDate(view, data);
break;
}
return true;
}
}
这些是我称之为的方法:
public void populateImage(View view, String imageData) {
try {
Log.i("JSON Image Data", "string " + imageData);
byte[] imageAsBytes = null;
try {
imageAsBytes = Base64.decode(imageData.getBytes());
} catch (IOException e1) {
e1.printStackTrace();
}
ImageView image = (ImageView) view.findViewById(R.id.activity_picture);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageAsBytes, 0,
imageAsBytes.length));
image.setBackgroundDrawable(d);
Drawable tappedPicture = getResources().getDrawable(R.drawable.tapped_picture);
Bitmap tappedPictureBitmap = ((BitmapDrawable) (tappedPicture)).getBitmap();
image.setImageBitmap(tappedPictureBitmap);
} catch (Exception e) {
}
}
public void populateLocation(View view, String data) {
TextView locationTxt = (TextView) view.findViewById(R.id.location_title);
locationTxt.setText(data);
}
public void populateDate(View view, String data) {
TextView dateTxt = (TextView) view.findViewById(R.id.date_title);
dateTxt.setText(data);
}
答案 2 :(得分:0)
抓住ImageView并使用setImageDrawable
或setImageResource
?
ImageView mImageView = (ImageView) getViewById(R.id.imageView10);
mImageView.setImageResource(R.drawable.gb);
并使用某种开关盒来挑选合适的抽屉?示例switch-case(你可以将setImageResource调用放在有问题国家的switch case中):
switch (item.getItemId()) {
case R.id.about:
displayAbout();
return true;
case R.id.help:
displayHelp();
return true;
答案 3 :(得分:0)
您正在实现默认的SimpleAdapter,您应该通过覆盖
来自定义此适配器getView()方法并设置图像src。例如
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.newsrow, null);
}
Item item = itemsList.get(position);
if (item != null) {
TextView nt = (TextView) convertView.findViewById(R.id.NewsTitle);
ImageView nth = (ImageView) convertView.findViewById(R.id.NewsThumbnail);
nt.setText("abc");
loadImage(item.getThumbnail(), nth);
}
return convertView;
}