3

I implemented a listview with 2 kinds of rows from here listViews with multiple rows tutorial

all is working properly,

the problem that I have now is setting the height for the rows,

if I leave the background as a color, all is good,

but if I use an image for background, I only get a tall row [on other simple list views the height respect the programatically set height], but in this case, the row changes

so

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/category_item_row"
  android:orientation="horizontal" 
  android:layout_width="fill_parent"
  android:background="#ff00ff"
  android:layout_height="70dp"
  android:padding="3dp"
 >

<ImageView
 android:id="@+id/image"
 android:layout_width="40dp"
 android:layout_height="70dp"
 android:gravity="center_vertical"/>

<TextView
 android:id="@+id/title"
 android:textSize="20sp"
 android:layout_width="90dp"
 android:layout_height="70dp"
 android:gravity="center_vertical"/>
</LinearLayout>

works fine,

but

android:background="@drawable/phone_cat_cell">

is not respecting the assigned height,

how to fix this height using my image background?

thanks!

edit 1, adapter

listViewItem.setAdapter(new PhoneItemAdapter(new ItemPhoneDataSource().getItems()));

private class PhoneItemAdapter extends BaseAdapter {  
    final List<RowPhone> rows;//row

    //data source, style
    PhoneItemAdapter(List<ItemPhone> animals) {
        rows = new ArrayList<RowPhone>();//member variable

        //choose cell! iterator
        for (ItemPhone item : animals) {
            //if it has an image, use an ImageRow
            if (item.getImageId() != null) {
                rows.add(new RowFolderPhone(LayoutInflater.from(getActivity()), item)); //imageRow!
            } else {//otherwise use a DescriptionRow

                rows.add(new RowItemPhone(LayoutInflater.from(getActivity()), item));                   
            }
        }
    }

    //delegate
    @Override
    public int getViewTypeCount() {
        return RowTypePhone.values().length;
    }

    @Override
    public int getItemViewType(int position) {
        //con cast
        return ((RowPhone) rows.get(position)).getViewType();

    }

    public int getCount() {
        return rows.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        //cambiado con cast!
        return ((RowPhone) rows.get(position)).getView(convertView);
    }
   }
3
  • Can you post your adapter method?
    – Beginner
    Sep 19, 2013 at 5:34
  • @Beginner I placed the adapter thanks Sep 19, 2013 at 5:45
  • you want set a different background for each row.
    – Yugesh
    Sep 19, 2013 at 6:30

3 Answers 3

3

Check your adapter code, if you are passing null while inflating the view then please replace that code with this.

view = inflater.inflate(R.layout.listview_row, parent, false);
2
  • hi , thanks, I tried // ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, null); ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, (ViewGroup) view, false); but doesnt work either, tnx Sep 19, 2013 at 5:58
  • Hi, In custom list view I have to change image size dynamically, for every row there are different size of image need to display, imageview height will not change, I have try above code, It not working for me, is there any other solution ? Dec 12, 2013 at 7:18
2

you just need to make not static images, but "9patch" images. All android versions are supporting that. Have a look here http://developer.android.com/tools/help/draw9patch.html

2
2

Try this:

<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="70dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:gravity="center_vertical"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.