1

i create new activity as favorite page and put in many textview and set visibility all of them =gone now i recive int variable from another activity with sharedprefrence() i get the value butt i dont know how to visible textview one by one i use if/switch to check value but only one textview at same time be visible how can i keep one visibility and show other textview?

here my cod

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    int score = pref.getInt("score", 0);

if (score==100) {v1();} 
else if (score==101) {v2();}
else if (score==102) {v3();}
else if (score==103) {v4();}
else if (score==104) {v5();}
else if (score==105) {v6();}
else if (score==106) {v7();}


public void v1(){
tv_fav7.setVisibility(View.VISIBLE);
tv_fav7.setText("777");
}   
public void v2(){
tv_fav6.setVisibility(View.VISIBLE);
tv_fav6.setText("666");
}       
public void v3(){
tv_fav5.setVisibility(View.VISIBLE);
tv_fav5.setText("555");
}   
public void v4(){
tv_fav4.setVisibility(View.VISIBLE);
tv_fav4.setText("444");
}   
public void v5(){
tv_fav3.setVisibility(View.VISIBLE);
tv_fav3.setText("333");
}
public void v6(){
tv_fav2.setVisibility(View.VISIBLE);
tv_fav2.setText("222");
}   
public void v7(){
tv_fav1.setVisibility(View.VISIBLE);
tv_fav1.setText("111");
}       
farzad226
  • 41
  • 7
  • You show/hide `TextView`s depending on `score` value, but `pref.getInt("score", 0);` returns only **one** value, how do you get the rest values of `score` ? – Rami Apr 14 '15 at 15:15
  • i want show textview one by one, means if user clicked button1 in this activity i show textview1 and if later clicked on button2 the textview2 will be showed, its choice of user butt i want the previous chioce of user be will be visible – farzad226 Apr 14 '15 at 15:50
  • So you need to save a *list* of last showed TextViews, not only the last one – Rami Apr 14 '15 at 16:13
  • So how can i do it ? I can get visible by user choice – farzad226 Apr 14 '15 at 16:28
  • nobody can help me ?? – farzad226 Apr 15 '15 at 03:53

1 Answers1

0

My suggestion is:

1) Use a list of booleans to save visibility state of each textview ( e.g ArrayList<Boolean> mVisibilityStateList; ) and make it as class variable.

Notice: the size of your list depends on number of TextViews.

2) Inside the clickListener for each Button, change the value of the corresponding TextView:

        //e.g: button7 to hide/show textView7

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(textView7.getVisibility()==View.GONE){
                   textView7.setVisibility(View.VISIBLE);
                   mVisibilityStateList.set(7, true);// here 7 = the position of your textview in boolean list
                } else{
                   textView7.setVisibility(View.GONE);
                   mVisibilityStateList.set(7, false);
                }
            }
        });

3) When you leave the activity save your boolean list in SharedPreferences.

        SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
        Editor editor = prefs.edit();
        try {
            editor.putString("your_pref_name", ObjectSerializer.serialize(mVisibilityStateList));
        } catch (IOException e) {
            e.printStackTrace();
        }
        editor.commit();

4) When you crete your activity (onCreate() method):

4-a) load the boolean list:

       SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

        try {
            mVisibilityStateList = (ArrayList<Boolean>) ObjectSerializer.deserialize(prefs.getString("your_pref_name", ObjectSerializer.serialize(new ArrayList<Boolean>())));
           if(mVisibilityStateList== null || mVisibilityStateList.size()==0){
              initList();// first call of the activity => load default list
           }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

initList() method:

private void initList(){
     mVisibilityStateList = new ArrayList<Boolean>();
     for(int i =0; i<numberOfYourTextViews; i++){
         mVisibilityStateList.add(false); // false because by default there are GONE
     }
}

4-b) Update the visibility of your textViews:

private void updateTextsVisibility(){

        if(mVisibilityStateList.size() == numberOfYourTextViews){
            if(mVisibilityStateList.get(0)){// if true => Text should be visible
              textView0.setVisibility(View.VISIBLE);
            } else{
              textView0.setVisibility(View.GONE);
            }

             // .... do the same thing for the rest of your TextViews

            if(mVisibilityStateList.get(7)){
              textView7.setVisibility(View.VISIBLE);
            } else{
              textView7.setVisibility(View.GONE);
            }
        } else{
          // there is something wrong
        }
}

PS:

  • This is a quick solution, maybe there is something better.

  • For saving the ArrayList to SharedPreferences i've used this code.

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
  • its work but have problem on last code , with this code only one texview is visible in same time – farzad226 May 02 '15 at 12:22
  • Did you have the right values in your array when you get it from SharedPreferences? (in your mVisibilityStateList ) – Rami May 02 '15 at 15:44