我从 URl 解析的 json 中获取数据,并且根据 json 中的一些数据,我需要使自定义的列表视图按钮可见,否则它处于不可见状态。如果条件类似,String == "123"
那么我需要将按钮设置为可见。下面的代码将按钮设置为可见,但是当我向后滚动时,它也会出现在其他一些行中。它没有正确显示。按钮应该只出现在满足条件的行中。怎么能解决这个问题?
在我的布局中,我将按钮设置如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="70dip"
android:layout_height="70dip"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of program-->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="title Name"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- category Name -->
<TextView
android:id="@+id/subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="subtitle" />
<!-- start time -->
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/subtitle"
android:layout_toRightOf="@+id/thumbnail"
android:text="time"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<!-- Rightend button -->
<Button android:id="@+id/watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/watchnow"
android:layout_alignParentRight="true"
android:layout_below="@+id/time"
android:layout_centerVertical="true"
android:clickable="true"
android:focusable="false"/>
</RelativeLayout>
我的 getView 代码如下:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.single_row, null);
final TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.subtitle); // artist name
TextView duration = (TextView)vi.findViewById(R.id.time); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(song.get(CustomizedListView.TAG_NAME));
artist.setText(song.get(CustomizedListView.TAG_PR));
duration.setText(song.get(CustomizedListView.TAG_DES));
imageLoader.DisplayImage(song.get(CustomizedListView.TAG_PID), thumb_image);
if(duration.getText().toString().equalsIgnoreCase("123456")){
Button watch = (Button)vi.findViewById(R.id.watch);
watch.setVisibility(View.VISIBLE);
}
return vi;
}