I got a AutoCompleteTextView , but I want it to use the users' previous input instead of some strings I said it to complete, so question is: How do I do this?
AutoCompleteTextView do not propose this feature by default but you could implement it yourself.
storing each user input in a file or a database (I recommend to use a database)
retrieving those input and setting an adaptater to the AutoCompleteTextView.
Example to set an adpatater with pre defined string array from Google doc:
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Instead to use a fix array COUNTRIES like in the example above, use a String array retrieved from the database,
If you want your AutoCompleteTextView to show suggestions based on some string rather than taking input from user then just call:
myAutoCompleteTextView.setText("cou", true);
and then in case drop down is not shown, call:
myAutoCompleteTextView.showDropDown();
Related
I'm creating a lyric app and I need some help in coding the next processes I need.
I created a ListView and added some Strings on it.
public class MainActivity extends AppCompatActivity {
String titles[] = new String [] {"Amazing Grace", "How Great Thou Art",
"King of All Kings", "What A Beautiful Name"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.titlelist);
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,titles);
listView.setAdapter(adapter);
}
}
Now the next step is to create an OnItemClickListener and let's say
if "Amazing Grace" was selected from the list,
it will look for a file the same name as it is defined in the String.
For example : "Amazing Grace.xml" //even with the space included
so the logic will be like : open filelocation/"title that was selected".xml
I can't use "case" since I will be creating lots of song titles and add more as I update the app.
Thanks for reading, I'd really appreciate any help with this ;)
You should map Your lyrics, so when You click on item with pos = 5 You will know what item's ps correlates with what file(or xml).
Here is the sample how to map ids with filenames:
HashMap<String,Strin> lyricsMap = new HashMap<>();
lyricsMap(0, R.raw.song_lyric0);
lyricsMap(1, R.raw.song_lyric1);
lyricsMap(2, R.raw.song_lyric2);
lyricsMap(3, R.raw.song_lyric3);
lyricsMap(4, R.raw.song_lyric4);
lyricsMap(5, R.raw.song_lyric5);
lyricsMap(6, R.raw.song_lyric6);
//..
Here is the sample how to use OnItemClickListener:
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
int rawResId = lyricsMap.get(pos);
//here comes the method for returning lyrics for file by it's resource id
//...
}
};
adapter.setOnItemClickListener(onItemClickListener);
P.S I assume You are not working on database items, otherwise You should use id instead of pos value.
To open file:
int selected = 0; // set selected to index of what is selected
File file = new File(Environment.getExternalStorageDirectory(), //folder location where you store the files
titles[selected]+".xml"); //in case of xml files. If other types, you'll need to add case for diff types
Uri path = Uri.fromFile(file);
Intent fileOpenintent = new Intent(Intent.ACTION_VIEW);
fileOpenintent .setDataAndType(path, "application/xml"); //for xml MIME types are text/xml and application/xml
try {
startActivity(fileOpenintent);
}
catch (ActivityNotFoundException e) {
}
Your biggest issue as you explained it was how to handle multiple file names. That's this part of code: titles[selected]+".xml"
Pretty new to Java in general. I have 3 Spinners in my code, and my 2 spinners would display lists depending on 1 main spinner ( which has 2 selections). After reading a few threads I read about refreshing the lists using notifySetDataChanged(); but the spinner lists never changed. Few questions :
Am I using the notifySetDataChanged correctly?
Is there another way to populate the lists?
Is the IF function the appropriate method?
Here's the code upto the onCreate method.
public class MainActivity extends Activity {
private Spinner spinner1, spinner2, spinner3, spinner4;
private Button convertButton;
private EditText from;
private List <String> list1 = new ArrayList<String>();
private List <String> list2 = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
from = (EditText) findViewById(R.id.amount);
//spinners for units
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner4 = (Spinner) findViewById(R.id.spinner4_main);
List<String>list4 = new ArrayList<String>();
list4.add("Distance");
list4.add("Weight");
//adapter for main scale
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter <String> (this,
android.R.layout.simple_spinner_dropdown_item, list4);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(dataAdapter4);
//adapter for "from" currency
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
//adapter for "to" currency
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
Object choice = spinner4.getSelectedItemPosition();
if (choice.equals("Weight")) {
//units to convert from
dataAdapter1.clear();
dataAdapter1.add("Milligrams");
dataAdapter1.add("Grams");
dataAdapter1.add("Kilograms");
dataAdapter1.add("Metric Ton");
//units to convert to
dataAdapter2.clear();
dataAdapter2.add("Milligrams");
dataAdapter2.add("Grams");
dataAdapter2.add("Kilograms");
dataAdapter2.add("Metric Ton");
}
else //(spinner4.getSelectedItem().toString().equals("Distance"))
{
//spinner1 = (Spinner) findViewById(R.id.spinner1);
dataAdapter1.clear();
dataAdapter1.add("Millimeter");
dataAdapter1.add("Centimeter");
dataAdapter1.add("Meter");
dataAdapter1.add("Kilometer");
dataAdapter2.clear();
dataAdapter2.add("Millimeter");
dataAdapter2.add("Centimeter");
dataAdapter2.add("Meter");
dataAdapter2.add("Kilometer");
}
dataAdapter1.notifyDataSetChanged();
dataAdapter2.notifyDataSetChanged();
}`
If anyone could explain what's wrong please enlighten this newbie. =)
U are approaching this wrong. Here is what you should do..
Define Spinner 1, 2 and 3.
Define arrayAdapters for spinner 1,2,3.
Populate ArrayAdapter and define this as the adapter for Spinner 1 (spinner1.setadapter(arrayadapter1)
Then call the spinner1.setOnItemSelectedListener. In the method onItemSelected, populate the logic to populate the array for spinner 2 and spinner 3 as per your requirement. Then call spinner2.setadapter(arrayadapter2) and spinner3.setadapter(arrayadapter3).
This should work.
So initially, spinner2 and spinner3 will not have any content. even if user clicks, there will be nothing in the dropdown. But once Spinner1 is selected by user, spinner2 and spinner3 will have dropdown values.
Please read up on setOnItemSelectedListener. Lots of tutorials are available. You can refer here to get a start.
If this works for you, please accept my answer.
Object choice = spinner4.getSelectedItemPosition();
This can't work.
getSelectedItemPosition()
returns an int (documentation) which won't equal your String as in
if (choice.equals("Weight")
Never ever (except in rare occasions) use the Object class as a reference, as any and every object in Java will be an Object (duh!) so you probably compare apples with oranges (which both are fruits). Seems like you're coming from a more weakly typed language, eh? ;)
Solution: Compare the positions:
if (spinnerX.getSelectedItemPosition == 0)...
Also, are you sure you want to change the content of spinner1 if something is selected on spinner4?
I'm using a SimpleAdapter to fill my ListView with below code.
SimpleAdapter adapter = new SimpleAdapter(this,
saleDriver.getOutstandings(clientId),
R.layout.outstanding_list_row, new String[] { "sale_id",
"sale_date", "invoice_number", "sale_total", },
new int[] { R.id.tt_check_box, R.id.tt_invoice_date,
R.id.tt_invoice_no, R.id.tt_invoice_tot });
setListAdapter(adapter);
According to above code, i have bind sale_id with CheckBox (R.id.tt_check_box) in the listview. When i run the program, value of checkboxes displayed right of the CheckBox as text. but i don't want to display them.
My actual need is, when user checked checkboxes, i need to get sale_ids bind with them.
How could i access sale_ids bind with checked checkboxes in my java programe ?
use
android.R.layout.simple_list_item_multiple_choice
I've attached code to obtain listview that works with multichoice checkboxes..
public class MultiChoiceActivity extends Activity {
String[] choice = { "Choice A",
"Choice B", "Choice C", "Choice D", "Choice E"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
choiceList = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
choice);
choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
choiceList.setAdapter(adapter);
}
Refernce Link
Override the getView() function in the SimpleAdapter and use the value of sale_id to check/uncheck the checkbox. Then use this custom adapter for your list.
EDIT:
In looking at another answer, my refined guess is that you need the Multiple Choice solution rather than this (since you need to find out the selections). This solution is more to display the checkbox based on existing data. If you still need this, let me know and I will post the sample code once I have access to my code.
So I'm gunna try give as much information as I can to get this sorted. My application contains a database with the standard methods implemented; retrieving a record or all records returns a Cursor.
My application has a ListView, a text box and an add button. Here's what I'm trying to achieve (please note, the first is the most important):
I would like to display the current contents of the database in the ListView area.
I want to have the button insert whatever is in the text box into the database (and the ListView should automatically update to show the insertion)
I would like the ability to tap an item in the ListView and have it deleted from the database.
I have tried to tackle the first bullet point through assigning a Cursor to the return of the getAllRecords() method; Cursor c = dba.getAllRecords();. I have tired to get it to add field entries via a for-loop which didn't turn out too well.
Button add;
EditText tm;
ListView lv;
DBAdapter dba = new DBAdapter(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general);
add = (Button) findViewById(R.id.button1);
tm = (EditText) findViewById(R.id.editText1);
lv = (ListView) findViewById(R.id.generalList);
Cursor c = dba.getAllRecords();
c.moveToFirst();
// Trying to add database contents to ListView here.
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
insertIntoDatabase();
}
});
}
If you have a Cursor from a database and whant to show into a list, and is simple data like a few text values. Take a look at SimpleCursorAdapter.
The situation is this, you need an adapter, the adapter is the one that loads from the cursor and push into the layout that represent the correspoding list item. Anything that inherits from a CursorAdapter is good. All depends on how much flexibility you need, so you may implement as much as you need.
This is a snnipet from a sample app that I posted at github.
https://github.com/soynerdito/trainningSample/blob/master/src/com/example/sample/app/Dashboard.java
Also the app is in the marketplace. Is just a very simple app used during a "trainning" the app is called "Nerdito Sample" search for it, try and the code is on github. Adds items to a database and show in list.
Sample:
Device device = new Device();
Cursor cursor = mdb.get(device);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[] {
device.mDescription.mName, device.mDeviceID.mName },
new int[] { android.R.id.text1, android.R.id.text2 }, 0);
// Create Cursor and set to List
mDeviceListView.setAdapter(adapter);
This is the first time I am trying to use a spinner and I need some help.. I made a layout with a spinner object on it and then I also made a array.xml with the numbers that I would like in the spinner in it. I run the following code and the screen displays without any values in my spinner??
public class SpinnerExaple extends Activity {
private Spinner numbersSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.numbersSpinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> numbersArray =
new ArrayAdapter<String>(this, R.layout.main,
getResources().
getStringArray(R.array.numbers));
}
}
You got the reference to the spinner, you got your AdapterArray set correctly- but you didn't attach the adapter to your spinner.
Add the line:
numbersSpinner.setAdapter( numbersArray );
You never set your numbers array into the spinner. Try adding the following at the end of the method:
numbersSpinner.setAdapter(numbersArray);
Also check out the hello-spinner tutorial.