how to add firstvalue by default null in spinner.? - android

I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.

You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.

Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..

A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.

Related

How to evaluate a variable (changing value) in a Fragment from a RecyclerView Adapter (Kotlin) for conditional processing?

I'm new to all of this and was making good progress but I have hit a brick wall with working out how to do the next thing. I am using Kotlin and have a Fragment with an associated Recyclerview Adapter. I would like to set OnClick (On or Off) against items in a row depending upon a value in the Fragment which could change at any time.
My adapter works fine to show and update the array of data and also to implement OnClick.
I have tried sending a data element via a constructor which changed in the fragment but always showed as the initial setting in the adapter. The same with trying to call a method.
Many other questions touch on the issue but only show snippets of code, and it seems that I'm not advanced enough to get them working successfully in my code.
Could anyone please provide a pointer to a working set of Kotlin code that includes parsing a variable from fragment to adapter - perhaps in Git or a tutorial. I'm sure that if I can study a working program I can move forward. Thank you.
It would have been better if you had included your Adapter and Fragment code in the question, that would have helped us in understanding how you have setup everything and what data model are you passing to adapter.
But looking at your question, one solution that comes to my mind is to add an enabled boolean in your data model that is displayed in the ViewHolder. Using this you can set view.clickable = model.enabled. Now whenever your "value in the Fragment" changes you can update this list and let the adapter rebind items.
Note that the above solution is when you want to selectively enable/disable clicks on individual items. If you want to do this for all items at once, it's better to create a variable in adapter that you can change from the Fragment, and inside the clickListener you can check the value of that adapter variable. If it's false, just return out of the click listener. Something like,
view.setOnClickListener {
if(adapterValue) {
// handle Click
}
}
If this approach doesn't help, I would ask you to add more context in your question and show what you have done so far.

How to fetch data from firebase to recyclerview

I have an app, where I try to implement a function that would allow to create posts like in facebook. So, I want to create recyclerview that would fetch a data and display it on the screen when the post is created.
How do I do this?
I've written a code for this. But I get an error that in EventRecyclerAdapter.java in 40th line of code .get(position) is called on null reference object.
https://github.com/garaanon/IACS Here you can see the .java file I am referencing
Your Else Condition will trigger when the Data is Null than why are you trying to get Description When it is Null.Remove the getDesc() from Else Condition will Remove Error.

How to modify a ListView item in Android?

I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)

how to get the Default Listview Value in android?

In my application i have customListView ,i need to get the first value of list view(i.e. Zero position value) when very first time activity is loaded without using OnItemClickLiseners and it is focusable.How Can i do?,please can any one help me.
Thanking in Advance.
You can either use
listView.getChildAt(0)
or
listView.getChildAt(listView.getFirstVisiblePosition());
This is based on the assumption that you need to pull out values of the item that is at the first (0th) position in the ListView.
Since you are using a Custom ListView, you should also have a custom Adapter (possibly an ArrayAdapter or a BaseAdapter) that you use to show the data etc.
In any case, just check for this in the getView():
if (position == 0) {
// GET WHAT EVER DATA YOU NEED OUT OF THE ITEM.
}
If you can add more info to the OP about how you deal with the data, perhaps this answer can be made a little bit more useful. For example, do you use a POJO class to bind the data to the ListView?

ListView / ListAdapter find back the object out of the list

I'm wondering a simple way to find back the object corresponding to the clicked item..
They're tons of examples on the web on how to figure out the ListView setup with the setListAdpater, but much less on how to well handle its listener.
Is "by position" the only way ?? I'm wondering a possibility to associate the objects itselves to the adapter, to not have to use their position in list (or even the displayed String!) to find back the Object referred by the clicked label..
Position is what is used always. The ListView works with using the position.
But if you want to access the ListAdapter and get a value out by providing a string, than you will have to extend ListView and implement that functionality yourself. You can overwrite the different methods that handle adding and removing and keep a HashMap where you keep the string representing the object. Then through a getObject(String key) you return the object that is in the hashmap for that key.

Categories

Resources