I have to create a form in Android which may contain spinner or may not, based on data we have to create spinner. So I am trying to create a dynamic spinner in the form.
But it is not working. I am able to create the spinner but when I click on the spinner it show error message.
Code that I wrote:
dynamicSpinner = new Spinner(getApplicationContext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, option);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
dynamicSpinner.setAdapter(adapter);
option is a ArrayList<String>, which content the list of the options.
If anyone have solution please share with me.
try this
Spinner s=new Spinner(yourActivity.this);
ParentLayout.addView(s);
I dont see you adding your spinner to parent layout.
I am more interested in what error are you getting exactly
Include a spinner in your xml with an id..
and then in code , use:
dynamicSpinner = (Spinner)findViewById(R.id.mydynamicspinner);
instead of
dynamicSpinner = new Spinner(getApplicationContext());
rest all looks fine...
Have a spinner in your xml, then in activity, use following:
dynamicSpinner = (Spinner)findViewbyID(spinner ID);
if(data available)
populate the spinner;
else
dynamicSpinner.setVisibility(View.Gone);
Hope it helps.
EDIT :
Incase you want to add spinner progarammatically, still you would be having a layout in xml which you are using in setContentView(layout id).. so get the layout in which all the childs are present in xml throught its id and then add spinner to that layout.
Have you tried viewflipper?
http://developer.android.com/reference/android/widget/ViewFlipper.html
Its effective especially for those in need in multiple views/widgets.
The thing is just that, the widgets/views that will be viewed/added is defined in xml.
Hope it helps :D
Related
I am creating a feature inside my Android app that will allow users to see their 6 last used apps in a gridview with only the application image. So far I have tried this:
//Load recent used apps
ActivityManager result = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<RecentTaskInfo> apps = (ArrayList<RecentTaskInfo>) result.getRecentTasks(10, ActivityManager.RECENT_WITH_EXCLUDED);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, apps);
recentappsGridView.setAdapter(adapter);
But this only shows a lot of text in each row and column. How can I fix this/make it happen? Please note that I am already using a ListView within the same activity with a method for its click events.
Create a subclass of ArrayAdapter. Override getView(). Set up your cells to be images, not TextView. Use resolveActivityInfo() on the Intent you get from baseIntent in the RecentTaskInfo to get an ActivityInfo object. Use icon on ActivityInfo to populate your cell.
I haven't tried that recipe, but it should get you closer.
I am developing an android project in which I have to load from group of String arrays(say title,description,id) to the listview item TextView.
I did something similar with a database using a cursor like this
String[] from = new String[]{"medicine","healthsystem"};
int[] to = new int[] {R.id.textlist1,R.id.textlist2};
// Now creating an array adapter and set it to display using my row
SimpleCursorAdapter notes =new SimpleCursorAdapter(this,R.layout.notes_row, c, from, to);
I listed all the targets in "from" and all the origin in "to".
Now my problem is I don't have a database so cant use a cursor.
I have 3 arrays of strings which i want to load into textviews(title,description,id) of each item
How to do this
Please kindly help me out
thank you :)
If you don't have a cursor, why are you using a SimpleCursorAdapter?
Read this article about creating a SimpleListView using SimpleAdapter for alternate ideas.
There are lots of examples of populating lists from various data sources in the ApiDemos project that comes with the Android SDK - browse through those, and you should find one that fits what you're trying to do.
First of all you need to create a Class that holds that information, something like:
public class StringHolder{
String titte;
String description;
int id;
}
Then you create the layout of your row that says where you want your title, your description and your image.
Then you create an Adapter. The adapter will hold your data and will say for each position in the list what information should be loaded.
At last you need to use your adapter in an activity.
For more information you can see a tutorial here: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi am working on an android application. And am using a listview in some of my activities.
The problem is all of my listviews displayed are much longer so that the user needs to scroll the whole list to go for the last item.
Am trying to implement a pagination for this, like at first say only 20 items need to displayed on the listview. And at the end of my listview i need a titlebar which have next & previous buttons and on clicking on next button the listview will load the next records from 21st to 40 and so on.
Am using java rest webservice to load the listview.
Can anyone give me a good suggestion for solving my problem.?
Solution 1:
You can load all the data at once if its not TOO MUCH, store it locally & then you can navigate in that locally stored data. Define some variables like StartPoint & EndPoint & get the desired data from that stored data. Increment decrement the values of StartPoint & EndPoint by using the PreviouButton & NextButton.
Solution 2:
Get only the desired data from your data source for example 10 records each time when a Navigation button is clicked.
I suggest than you load list data in a custom Adapter class that extends BaseAdapter class. Like #oriolpons suggested, you should add a footer view, and when you click on button next call some method that is fetching next for example 20 rows, and then add them in your adapter object and call notifyDataSetChanged().
For example
private OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
ArrayList<YourObject> al = getSomeData(int startRow, int endRow);
MyCustomAdapter adapter = new MyCustomAdapter();
for(YourObject a : al)
adapter.add(a);
getListView.setAdapter(adapter);
notifyDataSetChanged();
}
};
Hope this helps.
The easiest solution is to add a footer view to the listview. And on the item click listener you can see if it is the last position (load more items), or not
//add the footer before adding the adapter, else the footer will not load!
View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
#Tijo . Refer this site http://www.androidhive.info/2012/03/android-listview-with-load-more-button/. You can have a button which would call the execute method of Async task and that will load the remaining list for you.
I have a xml layout where is my spinner with id "test"
How can I put my variable "IdSpinner" into R object.
I want to make a common method to create a lot of spinners ,where argument is a spinner id.
listLanguage('test');
public void listLanguage(String test) {
final Spinner spinnerLanguage = (Spinner) findViewById(R.id....);
}
You can't do it in this way (if I understood correctly)- I mean put idSpinner in R.id..., use another approach - just create dynamically Spinner object, in this case you can create as much Spinners as you want.
I have a listview with 2 textviews inside of it that are filled by a database using the simple cursor adapter. I want to add an image inside each item in the list with the corresponding image from the database. I have searched around about this topic but have yet to find a solution. any help would be greatly appreciated
String[] from = new String[]{InventoryDbAdapter.KEY_TITLE, InventoryDbAdapter.KEY_DESCRIPTION};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1,R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter inventory =
new SimpleCursorAdapter(this, R.layout.row, InventoryCursor, from, to);
setListAdapter(inventory
thanks
Really? Because a quick Google search turned up this blog entry which I think is the same one I used for my Android applications.