mardi 2 juillet 2019

how save value of checkbox after close the app in android?

I want save the state of checkbox after close the app what I should to do? I don't know how I do that with list view and arrayadapter how use sharedpreferences here ? mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

in MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word_list);


        MyAdView.SetAd((AdView)findViewById(R.id.adView));


        ListView list = (ListView) findViewById(R.id.list);

        ArrayList<Word> worda = new ArrayList<>();

        worda.add(new Word("The B",R.drawable.ic_launcher_background));
        worda.add(new Word("The B",R.drawable.ic_launcher_background));



        WordAdapter adapter = new WordAdapter(this, worda);
        list.setAdapter(adapter);



        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                if (position == 0) {
                    Intent intent = new Intent(MainActivity.this, TheBossBabyS1.class);
                    startActivity(intent);
                }

            }        });
    }
}

in Word.java

public class Word {

    private String mConversation;

    private int mImageResourceId = NO_IMAGE_PROVIDED;

    public static final int NO_IMAGE_PROVIDED = -1;



    public Word(String conversation){

        mConversation = conversation;

    }

    public Word(String conversation, int imageResourceId){

        mConversation = conversation;

        mImageResourceId = imageResourceId;

    }


    public String getConversation(){
        return  mConversation;
    }

    public int getImageResourceId(){ return mImageResourceId; }

    public boolean hasImage(){
        return mImageResourceId != NO_IMAGE_PROVIDED;
    }


}




in WordAdapter.java

public class WordAdapter extends ArrayAdapter {

    private int mColorResourceId;

    public WordAdapter(Context context, ArrayList<Word> worda){

        super(context, 0, worda);

    }

    @SuppressLint("ResourceAsColor")
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {


        View listItemView = convertView;

        if(listItemView == null)
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

        Word currentWord = (Word) getItem(position);


        TextView convTextView = (TextView) listItemView.findViewById(R.id.conv_text_view);

        convTextView.setText(currentWord.getConversation());


        ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);

        if(currentWord.hasImage()) {
            imageView.setImageResource(currentWord.getImageResourceId());

            imageView.setVisibility(View.VISIBLE);
        }
        else {
            imageView.setVisibility(View.GONE);
        }

        CheckBox checkbox = (CheckBox) listItemView.findViewById(R.id.checkBox2);
        checkbox.setFocusable(false);
        checkbox.setFocusableInTouchMode(false);


        View textContainer = listItemView.findViewById(R.id.text_container2);

        textContainer.setBackgroundColor(((position % 2) == 0) ? Color.parseColor("#B2DFDB") : Color.parseColor("#80CBC4"));

        return listItemView;

    }
}



in list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tan_background"
android:orientation="horizontal"
android:id="@+id/text_container2"

    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="22dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:contentDescription="@null"
        tools:src="@mipmap/ic_launcher" />

    <TextView

        android:id="@+id/conv_text_view"
        style="@style/CategoryStyle"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:layout_height="wrap_content"
        tools:text="lutti" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="24dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
       />


</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire