Button Click event in android tabgroup - android

I have used a tabHost in my android application. The main ActivityGroup class has a listview. When a listview item is clicked, it displays the details in the same tab. I have used a simple activity class for the detail view. the detail view contains a save button which sves the data and returns to the listview(main) screen.
My problem is that for the first time the save button is pressed it works as expected but if the list item is selected again and in the details view if save button is pressed, the application throws error.
Please help. thanks in advance.

The error could be caused by many different things. Check to see if you have implemented your Intent to switch between activities correctly. Like so...
/** Called when the user clicks the save button */
public void save(View view) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
}
If this isn't what you were looking for, can you provide some examples of your code?

Related

First Activity to Second Activity (Intent, Database and Adapter)

I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks

clicking list item in ListView does not open activity in same tab

I have an activity which has four tab. first tab has list view and some list item . after clicking on list item i want to open an activity on the same tab (first tab).
I couldn't find any solution of that.
help me !! Thanks
There is a way to open a new activity, which is with an Intent, so you can do the following:
Intent intent=new Intent(Activity.this or this,Activity2.class);
//here you can add the flags you might need
startActivity(intent);
The activity Activity.this or this is the one that launches the new Activity, so the code must be inside the Activity that is showing the tab you are using.For example, if you need to change the title you can add the following:
#Override
public void onResume()
{
super.onResume();
this.getParent().setTitle("Your title");
}

How to clear the Child activities on clicking the particular tab

I want to know how to clear child activities and how to invoke the parent activity of a particular tab on its second click (tab functionality like on iPhone?). I want to invoke the click listener for each tab. Assume as if the application gets deeper while using, it's hard to use the back button often to reach the parent activity. So i need to invoke the parent activity by clicking the tab.
On first tab it is working fine, here is the code
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals("gebrauchte")) {
Intent intent = new Intent(getApplicationContext(),TabHome.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
tabs.setCurrentTab(0);
}
}
});
I tried to use the same code for the second tab with getChildAt(1), should i give TabHome class for all intent, if I try to give their respective class of 2nd, 3rd and 4th tab, the tabHost gets hidden.
You don't need to set onClickListener on Tabs. I would advice to use Android Tabs with Fragments

onListItemClick in a class that is extending Activity

I am trying to implement item click listener on my list in my main class that extends Activity, because for some reason if i extend ListActivity and expand my list with setListAdapter, the app force closes on startup, but if i extend Activity and expand it with setAdapter it runs fine.
Now my problem is i cannot find a way to implement a click listener for my list, I have tried implementing it in the custom ListAdapter.
The list clicks are going to open another activity which has another list.
------Updated------
#Override
public void run() {
// call any new activity here or do any thing you want here
final Intent intent = new Intent();
if(selectedListItem == 0) {
intent.putExtra("value1", "value1");
intent.setClass(this, com.lister.listexample.ListexampleActivity.class);
}
startActivityForResult(intent, selectedListItem);
}
Change ListView background - strange behaviour
check the above link where i mentioned a list example. that will enable you to trick down your list problems.
Try making a sample project first by copy pasting the above example. then you will be able to understand the answer better
Hope it helps :)

Problem with back button on emulator when multiple activities are there in single tab

I have created two tabs, say TAB1 and TAB2. For TAB1, i have loaded one Activity, say ActivityOne, into TAB1 as
Intent intent = new Intent(this,ActivityOne.class);
TabHost.TabSpec spec = getTabHost().newTabSpec("ActivityOne")
.setIndicator("Activity One",getResources().getDrawable(R.drawable.artists)).setContent(intent);
getTabHost().addTab(spec);
This ActivityOne has extended the ActivityGroup and i added one button in this activity. By clicking on this button it will call another activity, say ActivityOne_One, as
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ActivityOne.this,ActivityOne_One.class);
replaceContentView("ActivityOne_One",intent);
}
public void replaceContentView(String id, Intent intent){
View view = this.getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
When we click on that button ActivityOne_One will be launched under same TAB1. In this application i have two problems:
1) If i want to go back to ActivityOne under same TAB1 by using traditional BACK button on emulator it is not working..
2)ActivityOne_One is launching with no animation(like sliding from right to left) effect.
If anyone know about any one of these, give your advice..
Thanks,
venu
I have found the solution for my question. The following blog gave me a route for this
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
see this link which may help you out with this. http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
i have implemented what is advised in the link i provided. it works OK, but it is quirky and does not always redirect you BACK like you would expect. i have found that you must implement your own custom tabs to really get this desired effect.

Categories

Resources