How to delete item from custom listview on long click in android?

3.1k views Asked by At

I have a listview with custom base adapter which validate some items in listview. What i want is when i long click on item oflistview, a dialog should open stating "Yes" or "No" and when i tap on "Yes" it should delete that item from adapter.How can i do that.

Here is code of Adapter

private static final String TAG = CDealAppListingAdapter.class.getSimpleName();
private static final String DEAL_CODE = "DealCode";
private static final String HEADER_TEXT = "headerText";
private static final String LOGO_PATH = "logoPath";
private final Context m_Context;// declaring context variable
private final ArrayList<CDealAppDatastorage> s_oDataset;// declaring array list ariable

public CDealAppListingAdapter(Context m_Context, ArrayList<CDealAppDatastorage> mDataList) {
    this.m_Context = m_Context;
    s_oDataset = mDataList;
}

@Override
public int getCount() {// get total arraylist size
    return s_oDataset.size();
}

@Override
public Object getItem(int position) {// get item position in array list
    return s_oDataset.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressWarnings("deprecation")
@SuppressLint({"SetTextI18n", "InflateParams"})
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.deallisting_card_view, null);
        viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
        viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
        viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
        viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
        viewHolder.mProgress = (ProgressBar) convertView.findViewById(R.id.progressBar3);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.m_getBtn.setOnClickListener(new View.OnClickListener() {// onclick getDeal Btn
        @Override
        public void onClick(View v) {//send to deal detail page onclick getDeal Btn
            if (NetworkUtil.isConnected(m_Context)) {
                Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
                i.putExtra(DEAL_CODE, s_oDataset.get(position).getM_szsubHeaderText());// get deal code from deal data storage
                i.putExtra(HEADER_TEXT, s_oDataset.get(position).getM_szHeaderText());// get deal name from deal dta storage
                i.putExtra(LOGO_PATH, s_oDataset.get(position).getM_szLogoPath());
                v.getContext().startActivity(i);
            } else {
                /*here I am getting error*/
                CSnackBar.showSnackBarError(v, m_Context.getString(R.string.no_internet_connection), v.getContext());
            }

        }
    });

    CDealAppDatastorage m = s_oDataset.get(position);
    viewHolder.m_Header.setText(m.getM_szHeaderText());
    viewHolder.m_DummyText.setText(m.getM_szDetails());
    viewHolder.m_getBtn.setText("GET " + m.getM_szDealValue() + " POINTS");// set deal button text
    Picasso.with(m_Context).load(m.getM_szLogoPath()).into(viewHolder.m_logoImage, new Callback() {
        @Override
        public void onSuccess() {
            Log.e(TAG, "OnSuccess Called::");
            viewHolder.mProgress.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onError() {
            Log.e(TAG, "OnError Called::");
        }
    });

    return convertView;
}

private class ViewHolder {
    public TextView m_Header, m_Subheader, m_DummyText;
    public ImageView m_logoImage;
    public Button m_getBtn;
    public ProgressBar mProgress;


}
}
4

There are 4 answers

3
Febi M Felix On

Sample code structure below;

listview.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                        int pos, long id) {
                    // TODO Auto-generated method stub

                    //show dialog asking user option to delete or not
                    //On OK click, dataset.remove(position);
                   //adapter.notifyDatasetChanged();

                    return true;
                }
            }); 
0
ViramP On

There are two ways to remove item on long press.

1 From class

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                return false;
            }
        });

2 From adapter

viewHolder.m_getBtn.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return false;
                }
            });
0
sivaBE35 On

Remove selected item from arraylist and referesh listview

s_oDataset.remove(youritemPostion);  

notifyDataSetChanged();

after you click YES

0
lifeisbeautiful On
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub
            //call Dialog builder class here.

        }
    }); 

First call setOnItemLongClickListener and inside that call Dialogbuilder class ,And inside that dialog builder set your buttons on that dialog using .setPositiveButton or .setNegativeButton methods