如何在android中的listview中隐藏和显示图像?

时间:2014-10-27 09:46:01

标签: java android

我有一个列表,其中我有一个图像视图。我只需要在我的一个变量为真时才显示该图像。实际上,我创建了一个自定义适配器,其中我有一个变量" serviceAlertPresent"如果这是真的我需要显示图像我不想在行上显示图像。我将如何实现这个

这是我每行的代码。

<?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:orientation="vertical"
android:background="@android:color/transparent"
android:padding="5dp" >

    <TextView
        android:id="@+id/item_platform"
      android:layout_width="25dp"
        android:layout_height="wrap_content"

        android:layout_marginRight="16dp"

        android:text="Ptf."
        android:textAppearance="?android:attr/textAppearanceMedium" />



    <TextView
        android:id="@+id/item_schDepart"
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_platform"
        android:layout_alignBottom="@+id/item_platform"
        android:layout_toRightOf="@+id/item_platform"
        android:text="dpt"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/item_arrival"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_schDepart"
        android:layout_alignBottom="@+id/item_schDepart"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/item_expectdepart"
        android:text="Arr."
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/item_expectdepart"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/item_schDepart"
        android:layout_marginTop="14dp"
        android:layout_toRightOf="@+id/item_platform"
        android:text="Expert"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/item_expertarrival"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_expectdepart"
        android:layout_alignBottom="@+id/item_expectdepart"
        android:layout_alignLeft="@+id/item_arrival"
        android:text="exprt Arrival"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/item_stationName"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_arrival"
        android:layout_alignBottom="@+id/item_arrival"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/item_expertarrival"
        android:text="Des."
        android:ellipsize="end"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/alertStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="0dp"
        android:layout_toRightOf="@+id/item_stationName"
     android:src="@drawable/alert_icon" />

</RelativeLayout>

这是自定义适配器

public class CustomListAdapter extends BaseAdapter{
    ArrayList<deparaturedaseboarddto> deparaturedaseboarddto;
    private Context context;
    public CustomListAdapter( Context context, ArrayList<deparaturedaseboarddto> deparaturedaseboarddto){
        this.deparaturedaseboarddto=deparaturedaseboarddto;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return deparaturedaseboarddto.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return deparaturedaseboarddto.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         View v = convertView;
            if (v == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = mInflater.inflate(R.layout.row_listitem, null);
            }

            final TextView platFormName = (TextView) v
                    .findViewById(R.id.item_platform);

            final TextView schDepart = (TextView) v
                    .findViewById(R.id.item_schDepart);
            final TextView expDepart = (TextView) v
                    .findViewById(R.id.item_expectdepart);
            final TextView arrival = (TextView) v
                    .findViewById(R.id.item_arrival);
            final TextView exparrival = (TextView) v
                    .findViewById(R.id.item_expertarrival);
            final TextView stationName = (TextView) v
                    .findViewById(R.id.item_stationName);

            final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
            final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
            final String schExpectValue= deparaturedaseboarddto.get(position).getExpDepart();
            final String arrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
            final String exparrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
           String stationNameValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();

            platFormName.setText(platformValue);

            schDepart.setText(schDepartValue);
            expDepart.setText(schExpectValue);
            arrival.setText(arrivalValue);
            exparrival.setText(exparrivalValue);
            stationName.setText(stationNameValue);
            return v;
    }

    public void referhList( ArrayList<deparaturedaseboarddto> deparaturedaseboarddto){
        this.deparaturedaseboarddto=deparaturedaseboarddto;

        notifyDataSetChanged();
    }

}

我问这个图片视图

<ImageView
        android:id="@+id/alertStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="0dp"
        android:layout_toRightOf="@+id/item_stationName"
     android:src="@drawable/alert_icon" />

我需要在&#34; serviceAlertPresent&#34; 时显示此信息。我将获得类似 deparaturedaseboarddto.get(position).isServiceAlertPresent()

4 个答案:

答案 0 :(得分:2)

在适配器中获取imageView并将visibilite设置为INVISIBLE或VISIBLE。

final ImageView alertStatus = (ImageView) v
                    .findViewById(R.id.alertStatus);

if (condition){
     alertStatus.setVisibility(View.VISIBLE);
}
else{
     alertStatus.setVisibility(View.INVISIBLE);
}

当条件改变时,你应该调用你的适配器的notifyDataSetChanged()。

希望它可以帮到你!

答案 1 :(得分:1)

您可以在getView()方法本身内检查serviceAlertPresent的布尔值。

如果确实如此,请使用mImageView.setVisibility(View.VISIBLE)其他人使用mImageView.setVisibility(View.GONE)删除它的存在。

注意:当您使用GONE时,它将是不可见的,同时它不会占用任何空间,而当您使用INVISIBLE时,它将是不可见的并且将占用布局空间。

答案 2 :(得分:0)

ImageView中,默认设置为android:setVisiblity="gone";

仅在您的变量为真时显示该图像。

imageView.setVisibility(View.GONE);

答案 3 :(得分:-1)

 final ImageView alertStatus= (ImageView) v.findViewById(R.id.alertStatus);
v.alertStatus.setVisibility(View.INVISIBLE);