getView() not being called on notifyDataSetChanged() - java

Here's my problem: I have an app that has an activity with a listview inside it. This activity is recursive, when you click on an item on the list it takes you to the same activity with different items in the listview. A lot of the code is within the ArrayAdaptor for the listview.
When moving forward through the activities the new ones call getView() when creating the listview rows. But when stepping back (pressing back closing the current instance of the activity and moving back to the previous one) these already existent activities don't call getView() so my rows don't get updated.
So I need , onResume, to call getView for the rows. I've tried adding notifyDataSetChanged(),requestLayout() and invalidate() on onResume, doesn't work. getView just doesn't get called.
I've searched for answers to my problem, did not find any. I'm sure there are a million ways to improve my app if I re-write it from the ground up, that's not gonna happen any time soon. I just have a few days to finish this, I'm quite desperate. Please help.

Related

onCreateView() is not showing any data am taking from Database after calling it second time

I already searched every topic related to this but none of them helped me...
I am a beginner and I would really appreciate an answer...
When I launch the app it's all good but when I call HomeFragment for the second time problem is appearing.
I tried
FragmentManager.executePendingTransactions() to set right after
fragmentTransaction.commit() but it didn't help...
here is where I call a fragment from a menu
and here is my onCreateView(); method in HomeFragment class
your code looks fine. but the problem is that the volley request is executing Asynchronously in another thread. so your adapter is created with empty bookTitle.
so you need to set your adapter to the listView inside onResponse() after your books are ready.
you can also call adapter.notifyDataSetChanged() inside onResponse to notify the adapter that your book has changed (no longer empty).

How to allow click item during notifyDataSetChanged() in adapter

I use adapter in RecyclerView. Each item contains a ProgressBar. Since progress speed is sometimes very high, a fragment permanently call notifyDataSetChanged(), which initiates update adapter. But there is one unpleasant feature: at this time ViewHolder doesn't respond to a click and user can't open the item, while progress speed will not decrease. Is it possible to solve this problem?
notifyDataSetChanged actually redraws the list for you. So click on the list item at that instant may actually refer to the content which was available before you refreshed the adapter contents. Hence its a good thing that click isn't enabled. Having said that, the refresh usually happens so quick that the user won't see a visible lag. Hence please go through you getView() method in the adapter if there is an intense or time taking process in the adapter implementation. Hope this helps! :)

Changing activities with Intent on listview button

In the application I am building I have 2 listviews with, among other elements, 2 buttons on every row, one for eliminating that row and the other to pass to another activity using an Intent.
I am detecting clicks on both buttons by setting click listeners on both buttons on the getView method of the Adapter class.
The first button was something I couldn't figure out, because I needed to identify in which listview the button was clicked and the position of the clicked row which I couldn't realize how to do.
The second one I thought would be easier since all I needed to do was Intents.
However I needed to call a method from the activity class (had to instance it) since I couldn't make Intents in a non-activity class.
This last one threw a NullPointerException.
Will post all code & logcat in a while, be right back, any possible help will be appreciated.
I will need to see your code, but basically what worked for me for being able to call intents within the code of the adapter was to use the host activity.
Something like this:
item_new = (TextView) convertView.findViewById(R.id.my_view);
Activity host = (Activity) item_new.getContext();
Intent intent = new Intent(host, MyActivity.class);
host.startActivity(intent);
If you post the code maybe I can give more details
I'm not figure out the entire problem, but if you want to start an Activity from an Adapter you could simply pass the current activity to the adapter constructor (or cast the getContext() to Activity, if you used that as context of your adapter)
In a line of code:
Activity.class.cast(getContext()).startActivity(intent);

Updating ListAdapter from different activity

I am working on my hobby project to learn more about android programming and stuffs. So i recently ran into a simple problem but couldn't find a solution for it.
My project for now have 2 activity [MainActivity], [CommentActivity].
In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. Everything works fine there. It will fetch all the comments and total comments for a certain post based on the post id. In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview.
When a user posted a comment on CommentActivity, it will save into the database via AsyncTask.
Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). So it will never reload the MainActivity again when back button is pressed. Also it will never update the total comment in the MainActivity. What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database.
Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity.
I believe there must be some easy way to do this. I can post codes if you guys need it to assists me.
Thank you.
Yes, there is an easy way.
Start your sencond activity using:
startActivityForResult(intent, RESULT_CODE)
use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database
override onActivityResult() in your first activity to check when the second activity returns properly
update the first adapter as needed.
Hope it helps.
In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity.
If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data

Update ListView or Activity?

I have a listView inside Activity inside tab layout. If I long press the item, I get the option to rename the item. Once the item is renamed, the change can't be seen until the activity is restarted.
I tried solving the problem by simply creating a new intent and re-opening activity, but since that activity is inside tablayout it doesn't work. I also tried re-opening tabLayout activity but then it automatically goes to tab 1, while I'm trying to refresh listView inside tab 2.
So then I tried solving it by creating updateListView() method:
public void updateListView(){
listAdapter.clear();
listAdapter.addAll(recordedFilesArray);
listAdapter.notifyDataSetChanged();
}
But that doesn't work either. When I'm using this method, it completely clears listView and then I have to restart the activity again to see the results.
So, anyone have any idea what I could do to see listView changes without restarting the activity? By the way, if it helps, I'm reading ListView (ArrayList) from a text file.
Try using notifyDataSetChanged() only without clearing your listAdapter
You can make a separate adapter class and implement its getView() function to manage this. Your activity will pass the array list to the adapter. The adapter's getView() will take care of displaying the contents from the list.
When the list gets modified, you need to reinitialize the adapter. So that way getView() always have the updated data to display on the screen.
For renaming the item, you can change your item object in the ArrayList,
and then call adapter.notifyDataSetChanged().
I found the problem. The ArrayList wasn't changed until the activity was restarted. I had to call arrayList.set() to change the element in the ArrayList. Now it works as it should.
My code:
recordedFilesArray.set(toDelete, input.getText().toString()); //toDelete is arg2 variable (onLongItemClickListener)
listAdapter.notifyDataSetChanged();

Categories

Resources