更新并删除ListView中的项目

时间:2016-07-01 23:13:06

标签: java android listview adapter

当通过弹出窗口编辑阵列列表或通过长按

删除项目时

它只是更新并删除最后一个值 arraylist和适配器以及listview未一起更新 这是我的代码:

CustomListViewAapter.java

package com.zoom.plumbingcalculation;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class CustomListViewAdapter extends BaseAdapter{

public static final String colFrom="From";
public static final String colTo="To";
public static final String colCFM="CFM";
public static final String colDia="Dia";
public static final String colLength="Length";
public static final String colTotLoss="Loss";
public static final String colDP="DP";

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFrom;
TextView txtTo;
TextView txtCFM;
TextView txtDia;
TextView txtLength;
TextView txtTotLoss;
TextView txtDP;

public CustomListViewAdapter(Activity activity, ArrayList<HashMap<String,          String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {

    return list.size();
}

@Override
public Object getItem(int position) {

    return list.get(position);
}

@Override
public long getItemId(int position) {

    return 0;
}



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

    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.custom_list_view, null);

        txtFrom=(TextView) convertView.findViewById(R.id.From);
        txtTo=(TextView) convertView.findViewById(R.id.To);
        txtCFM=(TextView) convertView.findViewById(R.id.CFM);
        txtDia=(TextView) convertView.findViewById(R.id.Dia);
        txtLength=(TextView) convertView.findViewById(R.id.Length);
        txtTotLoss=(TextView) convertView.findViewById(R.id.TotLoss);
        txtDP=(TextView) convertView.findViewById(R.id.DP);

    }

    HashMap<String, String> map=list.get(position);
    txtFrom.setText(map.get(colFrom));
    txtTo.setText(map.get(colTo));
    txtCFM.setText(map.get(colCFM));
    txtDia.setText(map.get(colDia));
    txtLength.setText(map.get(colLength));
    txtTotLoss.setText(map.get(colTotLoss));
    txtDP.setText(map.get(colDP));

    return convertView;
}
}

Activity.java

package com.zoom.plumbingcalculation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import static com.zoom.plumbingcalculation.CustomListViewAdapter.colCFM;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDP;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDia;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colFrom;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colLength;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTo;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTotLoss;



public class ps_ss_compressed_air extends Activity {

//Compressed Air Variable
private ImageButton CAButAdd;
private ListView CALvDataTable;
private ArrayList<HashMap<String, String>> CALvDataTableListItems;
private CustomListViewAdapter CALvDataTableAdapter;

//Compressed Air Popup Add Variable
private LayoutInflater layoutAddInflater;
private View popupAddView;
private PopupWindow popupAddWindow;

private Button CAPAButAdd;
private Button CAPAButCancel;
private Spinner CAPASpDia;
private EditText CAPATxtFrom;
private EditText CAPATxtTo;
private EditText CAPATxtCFM;
private EditText CAPATxtLength;

//Compressed Air Popup Edit Variable
private LayoutInflater layoutEditInflater;
private View popupEditView;
private PopupWindow popupEditWindow;

private Button CAPEButEdit;
private Button CAPEButCancel;
private Spinner CAPESpDia;
private EditText CAPETxtFrom;
private EditText CAPETxtTo;
private EditText CAPETxtCFM;
private EditText CAPETxtLength;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ps_ss_compressed_air);

    //Compressed Air Variable
    CAButAdd = (ImageButton) findViewById(R.id.CAButAdd);
    CALvDataTable =(ListView) findViewById(R.id.CALvDataTable);

    CALvDataTableListItems= new ArrayList<HashMap<String,String>>();
    CALvDataTableAdapter=new CustomListViewAdapter(this, CALvDataTableListItems);
    CALvDataTable.setAdapter(CALvDataTableAdapter);

    CAButAdd.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            layoutAddInflater = (LayoutInflater)getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            popupAddView = layoutAddInflater.inflate(R.layout.ps_ss_compressed_air_popup_add, null);
            //popupView.setAlpha(0.5F);
            popupAddView.setAlpha(1);
            popupAddWindow = new PopupWindow(
                    popupAddView,
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);

            CAPAButAdd = (Button)popupAddView.findViewById(R.id.CAPAButAdd);
            CAPAButCancel = (Button)popupAddView.findViewById(R.id.CAPAButCancel);
            CAPASpDia = (Spinner) popupAddView.findViewById(R.id.CAPASpDia);
            CAPATxtFrom = (EditText) popupAddView.findViewById(R.id.CAPATxtFrom);
            CAPATxtTo = (EditText) popupAddView.findViewById(R.id.CAPATxtTo);
            CAPATxtCFM = (EditText) popupAddView.findViewById(R.id.CAPATxtCFM);
            CAPATxtLength = (EditText) popupAddView.findViewById(R.id.CAPATxtLength);

            CAPAButAdd.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
