I have a Activity that's creating a Check list, if I close the activity with back Button the list is saved in a Database.
The list from the Database is used as the MainActivity content.
This is how my app will look:
If I press the List Item button, a new element should be added to the list. How can I add and sisplay a new Item (Layout with Checkbox and EditText)?
Do I need an Adapter? I don't want the 'list item' part repeated.
That looks so after i press 4 times
Activity
private ArrayAdapter mAdapter;
onCreate
ListView lv = (ListView) findViewById(R.id.my_list);
List<String> initialList = new ArrayList<String>(); //load these
mAdapter = new ArrayAdapter(this, android.R.id.text1, initialList)
lv.setadapter(mAdapter);
When the event happens
mAdapter.add(newString);
The add method in ArrayAdpater will automatically take care of the notifyDataSetChanged() call for you and update the display.
TextWatcher you might not need this part, it's if you want the text added as soon as the text is changed which your question seems to indicate - that is risky because you might get partial entries, so i recommend a done button instead, in which case you just do the afterTextChanged stuff in the onClick method.
EditText editText = (EditText) findViewById(R.id.my_edit_text);
editText .addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
//TODO: check it isnt emtpy
String text = arg0.getText().toString();
mAdapter.add(text);
//Hide the add item views again
arg0.setText("");
arg0.setVisibility(View.GONE);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) { /*nothing*/ }
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) { /*nothing*/ }
});
You can add plus button in your list as footerview as your image then new item add in your arraylist by the action(onClicklistener) of footer view button then call youradapter.notifyDataSetChanged().
Related
I have a spinner and I want to get the last selected value of it when I click on the spinner (basically just before the items in the list are being shown).
How can I achieve this?
Update:
Example: There are a dynamic amount of spinners. User clicks in spinner 1 and selects a value, let's say "house". Then clicks somewhere else. Then clicks in spinner 1 again. What I now need is to return the value "house" that was previously was selected before the user selects a different value i.e. "car" in that spinner. I can not use the local storage to save that value beforehand because it's going to be a dynamic amount of spinners to be added.
And yes, I did in fact read the documentation at http://developer.android.com/guide/topics/ui/controls/spinner.html already but what I need isn't explained there.
Maybe you can declare a String array and then use it to storage the items values. Showing this in a TextView is a way to see if it works.
Something like this:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selection.setText(items[arg2]);
}
I already have faced a similar situation, but I don't remember exactly how I solved it. Tell me if it gives a better idea to you, I'm new participating here.
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
When the user selects an item from the drop-down, the Spinner object
receives an on-item-selected event.
To define the selection event handler for a spinner, implement the
AdapterView.OnItemSelectedListener interface and the corresponding
onItemSelected() callback method. For example, here's an
implementation of the interface in an Activity:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Then you need to specify the interface implementation by calling
setOnItemSelectedListener():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
IN YOUR CASE:
TO se if the user clicked on the spinner, do this:
if (spin.getSelectedItemPosition() < 0) {
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
}
This means user clicked the spinner, but not any items of it.
EDIT AGAIN
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
> ...
>
> public void onItemSelected(AdapterView<?> parent, View view,
> int pos, long id) {
> parent.getItemAtPosition(pos) //THIS GETS YOU THAT VALUE THAT THE USER CLICKED ON DYNAMICALLY
> }
>
> public void onNothingSelected(AdapterView<?> parent) {
> // Another interface callback
> }
> }
http://developer.android.com/guide/topics/ui/controls/spinner.html
Solution is to add a TextView widget with each added Spinner. Set the TextView to "gone" (not visible). When the spinner changes set the value to that TextView. This way you can store the last selected spinner value in the TextView. When the user clicks on the spinner and changes the value you can still access the previously selected/unselected value from the spinner through the TextView widget.
// Use this TextView to temporarily store the spinner value
final TextView hiddenTextView = new TextView(getContext());
hiddenTextView.setVisibility(View.GONE);
final Spinner spinner = new Spinner(getContext());
spinner.setPrompt(getString(R.string.email_other));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.email_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d(TAG, "TextView Value before spinner change: " + hiddenTextView.getText());
String spinnerValue = String.valueOf(spinner.getSelectedItem());
hiddenTextView.setText(spinnerValue);
Log.d(TAG, "TextView Value after spinner change: " + hiddenTextView.getText());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
In my application there are lot's of views(spinner, buttons, editText, textView, ImageButton etc).
After selecting one value in the spinner it not reflect the value which I selected. The old value is till there on the spinner. But when I tap on other views like editTest then spinner value automatically updated.
I think no need to mention the code because normal spinner is there which populating data from ArrayAdapter and I am doing small task after selecting value.
CODE:-
spinnerWard = (Spinner) findViewById(R.id.spinnerWard);
aAdapterWard = new ArrayAdapter<WardList>(this,
android.R.layout.simple_spinner_item, listWard);
aAdapterWard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWard.setAdapter(aAdapterWard);
spinnerWard.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
intWardPosition = pos;
intFinalWardId = listWard.get(pos).getId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
I have two spinner in dialog. Second one is dependent on first one. I want to bind it and then select it in edit mode. All works fine but second spinner does not get selected. However it get selected when I open my dialog next time.
Here is a part of my code.
ArrayAdapter<String> myAdap1 = (ArrayAdapter<String>) spnForeignKeyTable
.getAdapter();
int spinnerPosition1 = myAdap1.getPosition(objcolumn_schema
.getForeignKeyTable());
spnForeignKeyTable.setSelection(spinnerPosition1);
// Bind Column Spinner.Second spinner
dblist = DBAdapter.getColumns(pf.getString("dbid", ""),String.valueOf(objcolumn_schema.getForeignKeyTableID()));
ArrayAdapter<String> adpf = new ArrayAdapter<String>(
column.this, android.R.layout.simple_spinner_item,
dblist);
adpf.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnForeignKeyColumn.setAdapter(adpf);
int spinnerPosition2 = adpf.getPosition(objcolumn_schema.getForeignKey());
spnForeignKeyColumn.setSelection(spinnerPosition2);
for changing selected item in second spinner when first spinner selection change you will need to set setOnItemSelectedListener for first spinner as:
spinnerPosition1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v,
int position, long id)
{
// change second Spinner selection here
}
public void onNothingSelected(AdapterView<?> arg0)
{
//
}
});
I do have a list of possibly several thousands items (just testing with shorter list with about 200 items). The information is stored in SQLite, the ContentProvider, loader and SimpleCursorAdapter is used. The list is sorted lexicographically, and the android:fastScrollEnabled is used. The list scrolls smoothly -- no problem when one knows the exact name of the item.
Occasionally, I would like to find the items that contain some substring somewhere in the middle of their name. The `... LIKE "%wanted%" is a solution for me. However, I would like to give the user an incremental filtering -- i.e. the list content be updated during typing the substring. The reasoning is that it may not be neccessary to type many characters, and one should find the item as soon as possible. The goal is not to find one or none item. The goal is to filter the list so that manual scrolling be acceptable to overview the candidate items and select one of them by touch.
I have met the SearchView widget that looks nicely at the action bar. Anyway, reading something more about it in the doc, I am not sure if it is the right tool for me. Or if the recommended implementation is the one for me. (I am an android beginner, I am even not sure if I understand it well.)
Is it possible to use the widget for incremental filtering of the list in the same activity that has the SearchView in the Action Bar? Can you point me to some code that possibly shows how to implement the wanted behaviour?
Sample code to try out :
public class AndroidListViewFilterActivity extends Activity {
ArrayAdapter<String> dataAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//Array list of countries
List<String> countryList = new ArrayList<String>();
countryList.add("Aruba");
countryList.add("Anguilla");
countryList.add("Netherlands Antilles");
countryList.add("Antigua and Barbuda");
countryList.add("Bahamas");
countryList.add("Belize");
countryList.add("Bermuda");
countryList.add("Barbados");
countryList.add("Canada");
countryList.add("Costa Rica");
countryList.add("Cuba");
countryList.add("Cayman Islands");
countryList.add("Dominica");
countryList.add("Dominican Republic");
countryList.add("Guadeloupe");
countryList.add("Grenada");
//create an ArrayAdaptar from the String Array
dataAdapter = new ArrayAdapter<String>(this,R.layout.country_list, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
}
}
I am having an issue with my function that searches my list. The list array used to be attached to a String[] in the actual .java files but I moved the array into the strings xml file to implement another feature of using different languages in the app. Therefore translating the listview
Anyway, I understand why the code is doing the following but I can't work out how to solve it...
Resources res = getResources();
final String[] items = res.getStringArray(R.array.societies_array);
listView2 = (ListView) findViewById(R.id.societieslist);
EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
System.out.println(cs);
SocietiesScreen.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
The onclick listener of thelist is depend on the index of the list so when not searched it works fine but when I search for a particular option the new 'first option' now gets assigned a new index number instead of keeping it's original
Hope that makes sense and I have given enough code, if you need anything else just let me know
Thanks
I think I've already answered a very similar question, here. You should also watch "the world of listView" lecture.
Basically, if you use a custom BaseAdapter, you can create a new mode for filtered items, which is enabled as long as there is something in the search field.
When this mode is enabled, items are retreived from a filtered list instead of from the original list.
If you don't use your own BaseAdapter, check out this link.