Android Gridview is not refreshed while pressing back button - android

I have one grid view in one activity.I have set Adapter using array-list.
Gridview is displaying the list of the items stored in the database.Let say it is some products.
When i click on any product in the grid-view it will navigate me to product detail screen.
From product detail screen i can delete product.If i delete product and press back button it will navigate me to product list screen.
Expected: Product list screen will come without deleted item.
Actual: Product list screen is coming with deleted item. When touch this item it will do nothing.
Supporting my question,please see below picture

You have get gridview items from db in OnResume method of activity. And when u delete any item and then back press, it will call OnResume method of previous activity where u get new list, and call notifyDataSetChanged() on the grid, in order to recreate the view

Try Some like:
if (homeGridViewDatas.size() > 0) {
homeGridViewAdapter = new HomeGridViewAdapter(activity, context,
homeGridViewDatas);
gvHome.setAdapter(homeGridViewAdapter);
homeGridViewAdapter.notifyDataSetChanged();
}

You should notify the gridadapter which something is changed by callingnotifyDataSetChanged on it. In onResumeshould be ok. Or when you update it too.

When i click on any product in the grid-view it will navigate me to
product detail screen.
When you go from gridView activity (product list screen) to detail activity(product detail screen.), the detail activity is launched on top of your gridView activity in Stack.
If i delete product and press back button it will navigate me to
product list screen.
And when you delete a particular product in detail screen and press back button, the detail activity is removed from the stack and hence the gridview activity which was below detail activity comes to foreground and becomes visible.
Product list screen is coming with deleted item. When touch this item
it will do nothing.
However your gridView activity still has the previous data.Hence it displays that old data and nothing happens on touch as actual data is different than the one in adapter.
Solution :
You need to notify the adapter for gridView in that activity, that the data has been changed and the view must be updated accordingly.
As onResume() is called everytime activity comes to foreground and is visible, it is the better place to call notifyDataSetChanged() on your gridadapter.

Related

How can I make a prev and next button to show next and prev element of a recycler view

I have implemented a recyclerview. On clicking a item it would display the related field of that item (eg. name, age).
Now i want to make two buttons prev and next to show the details of previous and next item without going back to the list (in same activity). How can i do that?
From my understanding you want to implement ViewPager. Check ViewPager
Here is the tutorial.

Shared Element list changes

Activity A contains a list of images. Activity B contains the selected image. Using shared elements it successfully animates the image being moved to the new activity and back again when I finish B.
The problem is when the list is changed and I am on B, the image will try to go back to the same spot in the list. If it was the 10th item in the list when I selected it, then when I press back there are only 3 images in the list now it will throw an exception as that 10th list item container won't exist anymore. If I select the 1st list image, but whilst on B the list grows and my item is forced to the 5th spot in the list, when I press back it will still go back to that 1st list item it came from.
Is there a way I can tell it to move to a different list item on its return transition? (Activity B is informed when the list changes, so I do have the option to get it's new position)
Any suggestions would be great guys.
Thanks
It seems you should only refresh your list when your ActivityA is ready.
Maybe you can use startActivityForResult() in ActivityA to start ActivityB, though I'm unsure about the exactly life-cycle callbacks of ActivityA when ActivityB returns.
Maybe you should look into SharedElementCallback and refresh your list when the transition has completed.

Android:listview store value of button

I have a like button in my list View Item, Whenever i click on like button it saves the values in mysql.
(cus_id, offer_id, status)
But if i go to some other activity and return back to list view activity the like button turns to be in default color.
I want to save activity done on like button, means if the like button is clicked once it should remain clicked. even after switching b\w any activity.
how can i resolve it...
You should set adapter to your list in onResume activity call back rather than onCreate Activity call back. Making this change should solve the problem.
I think you set your adapter for listview in onCreate,
You set your adapter in oncreate
Make changes to your listview item (Clicking Like Button)
Move to next activity.
Press Back and come to your list activity, your change not reflecting.
Because when you press back onCreate will not be gets called so your listview adapter refreshes its view with POJO that you have given to the adapter.
Solution
1) Set adapter in OnResume
2) I Suggest this solution, after updating SQL database, update your adapter POJO and call
adapter.notifyDataSetChanged();

How do I refresh connected fragments accordingly?

I'm making a simple notepad app. In one fragment there's a list view of added notes and when some note is selected the other fragment shows the details of that note. Now if user clicks delete button a note is deleted, but how do I refresh the list view and detail view accordingly?
For refreshing the ListView actually what you have to refresh is the underlaying Adapter.
This would be the workflow of your scenario:
Remove the selected item
Call notifyDataSetChanged()
Set another item as the current one, for example the previous note or the first one.
And that's it.

