动态跳转到一个选项卡并同时更改其内容?
在第一个选项卡中,我有一个列表视图,从中提取字符串,在第二个选项卡中,我有一个文本视图。当我单击列表中的某个项目时,我应该进入第二个选项卡,并且字符串值应该显示在文本视图中。
public static String mystring;
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString(); //it's ok here
Intent intent=new Intent(this, MyActivity.class);
startActivity(intent);
}
上面的代码(放置在 ListViewActivity 中)是我尝试过的并且它有效(但是 MyActivity 不再是选项卡内容,它是一个全屏独立活动),在 MyActivity 中具有以下代码片段:
tv = (TextView)findViewById(R.id.tvid);
lv = new ListViewActivity();
tv.setText(lv.mystring);
但是,正如我所说,我希望 MyActivity 成为一个选项卡内容,所以我尝试了以下方法:
public MyTabActivity taba = new MyTabActivity();
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString();
int i=1;//the tab where I want to go, and MyActivity is its content
taba.ChangeTab(i);
}
ChangeTab() 是一个 static 方法MyTabActivity(扩展 TabActivity 的活动),它只执行 setCurrentTab(i) 。
因此,这样做时,MyActivity 将始终显示第一个单击的项目的名称,即使我选择了列表中的其他项目。我相信我要做的是 setContent()MyActivity 或使用 static String mystring 做一些事情,我尝试了很多解决方案,但没有结果。我试图尽可能准确地表达我想做的事情,请就这个问题提供一些帮助。
In the first tab I have a listview from where I extract a string, and in the second tab I have a textview. When i click an item in the list, I should go in the second tab and and the string value should be displayed in the textview.
public static String mystring;
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString(); //it's ok here
Intent intent=new Intent(this, MyActivity.class);
startActivity(intent);
}
The code above(placed in ListViewActivity) is what i have tried and it works (but MyActivity is not anymore a tabcontent,it is a full-screen standalone activity), having the following code snippet in MyActivity:
tv = (TextView)findViewById(R.id.tvid);
lv = new ListViewActivity();
tv.setText(lv.mystring);
But, as I said I want that MyActivity to be a tabcontent,so I have tried this:
public MyTabActivity taba = new MyTabActivity();
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString();
int i=1;//the tab where I want to go, and MyActivity is its content
taba.ChangeTab(i);
}
ChangeTab() is a static method within MyTabActivity(the activity that extends TabActivity ) which simply does setCurrentTab(i) .
So, doing this, MyActivity will always display the name of first clicked item, even I select other items in the list then.I belive that what i have to do is setContent() again for MyActivity or do something with that static String mystring, I have tried many solutions but with no result. I tried to be as accurate I could on what i want to do, please some help on this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是使用广播。我认为时机需要粘性广播。也许是这样的:
然后在 MyActivity 中,编写这样的内容:
对于粘性广播,清单中需要额外的权限。我不确定,但如果您交换发送广播和更改选项卡的顺序,也许您可以发送常规广播。
My suggestion would be to use a broadcast. I think that the timing would require a sticky broadcast. Maybe something like this:
And then in MyActivity, write something like this:
For the sticky broadcast, there's an additional permission needed in the manifest. I'm not sure but maybe you could just send a regular broadcast if you swapped the order of sending the broadcast and changing the tab.