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.
Related
I have an application with two activities the launcher activity has two swipe tabs and one of those uses the a simpleCursorAdapter and a cursorloader to populate the views from a database.
The code works fine when I am using a single activity and without the tabs. I mean I get updates data as the the database changes, but in this new design I only get updated data only after I restart the app.
Here is the scenario after the application starts I initiate the the loader and populate the listview in the tab , then I can start the second activity by clicking on one of the item in in the listview, the main activity that I can close by pressing on a button.
after closing the second activity I get back to the main activity and insert a row in my database ( and up to this point every thing is working fine , I can see that in the debugger)
But the problem is that I wont get the newly entered item on my screen unless I restart the app.
Even I swipe several times between my tabs nothing happens.
I have tried to restart my loader in my fragment callbacks but still no changes.
What is going wrong in my design?
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.
Good day,
I make my test application in Android and find some difficult moments to do by myself.
I have two Tab Fragments and Host Activity (that creates two tab fragments) with Action Bar (actionbarsherlock). One Fragment allows to add some information about product and persist it into SQLite. Other Fragment has a listview that displays all the products from database.
The problem is that in the first tab I persist data in different thread with 5 second delay, so you click "Add Product to Database" button and after 5 seconds it will be added. But right after clicking I can switch to another tab with listview and the situation is as follows: data from the first fragment is not persisted yet because of delay, but after it is persisted, nothing changes in listview - the listview doesn't know about changes. I need listview updated authomatically when it notices there are changes in database. How can I do this? I use ArrayAdapter to populate data into listview.
I think you need necessary use broadcast receiver, and in it call method refreshDrawableState() for you list. Respectively receiver is your activity.
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.
I have a grid type of layout. When the app first loads, there are 3 buttons. I am using "Adapter" for the layout.
ONCLICK of the button, I want to refresh the same activity but with different set of 9 buttons.
Do I start a new Activity in all? OR Make a temporary activity to start the previous activity (and how)?
Since the ONCLICK event is written in the "Adapter" part of the code, starting new activity on click of the button is difficult. (is out of my knowledge).
if you are using adapter i.e., like baseadapter then you can try:
adapter.notifyDataSetChanged();
directly without starting activity again.
If you want the user to go back to the 3 button view on click of Back button, it will be easier to have the 9 buttons in a different Activity.
Otherwise, you can have the 3 buttons and 9 buttons within two different LinearLayouts in the same activity and hide the second layout using setVisibility(LinearLayout.GONE);
On Click of the button, you can hide the first layout and enable the second one using setVisibility(LinearLayout.VISIBLE);
In the adapter class, we can start an activity using context.startActivity(intent) I did not know that we can access "start Activity" from adapter...
but now it's working just fine!!
Thanks a lot for your recommendation...