从ArrayAdapter中删除项目

时间:2015-11-17 03:55:51

标签: android listview

我正在尝试实现删除项目(因为当用户在每个项目中按textView时,它会说明在Android中的listView中从0更新为1。例如: 有3个项目

  • apple 0
  • orrange 0
  • grape 0

在每个项目中都有一个textView作为按钮。当用户按下textView for Apple时,我的应用程序会将项目'apple'更新为1,并将其从listView中删除,并且在listview中'apple'将会消失。 这是我的列表代码

public class ListPelangganUnsyncFragment extends ListFragment
{
    AdapterListPelanggan pelangganAdp;
    ArrayList<Pelanggan> pelanggan = null;
    FloatingActionButton fab;
    Button sync;
    int list = 0;
    int newList = 0;
    ListView listView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState)
    {
        final View rootView = inflater.inflate(R.layout.list_pelanggan, container, false);
        super.onCreate(savedInstanceState);

        listView = (ListView) rootView.findViewById(android.R.id.list);
//        Buat kalo udah ada login
//        SharedPreferences setting

        pelanggan = new ArrayList<Pelanggan>();

//        Buat ngambil data dari DB lokal
        final DBPelanggan dbpel = new DBPelanggan(getActivity()); // get context dri activity, return null klo fragment ini ga attach
        try
        {
            dbpel.open();
            pelanggan = dbpel.getDataPelanggan();
            dbpel.close();
            list = pelanggan.size();
            Log.d("List", list+"");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        sync = (Button) rootView.findViewById(R.id.sinkron);
        Button add = (Button) rootView.findViewById(R.id.addPelanggan);
        add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(rootView.getContext(), FormPelangganActivity.class);
                intent.putExtra("fungsi", 0);
                startActivity(intent);
                getActivity().finish();
            }
        });

        sync.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                try
                {
                    dbpel.open();
                    dbpel.updateStat(pelanggan);
                    pelanggan = dbpel.getDataPelanggan();
                    dbpel.close();
                    if(pelanggan != null)
                    {
                        pelangganAdp = new AdapterListPelanggan(getActivity(), R.layout.isi_list_pelanggan, pelanggan);
                        listView.setAdapter(pelangganAdp);
                    }
                    ((MainActivity)getActivity()).selectFragment(0);

                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        });




        //Adapter

        listView.setClickable(true);
        if(pelanggan != null)
        {
            pelangganAdp = new AdapterListPelanggan(getActivity(), R.layout.isi_list_pelanggan, pelanggan);
            listView.setAdapter(pelangganAdp);

            newList = listView.getCount();
            Log.d("List", newList + "");
        }

        return rootView;
    }

这是我的适配器:

public class AdapterListPelanggan extends ArrayAdapter<Pelanggan>
{
    private Context context;
    private int resource;
    private ArrayList<Pelanggan> arPelanggan;
    Holder h = null;
    DBPelanggan db;
    private boolean status;

    // Holder = Object
    static class Holder
    {
        TextView tv_KTP, tv_Nama;
        ImageView syncButton;
    }

    public AdapterListPelanggan(Context context, int resource, ArrayList<Pelanggan> arPelanggan)
    {
        super(context, resource, arPelanggan);

        this.context = context;
        this.resource = resource;
        this.arPelanggan = arPelanggan;

        Log.d("ListAdapter", "Jumlah List c "+arPelanggan.size());
    }

    public AdapterListPelanggan(Context context, int resource, ArrayList<Pelanggan> arPelanggan, boolean status)
    {
        super(context, resource, arPelanggan);

        this.context = context;
        this.resource = resource;
        this.arPelanggan = arPelanggan;
        this.status = status;
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        h = new Holder();
        final Pelanggan pelanggan = arPelanggan.get(position); // Set Data per urutan
        Log.d("ListAdapter", "ID Pelanggan b "+pelanggan.getId_pelanggan()+"");
        if(convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resource, parent, false);

            h.tv_KTP = (TextView)convertView.findViewById(R.id.tv_ktp_isi_lp);
            h.tv_Nama = (TextView)convertView.findViewById(R.id.tv_nama_isi_lp);
            h.syncButton = (ImageView)convertView.findViewById(R.id.syncButton);

            if(status)
            {
                h.syncButton.setVisibility(View.GONE);
            }

            convertView.setTag(h);
        }
        else
        {
            h = (Holder)convertView.getTag();
        }

        // ------------------ Button Sync ------------------

        h.syncButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                try
                {
                    db = new DBPelanggan(getContext());
                    db.open();
                    db.updateStatPelanggan(pelanggan);
                    Log.d("ListAdapter", "ID hilang " + pelanggan.getId_pelanggan());
                    arPelanggan.remove(pelanggan);
                    arPelanggan = db.getDataPelanggan();
                    Log.d("ListAdapter", "Jumlah List c "+arPelanggan.size());


         Log.d("ListAdapter", pelanggan.getId_pelanggan()+"ID Sekarang");
                db.close();
                notifyDataSetChanged();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    });
    // ---------------------------------------------------------------

    h.tv_KTP.setText(pelanggan.getId_pelanggan());
    h.tv_Nama.setText(pelanggan.getNama());

    return convertView;
}


}

问题是当我按下textView for'apple'时,确实它已经在db中更新并且从我的列表中删除了,但是当我按下'orrange'后按'apple'我的应用程序会出错。

这是我的错误:

  

11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:at java.util.ArrayList.get(ArrayList.java:304)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:at com.example.michael.ims_application_apps.AdapterListPelanggan.getView(AdapterListPelanggan.java:63)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:在android.widget.AbsListView.obtainView(AbsListView.java:2260)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:在android.widget.ListView.makeAndAddView(ListView.java:1769)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:在android.widget.ListView.fillDown(ListView.java:672)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:在android.widget.ListView.fillGap(ListView.java:636)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5045)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3202)       11-17 10:34:55.639 5429-5429 / com.example.michael.ims_application_apps E / AndroidRuntime:在android.widget.AbsListView.onTouchEvent(AbsListView.java:3476)

任何人都可以建议我如何解决它?谢谢。

2 个答案:

答案 0 :(得分:0)

检查getview中的以下代码行。您在position变量中得到的值,它可能大于数组列表的长度 - &gt;

final Pelanggan pelanggan = arPelanggan.get(position); 

答案 1 :(得分:0)

问题出在这一行,

  arPelanggan = db.getDataPelanggan();

删除后,您正在从数据库重新加载数据。