开发者

ListView with a dynamic intents that change a TextView and WebView on each call

开发者 https://www.devze.com 2023-03-16 17:18 出处:网络
Activity that is sending the putExtra() @Override protected void onListItemClick(ListView l, View v, int position, long id)

Activity that is sending the putExtra()

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    final Intent intent = new Intent();
    // Set up different intents based on the item clicked: 
    switch (position)
    {
        case ACTIVITY_0:
            intent.putExtra("Value1", "Display this text!");
            intent.setClass(this, com.a.someclass.class);
}
    startActivityForResult(intent, position);
}

Activity receiving the putExtra()

@Override
protected void onCreate(Bundle bundle) {
    // TODO Auto-generated method stub
    super.onCreate(bundle);
    setContentView(R.layout.information);
    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        return;
    }
    String value1 = extras.getString("Value1");

    if (value1 != null) {
        informationTitle = (TextView)findViewById(R.id.informationTitle);
        informationText = (WebView开发者_如何转开发)findViewById(R.id.informationText);
        informationTitle.setText(value1);
    }

Original Message:

i have been searching everywhere for a good tutorial on this, i have posted my code online so people can look at it but have not found the help i needed.

I am new to this and what i am basically trying to do is just have a list of items which are all linked to one class that has a dynamic TextView that will be used for the title and a WebView for content, And so basically when a item is clicked on the list it will open up the new activity/intent and it will also pass arguments to change the TextView and WebView accordingly.

I know how to open a new activity by making a new class for each item on the list but i am pretty sure there is an easier way where i can reuse one class and just keep changing the TextView and WebView. The reason i say this is because i have 15 items on my list, but that will expand overtime so i dont want to be making 50-60 different classes to open up each new item.

If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!

Thank you


To accomplish that, you would, instead of using a different Intent for each list item, call the same Activity with the same Intent, but pass extras along with it.

Let's say, for instance, that you want to pass a different String depending on which list item is clicked. You would want to

myIntent.putExtra(String key, String value);
startActivity(myIntent);

The Activity that you start with this Intent will then be able to grab these extras in its onCreate() using

Bundle extras = getIntent().getExtras();

and access the extras you put in the Intent using the methods outlined here. This way you can use a single Activity for all list items but have the Activity display different information based on the extra values.


I'll give you a link where you can get the best tutorial for that... http://www.vogella.de/articles/Android/article.html

0

精彩评论

暂无评论...
验证码 换一张
取 消