Android:listview store value of button - android

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();

Related

click on listview, android studio

I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.

Android Gridview is not refreshed while pressing back button

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.

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.

Refreshing the Listview

Hi i have an app that contains 2 screens . The first screen(main screen) contains a ListView that is filling the rows dynamically by communicating a server. On clicking the row element a new screen is appears, there is a button on this screen, on pressing this button a communication with the server is made(for this i have used AsyncTask class). After the process is completed , i have to switch back to the first screen(main screen) automatically, and the previous row element whose process of communication with the server is just been finished has to be eliminated from the ListView.
I am facing the problem regarding eliminating the row element of ListView.
Please help!
use StartActivityForResult to open second activity and use Custom adepter in list view and update the data of the adapter with new one and notify change to list.
http://support.xamarin.com/customer/portal/articles/531998-android-startactivityforresult-and-finish-with-video-
I recommend you to start the second activity (when you click in your listview item) with startActivityForResult. So the second activity can perform the remove from the server and notify back to the main activity when the task is done. In your case, the main activity will receive the result from the second activity and will remove the item from the list view.
Hope that help!
For SimpleAdapter use the notifyDataSetChanged() function.

Changing the content of a ListView (Android)

I have added a menu to ListView Activity and wanted to allow the user to select the information that would be displayed in the ListView. So for example:
The ListView is populated in the onCreate method.
The user selects an option in the menu.
Upon the user making this selection the ListView would be populated with a different array.
Is onResume() executed after the menu is closed? I would test this but I am not currently home. What is the best way to do this?
Thanks,
Rob
No, OnResume is not called when the menu is closed. What you want to do is put the code that swaps out the information in the adapter in the OnMenuItemSelected method.

Categories

Resources