Harshil Pansare
Harshil Pansare

Reputation: 1147

Adjusting size of ListView according to objects in ListAdapter

I have a listview which contents of different post. My current post limit per page load is 25. Each post is of different size. I want ListView size upto those 25 objects, i.e., it should accomodate all the objects from listadapter. I have currently set my listview size as following.

<ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="5000sp" >
</ListView>

My ListAdapter is set as:

ListAdapter adapter = new SimpleAdapter(NewsFeed.this, contactList,
                R.layout.list_item, new String[] { TAG_MESSAGE,
                        TAG_CREATETIME }, new int[] { R.id.message,
                        R.id.time });
        setListAdapter(adapter);

list_item.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/time"
        android:gravity="right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#b1b1b1"
        android:background="#abc0e3" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/cover" />

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:text="TextView"
        android:linksClickable="true"
android:autoLink="web" />

    </LinearLayout>

Upvotes: 1

Views: 1238

Answers (2)

faljbour
faljbour

Reputation: 1377

see if this works for you, where you adjust the width then you can decide on what width to use based on the text length,

/* create an inline class that defines each item in the list
  private class Post 
  {
    String time;
    String message;
    Bitmap bmp;
    public String getTime(){return time;}
    public String getMessage(){return message;}
    public Bitmap getBitmap(){return bmp;}
  } 

  //** populate your array with each item in list 
  ArrayList<Post> array = new ArrayList<Post>();
  Post p = new Post();
  //* ..............

  ListAdapter  adapter = new ListAdapter (this, array);
  listView.setAdapter(adapter);

  private class ListAdapter  extends BaseAdapter 
  {
    Context context;
    ArrayList<Post> contactList;
    public ListAdapter(Context context, ArrayList<Post> contactList) 
    {
      this.context = context;
      this.contactList = contactList; 
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
      try
      {
          if(convertView==null)
          {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_item, parent, false);
          }  
          ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
          TextView timeview = (TextView) convertView.findViewById(R.id.time);
          TextView msgview = (TextView) convertView.findViewById(R.id.message);

          Post  post = getItem(position);
          imageView.setImageBitmap(post.getBitmap());
          timeView.setText(post.getTime());
          messageView.setText(post.getMessage());

          int width = convertView.getWidth();
          int height = convertView.getHeight();

          int messageWidth = post.getMessage().length();

          //* adjust the width based on your message width
          //* width = ........ do something here
          //* set the desired width here
          convertView.setLayoutParams(new LayoutParams(width, height));

        return convertView;
      }
      catch(Exception e)
      {
      }
      return null;
    }
    @Override
    public int getCount() 
    {
      return contactList.size();
    }

    @Override
    public Post getItem(int position) 
    {
        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
       return 0;
    }
  }

Upvotes: 1

Arshid KV
Arshid KV

Reputation: 10037

Change code in list_item.xml

android:layout_width="wrap_content" to fill_parent or match_parent

Upvotes: 0

Related Questions