2

Hey there, i have one question..

i try to write an android app with java.

i have a hashmaplist(1), saved in a list(2). to show the list(2) entrys(those, who saved in hashmap(1)), i use a ListAdapter object.

everything works fine, except of the following: one of the entrys, saved in the hashmaplist(1), is "country", like 'at', 'de', 'gb', ... now i want to change a pics' source, that was created on the layout, depends on the country entry.

here some code..

save entrys and show it with a ListAdapter object:

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);

Now the Layout 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>

Any ideas?

Prexx
  • 2,959
  • 5
  • 31
  • 48

4 Answers4

3

you have to create your own view binder like

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();
                   .....

            }
        }
}

and in your activity use your simpleAdapter as

        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());

Hope this will help u

Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
2

So I made it work like this.

This is the simple adapter:

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);

This is the binder:

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;

    }
}

And these are the methods I called:

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);
}
Marius Hilarious
  • 855
  • 9
  • 18
0

Grab the ImageView and use setImageDrawable or setImageResource ?

ImageView mImageView = (ImageView) getViewById(R.id.imageView10);
mImageView.setImageResource(R.drawable.gb);

And use some kind of switch case to pick the right drawable? Sample switch-case (you would put the setImageResource call inside the switch cases for the countries in question):

    switch (item.getItemId()) {
    case R.id.about:
        displayAbout();
        return true;
    case R.id.help:
        displayHelp();
        return true;
Codemonkey
  • 3,412
  • 3
  • 20
  • 24
  • yes, i just had the same idea. but that means, that i have to go through the whole list again and set the icons, right? – Prexx Mar 03 '11 at 09:51
0

your are implementing default SimpleAdapter you should customize this adapter by overriding

getView() method and set image src. eg

 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;
    }