为啥我的列表视图上的删除按钮不起作用[重复]
Posted
技术标签:
【中文标题】为啥我的列表视图上的删除按钮不起作用[重复]【英文标题】:Why is the delete button on my list view not working [duplicate]为什么我的列表视图上的删除按钮不起作用[重复] 【发布时间】:2021-07-09 05:55:02 【问题描述】:我想在我的列表视图上实现这个删除按钮...我试图将按钮的标签设置为列表项的位置,然后我尝试使用 remove 方法将其删除,但它只是不起作用。这里可能有什么问题?请注意,我在这方面还很陌生,所以如果我的代码不好,我很抱歉。
这是我的适配器的代码:
// full subtask adapter code
package com.example.taskmasterv3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class SubtaskAdapter extends ArrayAdapter<subtask>
private final Context context;
private ArrayList<subtask> values;
public SubtaskAdapter(Context context, ArrayList<subtask> list)
//since your are using custom view,pass zero and inflate the custom view by overriding getview
super(context, 0 , list);
this.context = context;
this.values = list;
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
//check if its null, if so inflate it, else simply reuse it
if (convertView == null)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subtask_item, parent, false);
//use convertView to refer the childviews to populate it with data
TextView tvSubtaskName = convertView.findViewById(R.id.tvSubtaskName);
ImageView ivPri = convertView.findViewById(R.id.ivPri);
ImageView ivTime = convertView.findViewById(R.id.ivTime);
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
ivDelete.setTag(position);
tvSubtaskName.setText(values.get(position).getSubtaskName());
if (values.get(position).isPriHigh())
ivPri.setImageResource(R.drawable.priority_high);
else if (values.get(position).isPriMed())
ivPri.setImageResource(R.drawable.priority_med);
else if (values.get(position).isPriLow())
ivPri.setImageResource(R.drawable.priority_low);
if (values.get(position).isTimeMore())
ivTime.setImageResource(R.drawable.time_symbol_more);
else if (values.get(position).isTimeMed())
ivTime.setImageResource(R.drawable.time_symbol_med);
else if (values.get(position).isTimeLess())
ivTime.setImageResource(R.drawable.time_symbol_less);
// Delete button for subtasks (NOT WORKING)
ivDelete.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
values.remove(ivDelete.getTag());
notifyDataSetChanged();
);
//return the view you inflated
return convertView;
//to keep adding the new subtasks try the following
public void addANewSubTask(subtask newSubTask)
ArrayList<subtask> newvalues = new ArrayList<>(this.values);
newvalues.add(newSubTask);
this.values = newvalues;
notifyDataSetChanged();
【问题讨论】:
【参考方案1】:我就是这样做的。也许它可以帮助你。无论如何,尝试使用调试器来检查按下时是否调用它
public class ListNoteAdapter extends BaseAdapter implements ListAdapter
private ArrayList<Nota> arrayList;
private Context context;
public ListNoteAdapter(ArrayList<Nota> arrayList, Context context)
this.arrayList = arrayList;
this.context = context;
@Override
public int getCount()
return arrayList.size();
@Override
public Object getItem(int position)
return arrayList.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View view = convertView;
if(view == null)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_with_button, parent, false);
TextView textView = view.findViewById(R.id.list_item);
textView.setText(arrayList.get(position).toString());
//When user click on a text view, text will be saved into clipboard
textView.setOnClickListener( v ->
ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("Note clipboard", textView.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(context, "Testo copiato negli appunti", Toast.LENGTH_SHORT).show();
);
ImageButton deleteButton = view.findViewById(R.id.deleteNote);
deleteButton.setOnClickListener( v ->
LayoutInflater layoutInflater = (LayoutInflater)parent.getContext().getSystemService(parent.getContext().LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.delete_popup_window, parent, false);
Button decline = popupView.findViewById(R.id.deleteDeclineButton);
/**
* In order to allow the user to delete any note voluntarily,
* a confirming popup window will appear
*/
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Button agreed = popupView.findViewById(R.id.deleteAgreeButton);
agreed.setOnClickListener(v1 ->
File file = new File(context.getFilesDir(),
arrayList.get(position).date + "@" + arrayList.get(position).time + ".txt");
if(file.exists())
file.delete();
arrayList.remove(position);
popupWindow.dismiss();
//Update the list view to show the new note without needing to restart the application
this.notifyDataSetChanged();
);
decline.setOnClickListener(vDecline ->
popupWindow.dismiss();
);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
);
return view;
【讨论】:
【参考方案2】:为什么不直接按位置删除
values.remove(position);
【讨论】:
我试过了!但由于某种原因,它每次都会给我一个索引越界异常以上是关于为啥我的列表视图上的删除按钮不起作用[重复]的主要内容,如果未能解决你的问题,请参考以下文章
AngularJS、SweetAlert.js 在自定义指令中不起作用