How can I getIntent from ViewHolder in RecyclerView ? - android

I have to get data from another Activity and set my text to those data in my onBindViewHolder, how can I do that, any ideas ?

When creating the adapter, send your Activity as context.
Then
adapter(Activity context);
..
context.getIntent();
..

I found this useful code of RecyclerView. Might it will help others. It is complete working example.
http://inducesmile.com/android/android-staggeredgridlayoutmanager-example-tutorial/

Related

Not sure how to deal with ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”

Sorry for a brick of a code but I read that I need to run something on UI thread, how do I do that? private ArrayList JsonFIveDays(String weatherSearchResults) is where I set my listview adapter if that helps.
You have to notify the adapter whenever you do changes in the weatherArrayList.
For Example,
weatherAdapter.notifyDataSetChanged()
But in your case to use like that you have to declare the weatherAdapter as a property of the class.
I hope it will help you.
You need to notify adapter that you changed data with notifyDataSetChanged(). You can do that in setData method after changing data.

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?

Where to load the data for an Adapter ? In Activity ? or in Adapter?

For a List view you must have an adapter. To fill the list items you need data. Now here is the question:
is this a good idea to download the data in custom adapter ?
or better is download data in activity & pass it to adapter ?
or their is any better way to achieve that ?
looks it is only one question with three option
is this a good idea to download the data in custom adapter or better is download data in activity & pass it to adapter their is any better way to achieve that ?
Create AsyncTask from Activity and set the data to adapter from onPostExecution callback function
a good tutorial link1
2- Load ListView in background AsyncTask
You can download in any activity class, then send the data through the Broadcast, edit your adapter and finally update the adapter i.e list by calling :
adapter.notifyDataSetChanged();
I got a better answer for this question:
prepare your data in Activity, then send it in your Adapter. It may be downloading the data via AsyncTask or from SQLite or something else.
In your Adapter load the data via AsyncTask or from SQLite or something else.
But the better option is loading the data in Activity & use it in Adapter. It will help you to make your codes cleaner & it will give you more flexibility, to manage your data. But in some situations, you may consider to prepare the data in Adapter; if you have difficulties to load it in Activity.
Both works fine, Its up to you which will you consider.

android listview refresh

hello every one
i want to refresh my listview after adding or removing any data.. the adapter is from the SDcard how can i do this?
thanks in advance
call this code after your data are
change..
adapter.notifyDataSetChanged();
Use notifyDataSetChanged() of ArrayAdapter.
See the following code:
((EfficientAdapter)listView1.getAdapter()).notifyDataSetChanged();
Hope this will help you.

Categories

Resources