ListView中有一个动态的意图是改变一个TextView和web视图在每次调用 [英] ListView with a dynamic intents that change a TextView and WebView on each call
问题描述
活动正在发送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);
}
活动接收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);
}
原始消息:
我一直在四处寻找上一个很好的教程,我已经发布我的code网上,使人们可以看到它,但还没有找到我所需要的帮助。
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.
我是新来这个,什么我基本上是试图做的就是有一个项目列表它们都链接到一个类,它有一个动态的TextView将用于标题和web视图的内容,所以基本上,当一个项目被点击的名单上,将开辟新的活动/意图,它也将传递参数改变TextView的和的WebView相应。
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.
我知道如何做一个新的类列表中的每个项目以打开一个新的活动,但我pretty的肯定有一个更简单的方法,我可以重复使用一个类,只是不断变化的TextView的和web视图。我之所以说这是因为我有15个项目在我的名单,但将扩大加班,所以我不希望被做50-60不同类别的开拓每一个新项目。
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.
如果一些可以点我在正确的教程或给我一些有识之士就在这里,我真的很AP preciate了!
If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!
感谢您
推荐答案
要完成的,而不是使用不同的意图
每个列表项,调用,你会,同样活动
用相同的意图
,但通过演员与它一起。
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);
在活动
,你开始用这种意图
将能够抓住这些演员在其的onCreate()
用
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.
这篇关于ListView中有一个动态的意图是改变一个TextView和web视图在每次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!