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

Adding images to custom Listview
InflateException Error inflating class EditText
Hybrid application development (PhoneGap, Cordova, Ionic) under the hood
How to load newest website through android webview
React Native build doesn't embed bundle file
What's the current practice for TDD in regards to Xamarin.Android's Activities?
Android Facebook Login onActivityResult
Getting GCM Registration ID using Firebase
Download image in GcmListenerService and post notification
Different resources for same language but different screen resolution
(Android) app:actionViewClass doesn't seem to show
react on android Studio where is device manager
Android: Modifying kernel parameter during runtime
Text view is null
React-native fetch method response json contains another json
Safetynet API helper error

Categories

HOME
paraview
formal-languages
jax-rs
pascal
fftw
sitecore
apiblueprint
permissions
vsm
angularfire2
lanczos
elasticsearch-5
openpyxl
ibeacon-android
fhir
gkturnbasedmatch
internationalization
red5
solrcloud
database-connection
windows-xp
lilypond
tiki-wiki
bulletphysics
nstimer
inversion-of-control
exchangewebservices
iis-express
ntp
indy10
processwire
imx6
sharepoint-workflow
salesforce-communities
cin
setup.py
mcustomscrollbar
datacachefactory
sharp-snmp
install.packages
ovf
activeperl
singleinstance
colorbar
sundials
bbpress
d3-force-directed
structuremap4
centrifuge
google-cloud-console
windows-azure-queues
lean
freepbx
bizagi
piracy-prevention
binomial-theorem
toolkit
ember-cli-rails
haskell-warp
airbrake
crowd
contrast
wcm
gii
angular-carousel
paste
radiant
decoupling
websphere-esb
menubar
gae-quotas
light
ebay-lms
mvc-editor-templates
mantle
robotlegs
extreme-programming
floating-point-exceptions
box2d-iphone
luajava
instantiationexception
folding
duplex
software-lifecycle
wysiwym

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