简体   繁体   English

如何使用对话框自定义ArrayAdapter时从ListView中删除项目

[英]How to delete item from ListView while using Custom ArrayAdapter w/ Dialog

Some Context: I have a Custom ArrayAdapter for a ListView that has 3 parameters Name, Edit Button, Delete Button. 一些上下文:我有一个ListView的自定义ArrayAdapter,它具有3个参数Name,Edit Button,Delete Button。 I have setup onClicks in the ArrayAdapter to be able to detect which profile is being clicked with the specific edit/delete button press. 我在ArrayAdapter中设置了onClicks ,以便能够通过按下特定的编辑/删除按钮来检测正在单击的配置文件。 When the user deletes a profile I remove the profile from SQLite DB however now I've ran into the problem of trying to update the ArrayList with the removed item and notifyDataSetChanged for my ListView . 当用户删除配置文件时,我从SQLite DB删除了配置文件,但是现在我遇到了尝试用删除的项目更新ArrayList并为ListView notifyDataSetChanged的问题。

Question 1: I can't figure out if I should be doing this in the Class that is containing the ListView and ArrayList or if I should be trying to update this from the ArrayAdapter in the onClicks. 问题1:我不知道是否应该在包含ListView和ArrayList的类中执行此操作,或者是否应该尝试从onClicks中的ArrayAdapter进行更新。

Question 2: Whatever method might be right how can I correctly update the deleted item from the ListView when the user confirms the delete in the Dialog. 问题2:无论哪种方法正确,当用户在对话框中确认删除后,如何正确地从ListView更新已删除的项目。

在此处输入图片说明

Current ArrayAdapter Class 当前的ArrayAdapter类

public class ListViewItemAdapter extends ArrayAdapter<ListViewItem>
{
    private Context mContext;
    private List<ListViewItem> list = new ArrayList<>();
    private DatabaseHelper databaseHelper;
    private String profileName;

    public ListViewItemAdapter(@NonNull Context context, ArrayList<ListViewItem> listItem) {
    super(context, 0 , listItem);
    mContext = context;
    list = listItem;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItem = convertView;
    if(listItem == null)
        listItem = LayoutInflater.from(mContext).inflate(R.layout.custom_listview,parent,false);

    final ListViewItem listViewItem = list.get(position);

    //Text View Profile
    final TextView name = (TextView) listItem.findViewById(R.id.textView_name);
    name.setText(listViewItem.getmName());
    profileName = listViewItem.getmName();

    //Edit Button Profile
    ImageButton image = listItem.findViewById(R.id.imageView_poster);
    image.setImageResource(listViewItem.getmImageDrawable());
    image.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(mContext,
                    "Edit Profile:" + listViewItem.getmProfile() + " Name:" + listViewItem.getmName(),
                    Toast.LENGTH_SHORT).show();
        }
    });


    //Delete Button Profile **Currently Testing**
    ImageButton image2 = listItem.findViewById(R.id.imageView_poster2);
    image2.setImageResource(listViewItem.getmImageDrawable2());
    image2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            databaseHelper = new DatabaseHelper(getContext());

            Toast.makeText(mContext,
                    "Delete Profile:" + listViewItem.getmProfile() + " Name:" + listViewItem.getmName(),
                    Toast.LENGTH_SHORT).show();
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext(),R.style.AlertDialogTheme);

            builder.setTitle("Delete Profile?")
                    .setMessage("Are you sure you want to delete\n" + listViewItem.getmName())
                    .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            databaseHelper.deleteRowProfile(listViewItem.getmName());

                            //
                            //This is where I'm try to update the ListView
                            //

                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .show();
        }
    });


    return listItem;
}
}

Function in Fragment Class that Populates the ListView onViewCreated 片段类中的函数,用于填充ListView onViewCreated

public void getProfileList()
{
    arrayList = new ArrayList<ListViewItemAdapter>();
    listViewItemAdapter = new ListViewItemAdapter(getContext(),arrayList);

    Cursor result = databaseHelper.getAllDataCarProfile();

    if(listViewItemAdapter != null){
        listViewItemAdapter.clear();
        listViewItemAdapter.notifyDataSetChanged();
    }

    if (result.getCount() != 0)
    {
        while (result.moveToNext())
        {
            arrayList.add(new ListViewItem("CarProfile",
                    result.getString(0),
                    R.drawable.ic_edit_gray_24dp,
                    R.drawable.ic_delete_grey_24dp));
        }
        listViewItemAdapter.notifyDataSetChanged();
    }

    listViewCarProfile.setAdapter(listViewItemAdapter);

}

You can do this in either way . 您可以通过任何一种方式来执行此操作。 You can create a function in your adapter class and perform the clickListener on it . 您可以在适配器类中创建一个函数并对其执行clickListener。

deleteItem.setOnClickListener(v -> {
            potsList.remove(getAdapterPosition());
            notifyDataSetChanged();
 }

Or in your class , when remove the item from list , don't forget to notify the adapter . 或者在您的课堂上,当从清单中删除物品时,不要忘记通知适配器。 One the adapter get notified , it will reflect the change on screen. 一个适配器得到通知,它将反映在屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用按钮删除 Listview w/ArrayAdapter 中的选定项目 - Using button to delete selected item in Listview w/ ArrayAdapter 如何使用自己的ArrayAdapter从列表视图中删除项目? - How to delete item from listview using own ArrayAdapter? 从片段中的自定义 ArrayAdapter 中单击 ListView 中的“删除”按钮,从数据库中删除相应的项目 - Delete corresponding Item from Database on click of 'delete' button in ListView from Custom ArrayAdapter in Fragment 我如何从使用ArrayAdapter的列表视图中删除项目 <String> 显示在列表视图上 - How do I delete item from listview I used ArrayAdapter<String> to display on listview Android:如何从ListView和arrayAdapter中删除项目 - Android: how to remove an item from a listView and arrayAdapter 如何使用arrayadapter和notifyDataSetChanged删除ListView项目 - How to remove ListView Item using arrayadapter and notifyDataSetChanged 使用ArrayAdapter从LinearLayout添加和删除项目 - Add & delete item from LinearLayout using ArrayAdapter 如何使用自定义baseadapter从listView中删除项目 - How to delete item from listView with custom baseadapter 如何使用警报对话框删除SQLite ListView Android中的项目 - How to delete item in sqlite listview android using alert dialog 如何从Android中的contextmenu删除listview对话框项? - How to delete listview dialog item from contextmenu in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM