0

i am quite new at coding for android. I tried to use SavePreferences in order to save User Inputted data for a ListView. However this method resulted in only saving the last item that the user inputted, not the entire ListView.(i think this was because i was overwriting the key so that it would only show the last value)

Then it was suggested that i should use a JSONArray but I could not understand what happened.

My Code using JSONArray is below: Note: there are a bunch of errors on the JSONArray since i dont think i used the correct Key.

public class TaskPage extends SherlockActivity {

EditText display;
ListView lv;
ArrayAdapter<String> adapter;
Button addButton;
ArrayList<String> dataSetarrlist = new ArrayList<String>();



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

    display = (EditText) findViewById(R.id.editText1);
    lv = (ListView) findViewById(R.id.listView1);
    addButton = (Button) findViewById(R.id.button1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
    lv.setAdapter(adapter);


    LoadPreferences();




    // setChoiceMode places the checkbox next to the listviews



    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String task = display.getText().toString();


                adapter.add(task);
                adapter.notifyDataSetChanged();



                SavePreferences("LISTS", task);
        }
    });
}

protected void SavePreferences(String key, String value) {
    // TODO Auto-generated method stub
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
    JSONArray array = new JSONArray(data.getString(key, value));

    SharedPreferences.Editor editor = data.edit();
    editor.putString(key, value);
    editor.commit();


}

protected void LoadPreferences(){
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);

    JSONArray dataSet1 = new JSONArray(data.getString("LISTS", "None Available"));
    for(int i = 0; i<dataSet1.length(); i++)
         adapter.add(dataSet1.getString(i));
         adapter.notifyDataSetChanged();

    adapter = new ArrayAdapter<String>(this, 
                         android.R.layout.simple_list_item_multiple_choice,dataSetarrlist);
    lv.setAdapter(adapter);


    // setChoiceMode places the checkbox next to the listviews
}

I am quite confused at this point so can someone please show me where Im going wrong and provide some example code. I almost certain it is because I am not using the key and value correctly to save the data but i cant seem to get this correct Lastly, is there another method where I dont have to use the JSONArray so that it will still show all the user inputs in the listview.

EDITED CODE IS BELOW

addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String task = display.getText().toString();


                adapter.add(task);
                dataSetarrlist.add(task);   
                adapter.notifyDataSetChanged();
                SavePreferences("LISTS", dataSetarrlist.toString());
        }
    });
}

protected void SavePreferences(String key, String value) {
    // TODO Auto-generated method stub
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = data.edit();  
    editor.putString("LISTS", dataSetarrlist.toString());
    editor.commit();


}

protected void LoadPreferences(){
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
    String dataSet = data.getString("LISTS", dataSetarrlist.toString());

    ArrayList<String> dataSetarrlist = new ArrayList<String>();
    dataSetarrlist.add(dataSet);
    adapter = new ArrayAdapter<String>(this, 
                         android.R.layout.simple_list_item_multiple_choice,dataSetarrlist);
    lv.setAdapter(adapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);



    // setChoiceMode places the checkbox next to the listviews
}
user1949400
  • 63
  • 2
  • 7

1 Answers1

0

Your TextView (display) only contains 1 value. On the OnClickListener of addButton, you should add "task" on your array & pass the array to SavePreferences instead of "task":

addButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String task = display.getText().toString();
        adapter.add(task);
        dataSetarrlist.add(task);
        adapter.notifyDataSetChanged();
        SavePreferences("LISTS", dataSetarrlist);
    }
});
}

You should modify SavePreferences to process the ArrayList.

dannyroa
  • 5,501
  • 6
  • 41
  • 59
  • i tried this method, however theres an error that says i cant have an ArrayList as one of the arguments in SavePreferences() – user1949400 Jun 27 '13 at 21:57
  • Try dataSetarrList.toString(). Not sure if that works.Or you need to iterate on the items in your ArrayList one by one & concatenate them into a string. Then in your LoadPreferences, you need to convert the string into an ArrayList. – dannyroa Jun 27 '13 at 22:01
  • ok dataSetarrlist.toSTring() worked to an extent. Now it is saving the inputted values but it is putting the Arraylist into 1 block in the ListView rather than separate blocks. I will update my code below. Any ideas? – user1949400 Jun 27 '13 at 22:16
  • You need to convert the string into an ArrayList. See http://stackoverflow.com/questions/7347856/how-to-convert-a-string-into-an-arraylist. Then you have to add the ArrayList into the adapter. – dannyroa Jun 27 '13 at 22:26