How to select any item from AutoCompleteTextView programmatically in Android Studio? - android

AutoCompleteTextView autoCompleteTextView
ArrayAdapter<String> adapter;
ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// code...
list.add("First");
list.add("Second");
list.add("Third");
list.add("Forth");
list.add("Fifth");
autoCompleteTextView = findViewById(R.id.auto_complete);
adapter = new ArrayAdapter<>(this, R.layout.drop_down, list);
autoCompleteTextView.setAdapter(adpater);
}
then, after adding some strings to
ArrayList and linking that list with autoCompleteTextView using Adapter.
autoCompleteTextView.setSelection(3);
Now I want any of my item getting selected automatically, once I open this activity. I treid setSelection() method, but it is not working.

We need to pass false as second argument to setText() method.
autoCompleteTextView.setText("First", false);
If we don't pass second arguiment "false" as a filter to setText() method, it will clear all the entries inside list and will keep just one entry. that we set as using setText() method.

your selected value will store selectedItem
#Override
protected void onCreate(Bundle savedInstanceState) {
// code...
autoCompleteTextView = findViewById(R.id.auto_complete);
list.add("First");
list.add("Second");
list.add("Third");
list.add("Forth");
list.add("Fifth");
adapter = new ArrayAdapter<>(this, R.layout.drop_down, list);
autoCompleteTextView.setAdapter(adpater);
autoCompleteTextView.setThreshold(1);
String selectedItem = autoCompleteTextView.getText().toString();
}

Related

Edit Text and put it to List View

I'm kinda new to Android Studio so I will be grateful if you can help me with my question. How can I enter some text and add it to a ListView with the use of a Bundle?
For example, say I enter a name in the EditText component in the MainActivty, and then when I press the OK button, it will be seen in to another Activity in a List View.
I've been using Bundles to transfer text to another Activity but I can't figure out how to transfer text to ListView.
If all you want to do is display strings in your list items, you can do it fairly simply, otherwise you will have to make a custom adapter. For the former, this is what you will want to do
Create an ArrayList<String> where you will store your values that are entered from the EditText.
Create an ArrayAdapter<String> that will serve to connect the ArrayList to the ListView.
Your final code will look something like this:
public class MainActivity extends Activity {
private ListView listView;
List<String> strings;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.yourLayout);
listView= (ListView) findViewById(R.id.yourListView);
strings = new ArrayList<String>();
strings.add("list item 1");
strings.add("list item 2");
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
strings);
listView.setAdapter(arrayAdapter);
}
}
Then in the onClick of the "OK" button simply add the string from the EditText to the arrayList, and notify the listView that it should be updated be calling this:
arrayAdapter.notifyDataSetChanged();
I don't think you can use Bundles for what you want to achieve. Here's how I do it...
First you need to set up your list. I would use global variables:
ListView listView;
EditText editText;
ArrayList<String> strings;
ArrayAdapter<String> adapter;
Now, in onCreate() , you can do...
listView = (ListView) findViewById(R.id.your_listview);
editText = (EditText) findViewById(R.id.your_edittext );
strings = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
You will need to set the OnClickListener for your Button. I would do this in onCreate() too:
Button button = (Button) findViewById(R.id.your_button);
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here we get the text from the EditText component
String text = editText.getText().toString();
// Now add it to the list
strings.add(text);
// And finally, update the list
adapter.notifyDataSetChanged();
}
});

Android: ListView displays only last values saved in an array

hi i have tried the following code to store the user input values of EditText to an array in another activity and display the values in the array. every time the user input values in EditText of Activity01. it should be stored in an array on the Activity02 to display all the values of user inputs.
The ListView displays one last entered value from Array.
This is the code what i have tried.
Activity01.java
Button GrmBtnSave = (Button) findViewById(R.id.grmbtnsave);
Grmitem = (AutoCompleteTextView) findViewById(R.id.itemsearch);
GrmBtnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent ( ItemSearch.this, MainInvoiceActivity.class );
intent.putExtra("GRM_ITEM", Grmitem.getText().toString());
startActivity(intent);
}
}
});
Activity02.java
ListView lv1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
//.....
//.....
lv1=(ListView)findViewById(R.id.getalllist);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv1.setAdapter(adapter);
String val="";
if(getIntent().getStringExtra("GRM_ITEM")!=null && getIntent().getStringExtra("GRM_ITEM").length() > 0){
val = getIntent().getStringExtra("GRM_ITEM");
list.add(val);
}
adapter.notifyDataSetChanged();
}
I tried solving it but still i get the last entered value in the EditText.
Thank you for help
The static keyword will do the magic.
Make change in declaration of list in your activity02.java like this. I have tested it. and it works.
static ArrayList<String> list = new ArrayList<String>();

Updating the ListView