Android ListView - Unable to go back to ListView after calling onItemClickListener

This question is actually a 3 Part Question. Please suggest the valid solution for my problem.
Application Overview
I'm creating an application having a splash screen and a ListView. On click of the launcher icon, first the splash screen is shown and then after 3-4 seconds later, the splash screen calls the ListView activity and user is left with ListView items on the phone screen. On click of each item, respective textual content will be visible to the user. After reading the content user will click the "Back" button and will be returned to hte ListView items section again. If he/she wants to read any other item content, will be taken to that page on the click of that item. If not willing, then will click "Back" button on phone and application will close.
Part 1 - What I actually want to do
I want to show different textual content (not normal text but styled (bold/italized/containing images and all) on the click of different listView items. I can create the html pages and store them in my resource folder locally but don't know how to call those pages while calling onItemClickListener. I'm not sure if this is a valid approach to do this sort of work or not. Please advice. It's not working so far
Part 2 - What I'm doing instead now
Since I was not able to call the textual content so I wrote the content on some text editor, did all the fancy work on the editor and when the content was ready, I took the screenshot and then call that image instead while calling onItemClickListener. It's working fine and I'm able to see the image on click of my ListView item.
Part 3 - The problem I'm facing
When the image is shown to the user, on click of "Back" button, the application is closed instead of going back to the ListView Section. Apparently the image opening on click of the listview item is in the same activity as is the ListView itself. What I want is that user should be able to go back to the ListView section after he/she finishes reading the textual content.
Below is the code of the ListView Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.menulist);
String[] values = new String[] { "1. First Article Here", "2. Second Article Here", "3. Third Article Here"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_listview, R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id){
//
if(0==position)
{
setContentView(R.layout.image0);
}
else if (1==position)
{
}
}
});
}
*The code works fine upto setContentView(R.layout.image0);
* But after this I want the application to return back to the ListView section on click of "Back" button but it closes instead. Please guide.
May be it solves by,
Using layout.image0 in another activity and call by intent when click on list item.
By use this, When you click back button you view your previous activity
When you press the back button, the current activity get closed and you get back to the previous activity on the stack. Given that you are not changing activity by pressing the list element, but only changing the layout, pressing the back button empties the stack and closes your app.
The cleanest way would be to launch the splash screen in another activity.
A couple of other ways to solve it are:
- add a stack of fragments
or (the dirtiest way), override the onBackPressed(), intercept it when your app is showing the splash screen and change back the layout to the listview.
I would prefer the other approaches.
If you use setContentView() method, the back button functionality will not work, since no new Activity was created, from which you can go back to the listView. I'd rather take the clicked item's useful information, put it in an Intent (link), and call startActivity() method, to start a new activity, to show content to the user. Then, if the user clicks the back button, the "details" activity will disappear, and the user again sees the listView.
You are using setContentView so its overriding the current view with ur new View in OnItem Click...Rather add a View Group to ur activity like a Relative layout...And when u click your Itme add a new layout to ur existing layout and on back remove the layout.
Or u can Use DialogView to dynamically add a view ..see below:
Dialog dialog = new Dialog(this, android.R.style.Theme_Dialog);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xff000000);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(Your VIew);
dialog.show();
Or at last you can start Another Intent using your current Activity..
Part 1 and 2
You can create complex and styled layouts using the XML files.
If you really want to use HTML, I advice you to use a WebView. You can store the plain html-string somewhere in your resources and use Webview.loadDataWithBaseUrl() to show it in your WebView.
You can make your choice depending on your functionality. If you have alot of different events to be handled (like clicking on a View), I would opt for the XML files. Because you can assign different listeners to seperate Views. When your functionality doesn't go further as it does now, only clicking a whole view. You can use a WebView as well.
Part 3
The problem is that you are abusing the setContentView-method. setContentView tells Activity which XML file it has to use. In your case, when the Activity is created, it will use R.layout.activity_main.
When you click your item, you tell the Activity.. Don't use the R.layout.activity_main anymore, but use R.layout.image0. The Activity will do this and your image will be shown.
This is not a good way to change layout though and will mess up your working flow. The reason that, when the image is shown, your application closes on Back is that there is only 1 Activity started. (Your stack contains only 1 Activity) You just switched the layout, but no new Activity is started.
Solution
Create 2 Activities:
Create a main Activity showing the your list and set the layout with setContentView(R.layout.activity_main). When your click-event is triggered, you will start the second Activity. Intent i = new Intent(...). You can also add some arguments to the Intent and access them in your second Activity to know which item was clicked and which image to show.
A detail Activity showing your content or in your case an Image. setContentView(R.layout.image0).
Solution 2
Create a popup to show your content. You can use a Dialog for that.

Categories

Resources