Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Dynamically Change Activity Title to Text of Selected List Item

I'm new to Android development.

I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.

When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.

I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.

All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.

Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.

Thanks, SonCoder

like image 376
dmiannay Avatar asked Jul 27 '13 18:07

dmiannay


2 Answers

Not exactly sure what you want but either way.. -You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.

-Write a custom list adapter (extend from base adapter). http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ In the getView method of this class you load the the String field of the model that you want in the textview.

-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.

-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from

parent.getAdapter().getItem(position); 

in the on click method) into the detail activity.

-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.

-if you want to set the titile in the actionbar set:

 this.getActionBar().setTitle(title)
like image 54
Alex Avatar answered Nov 07 '22 11:11

Alex


This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.

setTitle(String title) can be called from anywhere using the activity by the way.

So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.

In the listener you say something in the ways of this:

Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);

Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.

Continue in your MySubActivity's onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String key = getString(R.string.my_title_key);
    String title = intent.getString(key);
    setTitle(title);
}

NOTE: I'm not sure of all method names are correct and such but something like this.

like image 22
Johan S Avatar answered Nov 07 '22 12:11

Johan S