//Add--------------------------------------------------------------------------------------------
                    HashMap<String,String> temp = new HashMap<>();
                    temp.put(colFrom, CAPATxtFrom.getText().toString());
                    temp.put(colTo, CAPATxtTo.getText().toString());
                    temp.put(colCFM, CAPATxtCFM.getText().toString());
                    temp.put(colDia, CAPASpDia.getSelectedItem().toString());
                    temp.put(colLength, CAPATxtLength.getText().toString());
                    temp.put(colTotLoss, "");
                    temp.put(colDP, "");
                    CALvDataTableListItems.add(temp);
                    CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
                    popupAddWindow.dismiss();
                }});

            CAPAButCancel.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
                    popupAddWindow.dismiss();
                }});

            //popupWindow.showAsDropDown(CAButAdd, 50, 50);
            popupAddWindow.showAtLocation(popupAddView, Gravity.CENTER,0,0);
            popupAddWindow.setFocusable(true);
            popupAddWindow.update();

        }});

    CALvDataTable.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent,  View view, final int position, long id)
        {
            layoutEditInflater = (LayoutInflater)getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            popupEditView = layoutEditInflater.inflate(R.layout.ps_ss_compressed_air_popup_edit, null);
            popupEditView.setAlpha(1);
            popupEditWindow = new PopupWindow(
                    popupEditView,
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);

            CAPEButEdit = (Button)popupEditView.findViewById(R.id.CAPEButEdit);
            CAPEButCancel = (Button)popupEditView.findViewById(R.id.CAPEButCancel);
            CAPESpDia = (Spinner) popupEditView.findViewById(R.id.CAPESpDia);
            CAPETxtFrom = (EditText) popupEditView.findViewById(R.id.CAPETxtFrom);
            CAPETxtTo = (EditText) popupEditView.findViewById(R.id.CAPETxtTo);
            CAPETxtCFM = (EditText) popupEditView.findViewById(R.id.CAPETxtCFM);
            CAPETxtLength = (EditText) popupEditView.findViewById(R.id.CAPETxtLength);


            HashMap<String, String> temp=CALvDataTableListItems.get(position);
            CAPETxtFrom.setText(temp.get(colFrom));
            CAPETxtTo.setText(temp.get(colTo));
            CAPETxtCFM.setText(temp.get(colCFM));
            CAPESpDia.setSelection(((ArrayAdapter<String>)CAPESpDia.getAdapter()).getPosition(temp.get(colDia)));
            CAPETxtLength.setText(temp.get(colLength));

            CAPEButEdit.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
//Edit-------------------------------------------------------------------------------------------
                    HashMap<String,String> temp= CALvDataTableListItems.get(position);
                    temp.put(colFrom, CAPETxtFrom.getText().toString());
                    temp.put(colTo, CAPETxtTo.getText().toString());
                    temp.put(colCFM, CAPETxtCFM.getText().toString());
                    temp.put(colDia, CAPESpDia.getSelectedItem().toString());
                    temp.put(colLength, CAPETxtLength.getText().toString());
                    temp.put(colTotLoss, "");
                    temp.put(colDP, "");
                    CALvDataTableListItems.set(position,temp);
                    CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
                    popupEditWindow.dismiss();
                }});

            CAPEButCancel.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
                    popupEditWindow.dismiss();
                }});

            //popupWindow.showAsDropDown(CAButEdit, 50, 50);
            popupEditWindow.showAtLocation(popupEditView, Gravity.CENTER,0,0);
            popupEditWindow.setFocusable(true);
            popupEditWindow.update();



            //Toast.makeText(ps_ss_compressed_air.this, Integer.toString(position+1) + " Clicked", Toast.LENGTH_SHORT).show();
        }

    });

    CALvDataTable.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            CALvDataTableListItems.remove(position);
            CALvDataTableAdapter.notifyDataSetChanged();

            Toast.makeText(ps_ss_compressed_air.this,"Removed", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}
}

custom_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal"
    android:divider="#000000"
    android:weightSum="7">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/From"
        android:layout_weight="1"/>
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/To"
        android:layout_weight="1"/>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/CFM"
        android:layout_weight="1" />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/Dia"
        android:layout_weight="1"/>
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/Length"
        android:layout_weight="1"/>
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/TotLoss"
        android:layout_weight="1"/>
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/DP"
        android:layout_weight="1"/>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

</LinearLayout>

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
    android:id="@+id/CAappbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/CAtoolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:logo="@mipmap/ic_launcher"
        app:title="@string/app_name">
    </android.support.v7.widget.Toolbar>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:paddingStart="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="14sp"
            android:text="@string/Compressed_Air_Name"
            android:id="@+id/CALabCAName"
            android:layout_weight="7"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/CAButAdd"
            android:layout_weight="1"
            android:background="@drawable/ps_ss_compressed_air_but_add"/>

    </LinearLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@+id/CAappbar"
    android:id="@+id/linearLayoutCAHeader">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:divider="#000000">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="2">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/Nodes_Lab"
            android:id="@+id/CALabNodes"
            android:textStyle="bold"
            android:layout_weight="1"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="#000000" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:weightSum="2"
                android:layout_weight="1">
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/Nodes_From_Lab"
                    android:id="@+id/CALabFrom"
                    android:textStyle="bold"
                    android:layout_weight="1"/>
                <View
                    android:layout_width="2dp"
                    android:layout_height="match_parent"
                    android:background="#000000" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/Nodes_To_Lab"
                    android:id="@+id/CALabTo"
                    android:textStyle="bold"
                    android:layout_weight="1"/>
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/CFM_Lab"
            android:id="@+id/CALabCFM"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/Diameter_Lab"
            android:id="@+id/CALabDia"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/Length_Lab"
            android:id="@+id/CALabLength"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/Total_Loss_Lab"
            android:gravity="center"
            android:id="@+id/CALabTotLoss"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/Pressure_Drop_Lab"
            android:id="@+id/CALabDP"
            android:textStyle="bold"
            android:layout_weight="1"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000" />

</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/linearLayoutCAHeader"
android:id="@+id/CALvDataTable"
android:longClickable="true"/>

</RelativeLayout>

0 个答案:

没有答案