У меня есть основной класс, который отвечает за добавление карточек в представление переработчика и сохранение имени карточки в общих настройках. Но когда я удаляю карту, как я могу обновить ее в общих настройках? Поскольку удаление карты выполнено, является ли класс адаптера просмотра только для повторного использования?
Основной класс
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();
}
}
Класс адаптера для повторного просмотра
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();
}
}