[英]How to update the sharedpreferences from recyclerview adapter class class when a recyclerview card is removed in Main class?
I have a main class that is responsible for adding cards to recycler view and saving the card name in shared preferences.我有一个主类负责将卡片添加到回收站视图并将卡片名称保存在共享首选项中。 But when I am deleting a card then how can I update the same in shared preferences?但是,当我删除卡片时,如何在共享首选项中更新相同的卡片? Since the card deletion is done is the recyclerview adapter class only?由于卡片删除完成,是否只有 recyclerview 适配器类?
Main class主类
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();
}
}
Recyclerview Adapter class Recyclerview适配器类
public class AnimalsAdapter extends RecyclerView.Adapter<AnimalsAdapter.ViewHolder>{
private List<String> mDataSet;
private Context mContext;
private Random mRandom = new Random();
public SharedPreferences mSettings2;
public AnimalsAdapter(Context context,List<String> list){
mDataSet = list;
mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public ImageButton mRemoveButton;
public RelativeLayout mRelativeLayout;
public ViewHolder(View v){
super(v);
mTextView = v.findViewById(R.id.tv);
mRemoveButton = v.findViewById(R.id.ib_remove);
mRelativeLayout = v.findViewById(R.id.rl);
}
}
@Override
public AnimalsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// Create a new View
View v = LayoutInflater.from(mContext).inflate(R.layout.devices,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position){
holder.mTextView.setText(mDataSet.get(position));
// Set a click listener for TextView
holder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String animal = mDataSet.get(position);
Toast.makeText(mContext,animal,Toast.LENGTH_SHORT).show();
}
});
// 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());
Toast.makeText(mContext,"Removed : " + remove.mCount,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount(){
return mDataSet.size();
}
}
Use Interface to handle delete operation.使用接口来处理删除操作。
First create DeleteListener:首先创建DeleteListener:
public interface DeleteListener {
void onDelete(int position);
}
Then add listener to adapter as parameter and call it when click remove:然后将侦听器作为参数添加到适配器并在单击删除时调用它:
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();
}
});
On your activity call adapter like this:在您的活动调用适配器上,如下所示:
mAdapter = new AnimalsAdapter(mContext,animalsList, new DeleteListener() {
@Override
public void onDelete(int position) {
}
};);
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.