0

I'm new to android, and I'm getting an exception when I'm using the add function of ArrayAdapter for some reason.

I know this process should be trivial but i can't find my mistake.

I'm trying to add records of date & time to a ListView.

Here is how I'm doing it:

File Name: ClockTest.java

/* package name here */

/* import section here */
public class ClockTest extends Activity {

/** Called when the activity is first created. */   
public int logButtonState;
public int logEntriesCount;
ImageButton logButton;
ImageButton switchButton;
ListView entriesList;

public ArrayAdapter<String> listAdapter;

public String logEntries[]={
        "Entry1",
        "Entry2",
        "Entry3",
        "Entry4",
        "Entry5"};

public String date_info;
public String time_info;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // State: 0=login, 1=logout
        logButtonState = 0;
        logButton = (ImageButton)findViewById(R.id.log_button);
        switchButton = (ImageButton)findViewById(R.id.switch_button);

        // Infrastructure for entries on-screen presentation
        logEntriesCount = 0;

        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,                                                     
                                             logEntries);

        entriesList = (ListView)findViewById(R.id.entries_list);
        entriesList.setAdapter(listAdapter);

        // add a click listener to the exit button
        logButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView timeCaptured = (TextView)findViewById(R.id.timeCaptured);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(System.currentTimeMillis()));

            date_info = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
                        + "."
                        + String.valueOf(c.get(Calendar.MONTH))
                        + "."
                        + String.valueOf(c.get(Calendar.YEAR));

            time_info = String.valueOf(c.get(Calendar.HOUR_OF_DAY))
                + ":" 
                + String.valueOf(c.get(Calendar.MINUTE)) 
                + ":" 
                + String.valueOf(c.get(Calendar.SECOND));

            final String currentTime = asignLogString()
                                + " ["
                                + date_info
                            + " "
                            + time_info
                                + "]";

            logEntriesCount++;
            timeCaptured.setText(currentTime);

            //listAdapter.clear();
            // re-arrange on-screen list view  
            listAdapter.add(currentTime);
            listAdapter.notifyDataSetChanged();             
            entriesList.setAdapter(listAdapter);
              }
         });     
     }
 }
inazaruk
  • 74,247
  • 24
  • 188
  • 156

1 Answers1

0

The ArrayAdapter has an unmodifiable list. You won't be able to add things to it, else you'll get an UnsupportedOperationException (from what I remember).

Instead of using a String[], use an ArrayList<String>.

Take a look at Populate Spinner dynamically in android from edit text for a solution.

Community
  • 1
  • 1
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • **Thank you Bruce!** you saved me allot of frustration, time and effort. – arik android_senior May 27 '11 at 14:06
  • Bruce, from the place I'm coming from (Israel), it is customary to bless one that helps. my bless to you is that one of your great wishes (for the good) would come true till the end of this year. and again, thank you. – arik android_senior May 27 '11 at 14:14