0

I use checkedTextView in my cardAdapter and want to save text of the checked ones.I saw toast message usage but did not see how to save them(to shared preference or database). Is this possible? I am new to Android help please

I've already written a code about that but it does not work so I asked you 'is that possible?'

CardAdapter

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardTasarimTutucu> {
    private Context mContext;
    private ArrayList<Word> words;
    private SharedPreferences.Editor e;
    private ArrayList<String> sharedP;
    private String word = null;
    private int id=0;
    private StringBuffer str = null;
    private boolean flag=true;
    private SharedPreferences sp;




    public CardAdapter(Context mContext, ArrayList<Word> words) {
        this.mContext = mContext;
        this.words =  words;
    }


    public class CardTasarimTutucu extends RecyclerView.ViewHolder{
        private CardView rowCard;
        private CheckedTextView rowText;

        public CardTasarimTutucu(@NonNull View itemView) {
            super(itemView);
            this.rowCard = itemView.findViewById(R.id.rowCard);
            this.rowText = itemView.findViewById(R.id.rowText);
        }
    }
    @NonNull
    @Override
    public CardTasarimTutucu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_design,parent,false);
        return new CardTasarimTutucu(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final CardTasarimTutucu holder, int position) {

        sp = mContext.getSharedPreferences("checked",Context.MODE_PRIVATE);
        sharedP= new ArrayList<>();
        e=sp.edit();
        word = words.get(position).getMean();
        Log.e("getMean",word);
        id= words.get(position).getId();
        str= new StringBuffer("");

        holder.rowText.setText(word);
        holder.rowText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (holder.rowText.isChecked()) {

                    sharedP.remove(sharedP.indexOf(word));
                    flag=false;
                    holder.rowText.setCheckMarkDrawable(null);
                    holder.rowText.setChecked(false);

                } else {

                    sharedP.add(word);
                    flag=true;
                    Log.e("deneme",word);
                    holder.rowText.setCheckMarkDrawable(R.drawable.ic_check_black_24dp);
                    holder.rowText.setChecked(true);

                }

                str.delete(0,str.length());
                for(int i=0;i<sharedP.size();i++){
                    Log.e("sharedpreference1",sharedP.get(i).toString());
                    str.append(sharedP.get(i));
                    str.append(".");

                }
                if(flag==false){
                    e.remove("str");
                    e.commit();
                }
                Log.e("size",str.toString());
                e.putString("str", str.toString());
                e.commit();

            }

        });

    }
kestane
  • 11
  • 4
  • 3
    please share the code used, so that the community can take a look at what you have already done and help you in the areas where you have a problem. But, we are not here to implement these stuff for you – Infamous Dec 22 '19 at 19:21
  • @Infamous I added my code – kestane Dec 23 '19 at 09:36
  • It sure is possible to save the data from the Adapter to SharedPreference or the local SQLite DB. Since I have little idea of the nature of the stuff we are saving, I really can't help you with what option to go with.. whether it is SharedPrefs or DB. Anyways, it is simple enough to save values as key pairs in SP -> https://stackoverflow.com/questions/23024831/android-shared-preferences-for-creating-one-time-activity-example – Infamous Dec 23 '19 at 11:21

1 Answers1

0
String val = checkedTextView.getText().toString()

and save val variable value in shared preferences if you want to save val locally