TabHost: Execute methods in specific tab (OnTabChange) - java

I have a tabhost with two tabs. Each tab has his own activity. My goal is to execute a method in one of the tabs if the tab was changed. I have set up this OnTabChangedListener:
tabHost.setOnTabChangedListener(new OnTabChangeListener () {
#Override
public void onTabChanged(String tabID) {
Log.d("DEB", "Tab changed:" + tabID);
}});
This part works fine, everytime I change the tab a log entry is created.
Now I would like to call this method in the newly chosen tab:
public void refreshContent() {
setContentView(R.layout.listartists);
ArrayList<Song> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView02);
lv1.setAdapter(new MyCustomBaseAdapter2(this, searchResults));
Log.d("DEB", "ArtistView: Content refreshed.");
}
Is there anyone who can explain a possible solution for this?
Thank you in Advance!
Greetings from Germany!
Sebastian

try to put this code in onResume of the activity you want to execute this code when tab is changed because when the tab is clicked onResume is called for that activity

Related

multiple click on single button while opening from one fragment to another?

I have created an application which uses the fragment, I am opening a fragment on the click of the first fragment with custom animation, meanwhile the animation is going on I am able to click the button on the first fragment and it creates 2 fragments. how can I not click on my button while moving from one fragment to another, I just don't want double click of the same button.
can anyone help me?
Try below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.setEnabled(false);
}
});
// on animation complete, enable it
// button.setEnabled(true);
You could try set android:clickable in your XML layout to determine whether a button can be clicked.
You could implement the following method into your code and call it when needed.
public void myMethod(boolean isLoading){
myButton.setEnabled(!isLoading);
}
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {view.setEnabled(false);}
});
Try using myButton.setEnabled(false) in your click callback function.
Try the below kotlin snippet
view.setOnClickListener {
val tag = "my_dialog"
val oldFragment: Fragment? = supportFragmentManager.findFragmentByTag(tag)
if(!(oldFragment?.isAdded == true)) {
val myDialogFragment = MyDialogFragment.newInstance()
myDialogFragment.show(supportFragmentManager, tag)
}
}
In case a fragment(with the tag specified) is already added to the activity then this code prevents new fragment creation and adding it to the activity.

How to refresh a listadapter from within an onclick

I have an Android app that displays a list using a ListView, and there's an action bar button to clear said list. I decided to add a confirmation dialog so people don't accidentally delete all their entries, and I'm running into problems. If I use setListAdapter inside the onclick for the "yes" button within the dialog, it won't compile. If I use it outside that onclick, it'll work but not refresh the list until the user backs out of the activity and goes back into it, which for obvious reasons is not appropriate. Here's my method that gets called when the "clear list" action bar button is pressed, which contains the relevant onclick for the internal buttons.
I have a feeling I shouldn't be using "this" in setListAdapter since with the dialog, this no longer corresponds to the listview activity I think? But I'm not sure what to put instead.
public void clearTrigger(MenuItem item) {
//Set up a dialog with two buttons to verify that the user really wants to delete
everything
confirm = new Dialog(display.this);
confirm.setContentView(R.layout.conf);
confirm.setTitle("Confirm deletion");
yes = (Button)confirm.findViewById(R.id.yes);
//If the user says yes, then delete everything
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Remove everything from Hours.
Hours.clear();
String tempH = " ";
String tempW = " ";
//Then save it again in it's new, empty state so that it doesn't reappear the next time the app is run.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor edit = prefs.edit();
edit.putString("SAVEDATA", TextUtils.join(",", Hours));
edit.remove("totalh");
edit.remove("totalw");
edit.commit();
//And finally... refresh the list view - doesn't work
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_list, R.id.listText, Hours));
confirm.dismiss();
}
});
confirm.show();
}
The first argument of ArrayAdapter constructor is a Context, so you need to pass the Activity to it, something like new ArrayAdapter<String>(MyActivity.this, ...). Right now you're passing it your instance of OnClickListener which is why it's giving compiler error.
But the best way to update a ListView is to make changes on the ArrayAdapter itself using methods like adapter.add and adapter.remove, and then call adapter.notifyDataSetChanged(). In your case, you would call adapter.clear().

Android ListView within fragment doesn't refresh without a button click

