All- I have an app in which the user enters the names of players in a game. He/she can enter 2-4 players. The app takes the names and puts them into a spinner. When the user enters 4 players it works great but when they enter only 2 or 3 players, the spinner has 2 or 1 (respectively) empty spaces. How can I make it so when the user enters a number of players less than 4, only that number of names appears in the spinner (no empty spaces). Here is the code I am using:
String[] items = new String[] {"No Owner", message, message2, message3, message4};
Spinner spinner = (Spinner) findViewById(R.id.owner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
message= Player 1,
message2= Player2,
etc.
Sample code welcome, and thanks for your time.
EDIT:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(MainActivity.ANOTHER_MESSAGE);
String message3 = intent.getStringExtra(MainActivity.YET_ANOTHER);
String message4 = intent.getStringExtra(MainActivity.AND_ANOTHER);
setContentView(R.layout.next_main);
You could check which messages are empty and then modify your items array based on that information. The goal being to pass an array to your ArrayAdapter with no extra spaces in it
Edit:
List<String> playersList = new ArrayList() ;
if(!message.equals("")){
playersList.add(message);
}
etc..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, playersList);
Since you're importing your player names into a string, I would run string compare to see if each string matches the default value, if there is no player filling that slot. In other words, if
message.compareTo("")
returns 0, don't include that in items, which would be best used as an ArrayAdapter. You can do this through a simple if block.
Example code:
ArrayAdapter items = new ArrayAdapter<String>(this, int textViewResourceId);
if (message.compareTo("") != 0) {
items.add(message);
}
if (message2.compareTo("") != 0) {
items.add(message2);
}
....
And you would keep going with the rest of your items, using the resulting array (which you can pull out using toString()) to generate the Spinner.
EDIT: Fixed constructor code.
EDIT 2: Fixed textViewResourceId in the constructor.
Related
I'm trying to get the index of what the user chooses so that I can call the class object with the number from the drop-down menu.
String[] storeListArr;
Object storePick;
ArrayList<String> storeList = new ArrayList<>();
while (!(newStore[nCount] == null)) {
storeList.add(newStore[nCount].getName());
nCount++;
} //end loop
storeListArr = new String[storeList.size()];
storePick = JOptionPane.showInputDialog(null, "Choose Store", "Store Selection", JOptionPane.QUESTION_MESSAGE, null, storeListArr, storeListArr[0]);
So if the user picks newStore1 from the drop-down menu, I can call the store class and get whatever I want from it.
I'm trying to get the index of what the user chooses
The showInputDialog(...) only returns the String that was selected.
If you want to know the index then you need to use methods of the ArrayList:
storePick = JOptionPane.showInputDialog(...);
int index = storeList.indexOf( storePick );
I have a problem with adding values to list which is in SecondActivity. In MainActivity I set text in EditText boxes and send to second class. For the first time values are adding, but when I back to previous activity and one more time set text and send it, values in the list are replaced, not added. Someone know what is the source of this problem?
try this in first activity:
String[] array = {"Hi", "there", "yeah"};
Intent goIntent = new Intent(this, NewAppActivity.class);
/*
* put extra with "array" as a key and the String[] with your values as the value to pass
* */
goIntent.putExtra("array", array);
startActivity(goIntent);
and in second activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String[] array = extras.getStringArray("array");
}
I am trying to implement a button that saves integers entered into an EditText and save them into an ArrayList. I declared my ArrayList globally in my class and am calling it inside of my OnClickListener method. I am unsure whether or not I am saving to this ArrayList because I am unable to display what I have saved in said ArrayList.
My declaration of the list is;
ArrayList<String> savedScores = new ArrayList<String >();
This is what I am using to save to my ArrayList;
`savedScores.add(input1.getText().toString());`
Now, in my OnClickListener method, I have a button that saves user input into the ArrayList (or so I am hoping), and another to display what I have saved. However, when I click on the "editScore" button, the TextEdit is cleared as if I have nothing saved in my ArrayList. This is simply a test to see if I am properly saving to my array and any help would be much appreciated! Thank you.
switch (view.getId()) {
case R.id.buttTotal:
if (blankCheck.equals("")) {
Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
blankError.show();
break;
} else {
int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
int sum = num1 + score2;
score2 = sum;
output1.setText("Your score is : " + Integer.toString(sum));
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
break;
}
case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
output1.setText("you messed up");
break;
case R.id.editScore: //Need to set up Save Array before we can edit
output1.setText(savedScores.get(0));
break;
}
Because you are saving empty values into your ArrayList. See here
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
The value of input1 is empty. Clear the input after you saved it to the array.
I have three Edit Text on my activity and i will like to add each value to array if the Edit Text not empty.
Here is what i want:
EditText 1, EditText 2, EditText 3
public static ArrayList<String> arrayValue = new ArrayList<String>();
public static ArrayList<String> arrayTitleValue = new ArrayList<String>();
if edittext 1 not empty then
arrayTitleValue.add("Text Box Value 1")
arrayValue.add(EditText 1.getText.tostring)
if edittext 2 not empty then
Add edittext 2 to variable string e.g arrayValue.add(EditText 2.getText.tostring)
arrayTitleValue.add("Text Box Value 2")
if edittext 3 not empty then
Add edittext 3 to variable string e.g arrayValue.add(EditText 3.getText.tostring)
then finally: print the result this way:
System.out.println("Title Result: "+arrayTitleValue+" Array Value: "+arrayValue);
so at the end of the day i want my result to look like this:
Result value for arrayValue= Result1(edit text 1 value),Result2(edit text 2 value),Result3(edit text 3 value)
Result value for arrayTitleValue= ResultTile1("Title 1"),ResultTitle2("Title 2"),ResultTitle3("Title3")
i just want very fast way to achieve this task any help is welcome
You first have to give an id to each of your EditText in your layout file (.xml)
android:id="#+id/edittext_1"
After in your activity file (.java) you get an EditText object corresponding to each EditText :
EditText editText1 = (EditText) findViewById(R.id.edittext_1);
Then you check for your EditText to be filled and if it is, add its value to the List :
if(!editText1.getText().toString().equals("")){
arrayValue.add(editText1.getText().toString();
arrayTitleValue.add("Text Box Value 1");
}
Hi I'm very new to android and I'm trying to do the following. Provide some word completion for the sentence.
Here is the code I have for it:
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
// In the onCreate method
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
where it automatically pop up when user types Belgium. But the problem I face is, when the user type this sentence:
Belgium France
for the first word Belgium the pop up comes, when I type France autocompletion doesnt work. Why?
Where I'm making the mistake?
Thanks in advance.
You should use MultiAutoCompleteTextView for multiple words
Here is a good example of that
Comma is the default separator for MultiAutoComplete, you can set it to space delimiter, please have a look at this post to achieve that.
Have a look at this post to for space delimiter.
You are searching for "Belgium France", but you don't have this item in your list (in one String object). You will have to create custom Adapter which implements Filterable with custom Filter.