android


custom spinner with last item as a link


I want a Spinner which contains last Item as "Add more items"
and when i click on it, then i can add next item. The item i have added should get displayed in spinner list and will have same last item as "Add more items"..
I tried using Adapter but how can i keep last element as "Add more items"
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == spinner.getItemIdAtPosition(spinner.getCount()))
// my code for adding item to list using Adapter
else
// spinner.setSelection();
}
did i wrote anything wrong??
any help??..Thanks ...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="2dp"
android:dropDownWidth="500sp"
android:spinnerMode="dropdown" />
</RelativeLayout>
MainActivity.java :
public class MainActivity extends Activity {
private Spinner spinner;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("item " + i);
}
data.add("Add New Item");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
tv.setText(spinner.getSelectedItem().toString());
if (spinner.getSelectedItem().toString().equals("Add New Item")) {
data.remove(position);
data.add(position, "item " + position);
data.add((position + 1), "Add New Item");
updateAdapter(data);
Toast.makeText(getApplicationContext(), "item " + position,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void updateAdapter(ArrayList<String> data) {
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
data);
adapter.notifyDataSetChanged();
}
}

Related Links

Unity facebook API FB.API() java.io.FileNotFoundException
Android studio assets folder
outofmemory error with setimageresource for ImageView
Make an unsecure WiFi network available
asmack : Receiving custom IQ messages in PacketListener
Fiksu SDK Integration
How can improve the android frame rate?
How Does WhatsApp overcome the GCM Push notifications delay?
Marmelade Juice: error: SDK “macosx10.10” cannot be located
Exporting project from Blender to use in Android project
How to change the Radius of an Already created Geofence without getting same IDs multiple times
Android Viewholder implementation
I want to read the pdf files from assets or raw folder in android
Status bar is hidden under fullscreen but it shows up when menu key pressed in Android
android sql getContact by name first character
How to reuse selector for color-different buttons in android?

Categories

HOME
developer-tools
polymer
webdriver
gpu
fftw
saml-2.0
solver
r-googlesheets
cublas
rom
cakephp-3.4
jetty
spring-session
32bit-64bit
octave
heap
mautic
line
filechannel
cumulocity
symbol
bitnami
azure-powershell
keytool
google-picker
foreign-keys
symbols
scriptella
flask-sqlalchemy
direct3d11
fable-f#
oculus
sqsh
left-join
hreflang
android-gridview
activexobject
reactjs.net
slacktextviewcontroller
raft
openproject
metawidget
doc
datacachefactory
branching-and-merging
facebook-social-plugins
mesosphere
sonarlint-eclipse
dotnet-httpclient
twilio-click-to-call
x-ray
arq
edit
offline.js
bootstrap-tags-input
filenet
grails-plugin
mapxtreme
stax
contentcontrol
jnlua
atan2
toran-proxy
assertion
dllimport
forwarding
text-search
dock
doctype
batik
snackbar
jquery-forms-plugin
cosine-similarity
packet-sniffers
browserify-shim
openargs
mysql-error-1044
pagedown
virtual-pc
wcf-configuration
jsr286
gobject-introspection
ikimagebrowserview
promotion-code
landscape-portrait
massive
cuteeditor
codebase
socketserver
cac
metaweblog
hbm2java
wysiwym
localizable.strings
fdf
wcf-proxy
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App