从Java Android中的固定数组中删除项目

问题描述

我创建了一个自定义列表视图,每行都有一个删除按钮。 Delete按钮将正常工作,它将从数据库删除选定的行,但是我还需要从适配器列表中删除该项目,并通知适配器刷新列表。问题是我使用的是固定数组rNames [],看起来很难从其中删除项目。 我试图将其转换为ArrayList,但看起来非常复杂,因此我需要更改代码中的许多内容。 有没有简单的方法可以从固定数组rNames []中删除项目?

代码如下:

  // Our adapter class
class MyAdapter extends ArrayAdapter<String> {
    Context context;
    String rNames[];


    MyAdapter(Context c,String name[]) {
        super(c,R.layout.row,R.id.customerName,name);
        this.context = c;
        this.rNames = name;

    }

    // The delete button
    public View getView(final int position,View convertView,ViewGroup parent) {

        View theView = super.getView(position,convertView,parent);     // It took me three days to fix :) I posed a question for it in StackOverFlow
        Button deleteButt = (Button) theView.findViewById(R.id.deleteButton);
        deleteButt.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             
                ParseQuery<ParSEObject> query = ParseQuery.getQuery("Orders");
                query.whereEqualTo("objectId",mainObjectsArray.get(position)[3]);  // mainObjectsArray.get(position)[3] We fetched the object id and stored it in mainObjectArray. position is the position of the item in the list which is the same position of the the same item in the mainObjectArray. Smart isn't it?! :)
                query.findInBackground(new FindCallback<ParSEObject>() {
                    @Override
                public void done(final List<ParSEObject> object,ParseException e) {
                    if (e == null) {

                        // This dialog code was taken from Newcastle app
                        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                        alert.setMessage("Are you sure you want to delete this order?")
                                .setNegativeButton("No",null)
                                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,int which) {

                                        //Delete based on the position
                                        object.get(0).deleteInBackground(new DeleteCallback() {
                                            @Override
                                            public void done(ParseException e) {
                                                if (e == null) {
                                                    Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_SHORT).show();

                                                } else {
                                                    Toast.makeText(
                                                            getApplicationContext(),e.getMessage().toString(),Toast.LENGTH_LONG
                                                    ).show();
                                                }
                                            }
                                        });

                                    }
                                        });

                        alert.create().show();


                    } else {
                        Toast.makeText(
                                getApplicationContext(),Toast.LENGTH_LONG
                        ).show();
                    }
                };
            });

            }
        });

        return theView;
    }
    
}

从db中删除后,我应该从rNames中删除该项目,这意味着在行之后

Toast.makeText(getActivity(),Toast.LENGTH_SHORT).show();

然后通知适配器。

任何帮助将不胜感激。 非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)