如何在列表视图android中删除列表视图项?

时间:2015-02-13 11:36:58

标签: android android-layout

在我的应用程序中,我有一个自定义列表视图。每当我单击关闭图像列表项时,必须使用空格删除列表项布局。我使用visibility:gone属性,列表项布局已删除,但空间可见。我在滚动视图中使用listview,这也没有用....请帮帮我。

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.recharge_listitem, null);


        TextView accountnumber = (TextView) convertView
                .findViewById(R.id.accountnumber);
        TextView servicename = (TextView) convertView
                .findViewById(R.id.servicetypname);
        TextView planamount = (TextView) convertView
                .findViewById(R.id.rechargeamount);
      final ImageView close = (ImageView) convertView
                .findViewById(R.id.crosse_close_img);
       final LinearLayout layoutrecharge=(LinearLayout)convertView.findViewById(R.id.rechargelay);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutrecharge.setVisibility(View.GONE);
            }
        });


        accountnumber.setText("Rs."+bpData.get(position).getAccountNumber());
        servicename.setText(bpData.get(position).getServicename());
        planamount.setText(bpData.get(position).getRechargeamount());





        return convertView;
    }

}

的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"

        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">




    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1e365a">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:text="PAYMENT"
            android:textColor="#ffffff"
            android:textSize="20dp" />


    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="10dp"
            android:text="Your Recharges"
            android:textColor="#1e365a"
            android:textSize="20dp" />


    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="4dp"
        android:background="@drawable/dottedline_view" />


    <ListView

        android:id="@+id/recharge_lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">

    </ListView>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.8dp"

        android:layout_marginTop="12dp"
        android:background="#1e365a" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:text="Amount to be Paid"
            android:textColor="#1e365a"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:text="Rs.130"
            android:textColor="#1e365a"
            android:textSize="20dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:weightSum="100" >

        <EditText
            android:id="@+id/ev_b_dob"


            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="50"
            android:hint="Enter coupn code"
            android:inputType="text" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/bt_apply"
            style="@style/payment_button_style"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginLeft="-5dp"
            android:layout_weight="20"
            android:gravity="center"
            android:text="Apply" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="28dp"
        android:layout_marginRight="30dp">

        <CheckBox
            android:id="@+id/cb_reload_cash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pay using Reload Cash"
            android:textColor="#1e365a"
            android:textSize="20dp"
            android:layout_marginLeft="12dp"/>

    </LinearLayout>
    <Button
        android:id="@+id/atmdebit_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="#F93249"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="ATM/DEBIT/CREDIT"
        android:textColor="#ffffff"
        android:textSize="20dp"/>
    <Button
        android:id="@+id/netbankng_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="#F93249"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="NET BANKING"
        android:textColor="#ffffff"
        android:textSize="20dp"/>





</LinearLayout>
</ScrollView>
    </LinearLayout>

请帮帮我。

自定义列表视图项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rechargelay">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">


            <TextView
                android:id="@+id/accountnumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="8008505106" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="(" />

            <TextView
                android:id="@+id/servicetypname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vodafone" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=")" />
        </LinearLayout>

        <TextView
            android:id="@+id/rechargeamount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RS.30"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/crosse_close_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:layout_weight="1" />


    </LinearLayout>


</LinearLayout>

2 个答案:

答案 0 :(得分:0)

设置View.GONE的可见性SCROLL VIEW或列表视图的其他父级....

答案 1 :(得分:0)

为View设置View.GONE不是从ListView中删除行的正确方法。删除行从适配器数据源中删除选择行项目并调用适配器的通知数据更改方法以填充更改:

close.setTag(position);
close.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View v) {
           int deletePos=Integer.parseInt(v.getTag().toString());
            bpData.remove(deletePos);
            notifyDataSetChanged();
   }
 });