1

I need to display current values in TextView (after removing String). I'm adding String when button is On and I need to remove it, when it's Off ( I don't know if I do it well), next I need to display Strings in TextView without deleted String. I need to display only Strings from On buttons. Here is my code:

public class Calc extends ActionBarActivity {

    TextView display;

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

        display = (TextView)findViewById(R.id.textView2);
        display.setText("Add item");
    }

    static boolean isempty=true;

    public void changeButton(View sender) {
    ToggleButton btn = (ToggleButton) sender;

    ArrayList<String> mActiveToggles = new ArrayList<String>();

    String b = btn.getText().toString();

    boolean on = ((ToggleButton) sender).isChecked();

    if(on) {
        if (isempty) {
            if (b.equals("0")) return;
            display.setText(btn.getText());
            mActiveToggles.add(b);
            isempty = false;
        } else {
            display.append(btn.getText());
            mActiveToggles.add(b);
        }
    }
    else
    {
        if (b.equals(btn.getText()))
        {
            mActiveToggles.remove(b);
            display.setText(mActiveToggles.toString());

        }
    }
}
calvinklein
  • 31
  • 1
  • 7

2 Answers2

0

Edit based on new information:

To display the texts from all the toggles that are currently ON in your TextView I suggest that you create a

private ArrayList<String> mActiveToggles = new ArrayList<String>();

Whenever a toggle button is set to ON, you add the label of that toggle button to the list. Whenever a toggle button is set to OFF, you remove the label from the list.

if(on) {
    if (b.equals("0")) return; // Don't really know why you need this one...

    // If the button is ON, add its text label to the list.
    mActiveToggles.add(btn.getText());
} else {
    // If button is OFF, remove its text label from the list.
    mActiveToggles.remove(btn.getText());
}

To update your TextView simple iterate over the items in the list and add them to the TextView (with an appropriate separator). Something like this:

StringBuilder allLabels = new StringBuilder();
for(String s : mActiveToggles) {
    if(allLabels.length() > 0) {
        allLabels.append(" "); // some divider between the different texts
    }
    allLabels.append(s);
}
display.setText(allLabels.toString());

If you really do need the content of your TextView to be persistently stored, iterate over the list items and concatenate them and then store them to your SharedPreferences object. Restoring them from storage would simply be the reverse operation of splitting the String and populating the list. Based on the code that you've provided I don't see a need for this, but there could of course be more to it than what you've posted here.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • It doesn't work, because it clears everything and setText to button wich is Off. I need to display Strings from buttons wich are On. – calvinklein Feb 26 '15 at 18:45
  • well in that case just move the line where you set the text to be above the line where you remove it. – TofferJ Feb 26 '15 at 21:33
  • It doesn't work. I've just read that key can store only one value if it's true it can't work. Maybe some other ideas? – calvinklein Feb 26 '15 at 22:44
  • Yes you can only store one value. If you want to store multiple values you'll have to explain a bit more in detail what values you want to store and how to use them. You could e.g. chain the values valueA|valueB|valueC in your SharedPReferences entry. But I doubt that it's the best way. Right now there's just too many question marks for me to provide a good solution. – TofferJ Feb 26 '15 at 23:24
  • Seriously I want to do something like opposite to append and I'm looking for solution. My app is about adding items to TextView when ToggleButton is On, but when it's Off - String from ToggleButton needs to be removed from TextView. There would be a lot of buttons, so I only want to remove name from a button which is Off. For example when buttons are On it can show ToggleButton1+ToggleButton2+ToggleButton3, but if ToggleButton2 is Off, TextView will show ToggleButton1+ToggleButton3 – calvinklein Feb 27 '15 at 02:00
  • I've edited my question. Could you tell me how to display ArrayList in TextView? display.setText(mActiveToggles.toString()); doesn't work. – calvinklein Feb 27 '15 at 20:52
  • It has made display empty :( – calvinklein Mar 03 '15 at 00:26
  • I found solution here http://stackoverflow.com/questions/8726272/get-checked-items-from-listview-in-android but it's for ListView – calvinklein Mar 03 '15 at 00:54
  • Well that solution could be applied to your problem too. I've added some more sample code for you. I'm not going to write the whole thing for you. :) You still need to use my examples and put them together yourself. That's how you learn. – TofferJ Mar 03 '15 at 14:49
0

For getting strings from shared preferences you have to use :

if(sharedpreferences.contains(key_value)){
textview.setText(sharedpreferences.getString(key_value, null));
}

If there any issue you get please let me know.

Surender Kumar
  • 1,123
  • 8
  • 15