My ListView doesn't refresh its contents when I call the appropriate method unless the method was invoked with a button click.
The follow example code is how my test button works (temporary button to check to see if it was a problem with my refresh code):
testBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
exampleRefresh();
}
});
And the method it calls:
public void exampleRefresh() {
exampleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayOfItems());
exampleListView.setAdapter(exampleAdapter);
}
This works fine.
However, if I call exampleRefresh() in the switch statement for a context menu, nothing happens. Again, when I click the test button, the ListView refreshes instantly. These are calling the same method, I don't understand the issue.
I have tried adding nofifyDataSetChange(), but it doesn't work. The ListView only refreshes when I invoke a button press.
It's also worth noting that even if I call the method on the invoke of a context menu, it refreshes. It does not do anything without an invoke, it appears.
Any help will be very much appreciated.
have you tried to
exampleAdapter = new ArrayAdapter<String>();
exampleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayOfItems());
exampleListView.setAdapter(exampleAdapter);
this should force it to clear and re-add the
if you are changing data in list and then you want to refresh listview then dont call setadapter method again. just call below method:
exampleAdapter.notifyDataSetChange();
Try invoking notifyDataSetChange() from exampleListView.post() like this:
exampleListView.post(new Runnable() {
public void run() {
exampleAdapter.notifyDatasetChange();
}
}

How to dynamically add rows from a table (sqlite) to layout? (Android)

I am trying to take all the rows from my db and add it to the current layout, also, making each row clickable in the layout to take the user to a new screen with the id...
Here is my current code, but stuck on that part... I understand that I can put an onClickListener, but then does it have to be a button?
For a visual representation refer to a notepad app on any device where each note title appears and clicking on it takes you to that note.
public class MainActivity extends Activity implements OnClickListener {
private Button add_new_dictionary;
// Database helper
private DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// db setup
db = new DatabaseHelper(getApplicationContext());
// get all dictionaries
List<db_dictionary> allDictionaries = db.getAllDictioniaries();
for (db_dictionary dictionary_found : allDictionaries) {
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
}
add_new_dictionary = (Button) findViewById(R.id.add_new_dictionary);
}
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
}
}
To re-iterate the question: How do I go about dynamically taking rows from my db and adding it to my layout dynamically based on how many results are returned from the query? (However, the rows should be able to point to a new screen with the dictionary id)
All views in android can implement the OnClickListener interface. So no, it doesn't HAVE to be a button.
As you've decided to use the activity to handle this then you need to tell your code to pass the event to your implementation wihin your activity.
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
dictionary_button.setOnClickListener(this);
A trick I use to store information is the setTag method which would allow you to retrieve the correct reference during your onClick:
dictionary_button.setTag(some_record_id);
Then retrieve it later:
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
else (
Object tag = v.getTag();
//now launch the detail activity using the data from the tag
}
}
You should really look into ListAdapters and cursors to do this properly, but this method should get you going for now
If you need to pick data from a db and show it as a list (getting click events) you should probably look into CursorAdapter and ListView
http://developer.android.com/reference/android/widget/CursorAdapter.html
http://developer.android.com/reference/android/widget/ListView.html
You can fins many examples on the web on how to use a cursoradapter and the listview

TextView setText from another class

My problem is that i've a tabhost with two tabs.
The first is a listview and the second only have two textviews.
What i want is when i click on an item in the listview on tab one, the array position id (int) should be sent to tab/class two, where there is an array who fetches a text by the position id that was sent.
The switching of tabs is done, but i fail everytime i try to send the position-id.
This is how i send it:
TAB ONE:
//This function is called when i click a listitem
public void setDetails(String Text, long arr_id){
Detail detail = new Detail();
detail.setDetail(); // right now i don't send the parameters because it wont even work with or without it.
}
TAB TWO:
TextView descr;
TextView title;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
descr = (TextView)findViewById(R.id.desc);
title = (TextView)findViewById(R.id.title);
}
public void setDetailText(String Text){
descr.setText(Text);
}
public void setDetailTitle(String Text){
title.setText(Text);
}
public void setDetail(){
this.setDetailTitle("example text");
this.setDetailText("example text2");
}
I want to set the text on tab two BEFORE it switch to tab two.
This works if i use SetDetail() and setDetailTitle() in the same tab/class, but not in another.
I've googled my ass off, please help me
i do this in my code using getParent() or getActivity() methods in my TabActivity and inner Activitys, cause if we use a TabActivity (wich subclass the ActivityGroup class) we can obtain the 'TextActivity' using ActivityManager and obtain the activity instance, so, here we can call the setDetail() method, and this will execute before the Activity is showed,
in your ListActivity do something like this
((MyTextActivity)((MyTabActivity)getParent()).getLocalActivityManager().getActivity(ACTIVITY_ID)).setDetail();
this works only if in you start the childs activity within your TabActivity with:
intent = new Intent(ACTIVITY_ID).setClass(this, MyMapActivity.class);
getLocalActivityManager().startActivity(ACTIVITY_ID, intent);

Categories