I'm working on a program that uses a ListView which contains strings that a user enters. I want it to list out each string after the user clicks the button. Also I want every string value that is displayed in the ListView to also be in an array so that I can manipulate the list later. This is what I have right now:
public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView1);
choicesArray = new String[] { "You have not entered a choice yet" };
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
choicesArray);
listView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.Button1);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//choices.add(message);
//choicesArray = choices.toArray(choicesArray);
//listView.setAdapter(adapter);
}
});
}
This code creates an error:
FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)
The reason your list is not updating is that you are not ever adding your data items to the Adapter. You are only adding them to choicesArray which is the array that you passed to the new ArrayAdapter() constructor.
That constructor does not tie the adapter to the array, but basically copies the array into the adapter (at the time that you call the constructor) Changes made to the array after that will not be reflected in the ArrayAdapter.
I like to think of it that ArrayAdapter<> is basically the same thing as ArrayList<> with the added bonus of being able to generate child views for some AdpaterView parent. You can pass it an array[] to start with and it will initialize itself with the values in that array, but it does not link the array to the to the Adapter, or to the ListView. After that initial constructor call any changes to either one will not be reflected in the other.
So you should change your code to look like this:
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
//listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
}
});
Edit:
change your ArrayAdapter constructor and remove the choiceArray parameter so you should be left with this:
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
after that I think you should be good to go. Essentially the problem was that since you were passing in an array, and since arrays are of a fixed size (yours was length = 1) it was not allowing you to add anything. I suspect you could probably use an ArrayList<String> instead of String[] if you wanted, but honestly for your situation I think it will be easier to just skip that parameter all together and add items directly to your adapter whenever you need to add new ones.
after adding the new String to your list you just use notifyDataSetChanged() of your adapter which tells the adapter to update the display and it'll update the ListView with the new string
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
choicesArray = choices.toArray(choicesArray);
adapter.notifyDataSetChanged();
}
});
Update
you should consider making this change too
choicesArray = new String[choices.size()];
choicesArray = choices.toArray(choicesArray);

Android Spinner add items

I'm working on an App and need some help with the spinner widget.
The array has no entries by its creation and will be filled in another function. So I have to use adapter.add(item); and spinner.setAdapter(adapter); to update the spinner in this function, I guess.
// global variables
String[] test = new String[100];
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test[x] = String.valueOf(x);;
x++;
adapter.add(test[x]);
s.setAdapter(adapter);
}
just for education purpose I tried this, but there is still a bug in the onClick() function.
Something has to be done with the ArrayAdapter, I guess. But I don't familiar with this and didn't found an example for adding itmes by pressing a button.
Maybe there is just a simple failure somewhere else, but currently I just don't see it.
here the complete code:
ArrayList<Integer> test;
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
test = new ArrayList<Integer>();
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test.add(x);
x++;
s.setAdapter(adapter);
}
You are adding x to an array element then passing this element to the adapter.add method. This is effectively the same as using: adapter.add(x);.
Why not use an ArrayList instead of a array. ArrayLists are dynamic, and do not need to be given an initial size.
A very quick check has shown me that there is a constructor for an Array Adapter that is compatible with ArrayLists and I expect any descendant of the List class. See the code example below.
ArrayList<Integer> test = new ArrayList<Integer>();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
I know this is not a complete solution to your problem, but just a pointer to a possibly better way of doing it.
Do a web search with the terms ‘android updating arrayAdapter at runtime’ and I am sure you will stumble upon a more concrete example.
The problem is you are setting the ArrayAdapter's data to be a 100 null strings, and then adding to the end of that when you press the button. Try changing your test variable to be new String[0].
Then in your onClick method, just call adapter.add(String). It will add it to the end of your current data, so you don't need to give it a big enough array initially.

Populate ListView with Dynamic Array

I have a EditText box so a user can input names and then click the Add button and the name saves to the array playerList and clears the box so another name can be added.
I also have a ListView on the same Activity which will then be populated by the names in the Array playerList. The problem is that the ListView dosen't seem to be populated.
So I tried with a defualt set String teststring which you can see below and that populates the ListView fine. My question is how come it isn't working with the Array playerList maybe its not saving to the Array correctly?
Update just need adapter.notifyDataSetChanged(); adding to refresh the ListView Credit to #Lalit Poptani and #Jave
ArrayList<String> playerList = new ArrayList<String>();
ListView listview;
protected String[] teststring = {"Name 1", "Name 2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teststring);
ListView employeeList = (ListView) findViewById(R.id.namelistview);
employeeList.setAdapter(adapter);
Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
String name = playername.getText().toString();
playerList.add(name);
playername.setText("");
}});
To refresh the ListView after you add new data to it you have to call adapter.notifyDataSetChanged(). This is refresh your ListView will new data.
But in your case you are populating your ListView with String[] so it won't work dynamically you will have to give the size of the String[]. So, I will suggest you to populate your ListView with ArrayList itself for adding the content dynamically.
ListAdapter adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1,playerList);
You should call adapter.notifyDataSetChanged() after adding new items to make it update with the new data.
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.

Categories

Resources