-3

i am beginner in android programming and English language :) !! i want to get names from user and show the names that user inputs randomly by pressing a button. how can i store a user input names in a array list? and As regards I don't know how many names he wants to enter and don't show a name twice and after showing the name erase that name in array list. tanks a LOT.

package farshid.mk.teststringarraylist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class StringArraylistActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ArrayList<String> inputs = new ArrayList<String>();
1
  • is that all you´ve done on code? Commented Nov 20, 2014 at 14:37

2 Answers 2

0

Well, there are many ways to do this but one would be:

  1. Create an EditText, a view that allows user input
  2. Create a Button that the user would click to indicate that he/she is done entering the name in the EditText
  3. Create an OnClickHanlder for the button so that you can get the text from the EditText, convert it to a String (EditText returns an Editable object), and add it to your ArrayList<String>

    // add the string
    myArrayList.add(myEditText.getText().toString().trim());
    

To remove duplicates from the List you will need to convert it to a Set... google "java arraylist remove duplicates (First Link)

Also, in Java, it's best practice to use Generics:

// psuedocode
List inputs = new ArrayList

There are many ways you could display this list of text to the user in Android, you will need to research that on your own.

You simply clear the ArrayList when you are done with the names.

This should get you going =)

Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain more about the benefit of generalization in Java you've just mentioned? I'm genuinely curious on this..
I think it's best to send you over to the Java Tutorial. The have a great lesson there. docs.oracle.com/javase/tutorial/java/generics/index.html It may not apply directly to this situation... more of an FYI =)
0

There's a lot of things you'd need to do to achieve this:

  • Define the EditText in your layout's XML.
  • Get a reference to it from the Activity by calling findViewById(R.id.your_edittext_id).
  • From that reference, call getText().toString() to get the value from the EditText.
  • Add that String to your ArrayList.

That's basically the main idea behind what you're trying to achieve. I could give you the code but then perhaps its best for you to do it yourself (so that the next time you need to do this, you'd understand the concept).

Good luck! :)

Comments

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.