0

Why is the follow array assignment not working...

working part....

String[] values = new String[] { "Android","BlackBerry","HardCore"};
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) 
   {
      list.add(values[i]);
   }

not working part...whit I add the following code to that onclick of a button the app crashes can anyone help...my ultimate goal is to get the text from a edittext view from the screen and place in into the list...however I and trying to do the following first

public void onClick(View view) 
   {
   list.add(new String("testing"));                 
   adapter.notifyDataSetChanged();
   }
4
  • 3
    Welcome To StackOverflow. Are you getting any exception? If so, post the stacktrace. Commented Dec 19, 2013 at 14:41
  • 4
    I would recommend not to use an ArrayList because it is not typecasted... The generic supporting alternative would be List Commented Dec 19, 2013 at 14:45
  • 1
    How does ArrayList<String> even compile? ArrayList has no type parameters. Did you mean List<String>? Commented Dec 19, 2013 at 14:46
  • sorry now started android..how do one "post the stacktrace"? Commented Dec 19, 2013 at 16:34

2 Answers 2

1

Is onClick() supposed to be an event handler for a button click? If so, the signature is wrong.

I believe you are looking for something like this. I have changed it from an ArrayList to a generic List. And initialized it with a collection initializer, and made it accessible from within your function. Please run the code below and let us know if you get exceptions.

List<string> MyList=new List<string>() {"Android", "Blackberry", "Hardcore"};

public void onClick(View view)
   {
   MyList.Add("testing");
   adapter.notifyDataSetChanged();
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Design error with the Lst<string>....saying "Syntax error on token(s), misplaced construct(s)"
@user3103307 Are you programming in Java, or in C#? And make sure you check what your post for typos, to reduce confusion.
0

ArrayList<String> list = new ArrayList<String>(); can this sentence be compile successfully? it cause compiling error in my VS2010 and .NetFramework3.5. I think you could use List<string> list = new List<string>(); to replace it.

1 Comment

Design time error saying "Cannot instantiate the type List<R.string>" at the List<string>....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.