如何在Android中长按一下从自定义列表视图中删除项目?

时间:2017-01-05 05:36:19

标签: android listview onlongclicklistener

我有一个自定义基本适配器的列表视图,用于验证listview中的一些项目。我想要的是当我长时间点击列表视图时,会打开一个对话框,说明"是"或"否"当我点击"是"它应该从适配器中删除该项目。我该怎么做。

这是适配器的代码

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 个答案:

答案 0 :(得分:1)

有两种方法可以在长按时删除项目。

1来自班级

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

2来自适配器

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

答案 1 :(得分:0)

以下示例代码结构;

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;
                }
            }); 

答案 2 :(得分:0)

从arraylist和referesh listview中删除所选项目

s_oDataset.remove(youritemPostion);  

notifyDataSetChanged();

点击YES

答案 3 :(得分:0)

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.

        }
    }); 

首先调用setOnItemLongClickListener并调用Dialogbuilder类,然后在该对话框构建器中使用.setPositiveButton或.setNegativeButton方法在该对话框上设置按钮