Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当在主类中移除recyclerview卡时,如何从recyclerview适配器类更新SharedReferences?_Java_Android_Android Recyclerview_Sharedpreferences - Fatal编程技术网

Java 当在主类中移除recyclerview卡时,如何从recyclerview适配器类更新SharedReferences?

Java 当在主类中移除recyclerview卡时,如何从recyclerview适配器类更新SharedReferences?,java,android,android-recyclerview,sharedpreferences,Java,Android,Android Recyclerview,Sharedpreferences,我有一个主类,负责将卡添加到recycler视图,并将卡名保存在共享首选项中。但是,当我删除一张卡片时,如何在共享首选项中更新该卡片?由于卡删除已完成,因此recyclerview适配器类是否仅适用 主类 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lighting);

我有一个主类,负责将卡添加到recycler视图,并将卡名保存在共享首选项中。但是,当我删除一张卡片时,如何在共享首选项中更新该卡片?由于卡删除已完成,因此recyclerview适配器类是否仅适用

主类

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lighting);
        getSupportActionBar().hide();

        mContext = getApplicationContext();
        ahomeLayout = findViewById(R.id.lay4);
        buttonScan1 = findViewById(R.id.buttonscan1);
        buttonScan2 = findViewById(R.id.fab1);
        buttonScan3 = findViewById(R.id.fab2);

        mRelativeLayout = findViewById(R.id.rl);
        mRecyclerView = findViewById(R.id.recycler_view);

        animals = new String[]{};

        // Initialize an array list from array
        animalsList = new ArrayList(Arrays.asList(animals));

        // Define a layout for RecyclerView
        mLayoutManager = new LinearLayoutManager(mContext, LinearLayout.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // Initialize a new instance of RecyclerView Adapter instance
        mAdapter = new AnimalsAdapter(mContext,animalsList);

        // Set the adapter for RecyclerView
        mRecyclerView.setAdapter(mAdapter);

        LinearLayout.LayoutParams alparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        atv = new TextView(this);
        atv.setLayoutParams(alparams);
        atv.setText("Currently no 'Device' is added, tap '+' to configure and add new 'Device'.");
        atv.setTextSize(19);
        atv.setGravity(Gravity.CENTER);
        atv.setPadding(70, 470, 70, 50);
        ahomeLayout.addView(atv);

        toolbar = new Toolbar(this);
        LinearLayout.LayoutParams toolBarParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 120);
        toolbar.setLayoutParams(toolBarParams);
        toolbar.setBackgroundColor(Color.parseColor("#2fffffff"));
        toolbar.setVisibility(View.VISIBLE);

        add_dev = new Dialog(Lighting.this);
        add_dev.setContentView(R.layout.add_device);
        v1 = View.inflate(this, R.layout.add_device, null);
        add_dev.setContentView(v1);

        dev_name1 = v1.findViewById(R.id.dev_name);
        dev_id1 = v1.findViewById(R.id.dev_id);
        dev_but1 = v1.findViewById(R.id.deviceBut1);

        dev_but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dev_name1.getText().toString().trim().length()<=0)
                    Toast.makeText(getApplicationContext(), "Please input Home name", Toast.LENGTH_SHORT).show();
                else if(dev_id1.getText().toString().trim().length()<=0)
                    Toast.makeText(getApplicationContext(), "Please input Device ID", Toast.LENGTH_SHORT).show();
                else {
                    ahomeLayout.removeView(atv);
                    // String itemLabel = dev_name1.getText().toString().trim();

                    // Add an item to animals list
                    animalsList.add(position, "" + dev_name1.getText().toString());

                    // Scroll to newly added item position
                    mRecyclerView.scrollToPosition(position);

                    // Show the added item label
                    // Toast.makeText(mContext, "Added : " + itemLabel, Toast.LENGTH_SHORT).show();
                    names[mCount] = dev_name1.getText().toString().trim();
                    ids[mCount] = dev_id1.getText().toString().trim();
                    mCount++;
                    saveInPref();
                    add_dev.dismiss();
                }
            }
        });

        final Dialog alert = new Dialog(this);

        wv1 = new WebView(this);
        wv1.loadUrl("http:\\192.168.23.1");
        wv1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        wv1.requestFocusFromTouch();
        wv1.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    public void cardview2()
    {
        ahomeLayout.removeView(atv);
        mSettings2 = getSharedPreferences("APP_PREFERENCES_2", Context.MODE_PRIVATE);
        name = mSettings2.getString(name_key[j],"");

        animalsList.add(position, "" + name);
        mAdapter.notifyItemInserted(position);
        mRecyclerView.scrollToPosition(position);
        j++;
    }

    public void saveInPref()
    {
        mSettings2 = getSharedPreferences("APP_PREFERENCES_2", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mSettings2.edit();
        editor.putInt("COUNT_CARDS2", mCount);
        editor.putString(name_key[mCount-1],names[mCount-1]);
        editor.putString(id_key[mCount-1],ids[mCount-1]);
        editor.apply();
    }

    @Override
    public void onResume() {
        super.onResume();
        mSettings2 = getSharedPreferences("APP_PREFERENCES_2", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mSettings2.edit();
        editor.putInt("COUNT_CARDS2", mCount);
        if(mSettings2.contains("COUNT_CARDS2"))
        {
            mCount = mSettings2.getInt("COUNT_CARDS2", 0);
            for (int i=0; i<mCount; i++)
                cardview2();
        }
    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、照明);
getSupportActionBar().hide();
mContext=getApplicationContext();
ahomeLayout=findviewbyd(R.id.lay4);
buttonScan1=findViewById(R.id.buttonScan1);
buttonScan2=findViewById(R.id.fab1);
buttonScan3=findViewById(R.id.fab2);
mRelativeLayout=findviewbyd(R.id.rl);
mRecyclerView=findviewbyd(R.id.recycler\u视图);
动物=新字符串[]{};
//从数组初始化数组列表
animalsList=newarraylist(Arrays.asList(animals));
//定义RecyclerView的布局
mLayoutManager=新的LinearLayoutManager(mContext,LinearLayout.VERTICAL,false);
mRecyclerView.setLayoutManager(mllayoutmanager);
//初始化RecyclerView适配器实例的新实例
mAdapter=新的AnimalsAdapter(mContext,animalsList);
//为RecyclerView设置适配器
mRecyclerView.setAdapter(mAdapter);
LinearLayout.LayoutParams alparams=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);
atv=新文本视图(本);
亚视setLayoutParams(阿尔帕拉姆斯);
atv.setText(“当前未添加任何‘设备’,点击“+”配置并添加新的‘设备’”;
亚洲电视(19),;
atv设置重力(重心);
亚洲电视(70、470、70、50);
ahomeLayout.addView(亚洲电视);
工具栏=新工具栏(此);
LinearLayout.LayoutParams toolBarParams=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,120);
toolbar.setLayoutParams(toolBarParams);
toolbar.setBackgroundColor(Color.parseColor(“#2ffffff”));
工具栏.setVisibility(View.VISIBLE);
add_dev=新建对话框(Lighting.this);
添加_dev.setContentView(R.layout.add_设备);
v1=视图。充气(此,R.layout.add_设备,空);
添加_dev.setContentView(v1);
dev_name 1=v1.findviewbyd(R.id.dev_name);
dev_id1=v1.findviewbyd(R.id.dev_id);
dev_but1=v1.findviewbyd(R.id.deviceBut1);
dev_but1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){

if(dev_name1.getText().toString().trim().length()使用接口处理删除操作

首先创建DeleteListener:

public interface DeleteListener {
    void onDelete(int position);
}
然后将侦听器作为参数添加到适配器,并在单击“删除”时调用它:

public AnimalsAdapter(Context context,List<String> list, DeleteListener listener){
    mDataSet = list;
    mListener = listener;
    mContext = context;
}

...

// Set a click listener for item remove button
    holder.mRemoveButton.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View view) {
            // Get the clicked item label
            String itemLabel = mDataSet.get(position);

            // Remove the item on remove/button click
            mDataSet.remove(position);

           /* Lighting remove = new Lighting();
            remove.mCount--;*/

            notifyItemRemoved(position);
            notifyItemRangeChanged(position,mDataSet.size());

            mListener.onDelete(position);

            Toast.makeText(mContext,"Removed : " + remove.mCount,Toast.LENGTH_SHORT).show();
        }
    });

希望这有帮助。

请发布一些代码片段,如果没有它们,我们将无法真正提供帮助。要编辑共享首选项,您只需要上下文,因此可以使用任何回收器视图项目上下文(itemView.getContext()),您可以编辑首选项。有关具体答案,请共享代码段。请参阅现在添加的代码段。
public AnimalsAdapter(Context context,List<String> list, DeleteListener listener){
    mDataSet = list;
    mListener = listener;
    mContext = context;
}

...

// Set a click listener for item remove button
    holder.mRemoveButton.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View view) {
            // Get the clicked item label
            String itemLabel = mDataSet.get(position);

            // Remove the item on remove/button click
            mDataSet.remove(position);

           /* Lighting remove = new Lighting();
            remove.mCount--;*/

            notifyItemRemoved(position);
            notifyItemRangeChanged(position,mDataSet.size());

            mListener.onDelete(position);

            Toast.makeText(mContext,"Removed : " + remove.mCount,Toast.LENGTH_SHORT).show();
        }
    });
mAdapter = new AnimalsAdapter(mContext,animalsList, new DeleteListener() {
        @Override
        public void onDelete(int position) {

        }
    };);