0

我有一个 ListView,其中包含 1 个图像 + 2 个文本 + 1 个图像。这是我的 listview xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="20dp"
   android:gravity="left|center"
   android:layout_width="wrap_content"
   android:paddingLeft="5dp"
   android:paddingRight="5dp"
   android:background="@color/list_bg"
   >

    <ImageView
       android:id="@+id/avatar"
       android:layout_width="wrap_content"
       android:layout_height="48dp"
       android:layout_marginTop="10dp"
       android:layout_alignParentLeft="true"/>

    <LinearLayout
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="48dp"
       android:layout_toRightOf="@+id/avatar"
       android:layout_marginLeft="5dp"
       android:layout_marginTop="5dp"
       >


    <TextView android:id="@+id/listitem1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="2dp"
      android:textColor="@drawable/textblack_selected"
      android:textSize="18dp"
      android:textStyle="bold" 
      />
    <TextView android:id="@+id/listitem2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@drawable/textorange_selected"
      />

    </LinearLayout>

    <ImageView
       android:id="@+id/arrowImage"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="25dp"
       android:layout_alignParentRight="true"
       android:src="@drawable/arrow_selector"/>
</RelativeLayout>

我需要从最后一行删除最后一个图像(箭头)。在我的适配器中,我尝试使用它:

if (position == 4) lastimg.setImageResource(0);

but nothing changed.Here is my adapter:

public class UserItemAdapter extends ArrayAdapter < UserRecord > {
    private ArrayList < UserRecord > users;

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList < UserRecord > users) {
        super(context, textViewResourceId, users);
        this.users = users;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mainmenurowlist, null);
        }

        UserRecord user = users.get(position);
        if (user != null) {
            TextView listitem1 = (TextView) v.findViewById(R.id.listitem1);
            TextView listitem2 = (TextView) v.findViewById(R.id.listitem2);

            if (listitem1 != null) {
                listitem1.setText(user.listitem1);
            }

            if (listitem2 != null) {
                listitem2.setText(user.listitem2);
            }
        }

        firstimg = (ImageView) v.findViewById(R.id.avatar);
        lastimg = (ImageView) v.findViewById(R.id.arrowImage);

        firstimg.setImageResource(FirstImage[position]);
        if (position == 4) {
            lastimg.setImageResource(0);
            System.out.println("Exit!!");
        }
        return v;
    }
}

public class UserRecord {
    public String listitem1;
    public String listitem2;

    public UserRecord(String listitem1, String listitem2) {
        this.listitem1 = listitem1;
        this.listitem2 = listitem2;
    }
}
} 

有谁知道我该如何解决这个问题?提前致谢。

4

2 回答 2

2

试试这样

if(position == (data_id.size()-1))
{
  lastimg.setVisibility(View.INVISIBLE);
}

这里 data_id 只是一个数组列表

或尝试如下

if(position == 4)
{
  lastimg.setVisibility(View.INVISIBLE);
}

两者都必须相同

于 2012-06-25T09:15:49.253 回答
0

在 getView() 方法中使用这种方式

if((users.size()-1)==position){
   // do here your stuff what u want on last item of listview
 }else{
  // do here your stuff what u want on each item of listview except last item
 }
于 2012-06-25T09:14:59.163 回答