augustine
augustine

Reputation: 1022

Android:listview item problem?

Hi I created a list view and Item,The item contains 2 linear layouts and i want to visible these layout on the ,depends on values ,i tried to do this in getview of adapter but its not working prperly my code shows below
Item.xml

<LinearLayout android:id="@+id/LinearLayout00"
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:gravity="fill_horizontal"
    android:hapticFeedbackEnabled="true"
    android:layout_gravity="fill_horizontal"
    android:alwaysDrawnWithCache="true" android:background="#ccc"
    android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" android:orientation="horizontal"
        android:gravity="fill_horizontal"
        android:hapticFeedbackEnabled="true"
        android:layout_gravity="fill_horizontal"
        android:alwaysDrawnWithCache="true" android:background="#ccc"
        android:layout_height="wrap_content">
        <ImageView android:id="@+id/image"
            android:layout_width="50dip" android:layout_height="50dip"
            android:src="@drawable/stub" android:scaleType="centerCrop"
            android:background="#000000" android:layout_margin="5dip" />
        <LinearLayout android:id="@+id/LinearLayout01"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_horizontal"
            android:layout_width="fill_parent" android:orientation="vertical"
            android:paddingLeft="5dip" android:paddingRight="5dip">
            <TextView android:id="@+id/text" android:text="Username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" android:layout_gravity="left|center_vertical"
                android:textColor="#111" />
            <TextView android:layout_height="wrap_content"
                android:id="@+id/Story" android:text="Story"
                android:layout_width="fill_parent" android:textColor="#111"
                android:textColorHighlight="#06f" android:textStyle="bold"
                android:typeface="sans" android:textSize="15dip" />
            <TextView android:layout_height="wrap_content"
                android:id="@+id/Time" android:text="12 hours ago"
                android:layout_width="fill_parent" android:textColor="#333"
                android:textSize="11dip" android:paddingBottom="5dip" />
            <TextView android:layout_height="wrap_content"
                android:id="@+id/Pid" android:visibility="gone"
                android:layout_width="fill_parent" android:textSize="0dip" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:layout_width="fill_parent" android:orientation="vertical"
        android:visibility="gone" android:paddingLeft="5dip"

        android:paddingRight="5dip">
        <TextView android:layout_height="wrap_content"
            android:id="@+id/follows" android:text="12 hours ago"
            android:layout_width="fill_parent" android:textColor="#333"
            android:textSize="11dip" android:paddingBottom="5dip" />
        <TextView android:layout_height="wrap_content"
            android:id="@+id/followData" android:text="Click"
            android:clickable="true" android:layout_width="fill_parent"
            android:textColor="#333" android:textSize="11dip"
            android:paddingBottom="5dip" />
    </LinearLayout>
</LinearLayout>

java file

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

    View vi=convertView;
    ViewHolder holder;

    Post o=listItems1.get(position);

    if(convertView==null){

        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();

        LinearLayout ly1=(LinearLayout)vi.findViewById(R.id.LinearLayout01);
        LinearLayout ly2=(LinearLayout)vi.findViewById(R.id.LinearLayout02);
        if(o.getPostType().equals("0"))
        {
            ly1.setVisibility(View.VISIBLE);
            ly2.setVisibility(View.GONE);
        }
        else
        {
            ly2.setVisibility(View.VISIBLE);
            ly1.setVisibility(View.GONE);
        }
        //ly.setVisibility(View.VISIBLE);

        holder.text=(TextView)vi.findViewById(R.id.text);
        holder.story=(TextView)vi.findViewById(R.id.Story);
        holder.timeSpan=(TextView)vi.findViewById(R.id.Time);
        holder.image=(ImageView)vi.findViewById(R.id.image);
        holder.pid=(TextView)vi.findViewById(R.id.Pid);

        holder.follows=(TextView)vi.findViewById(R.id.follows);
        holder.followsData=(TextView)vi.findViewById(R.id.followData);

        vi.setTag(holder);

    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText(o.getUserName());
    holder.story.setText(Html.fromHtml(o.getStory()));
    holder.timeSpan.setText(o.getTime());
    holder.pid.setText(""+o.getPid());

    holder.image.setTag(listItemsUrl.get(position).toString());
    imageLoader.DisplayImage(listItemsUrl.get(position).toString(), activity, holder.image);

    return vi;

}

please help me
Regard Augustine

Upvotes: 2

Views: 1083

Answers (1)

rekaszeru
rekaszeru

Reputation: 19220

You need to set the visibility of those views regardless of the value of the convertView parameter.
Since it is recycled, might (and most likely does) hold other value than the one given to it right now.

So you should remove the part

LinearLayout ly1=(LinearLayout)vi.findViewById(R.id.LinearLayout01);
LinearLayout ly2=(LinearLayout)vi.findViewById(R.id.LinearLayout02);
if(o.getPostType().equals("0"))
{
    ly1.setVisibility(View.VISIBLE);
    ly2.setVisibility(View.GONE);
}
else
{
    ly2.setVisibility(View.VISIBLE);
    ly1.setVisibility(View.GONE);
}

from the if clause, and place after it.

You should also take a look at Romain Guy's series about Android layout tricks, to see how your layout could be less a waste of views.

Upvotes: 2

Related Questions