动态跳转到一个选项卡并同时更改其内容?

发布于 2024-12-16 22:37:27 字数 1475 浏览 4 评论 0原文

在第一个选项卡中,我有一个列表视图,从中提取字符串,在第二个选项卡中,我有一个文本视图。当我单击列表中的某个项目时,我应该进入第二个选项卡,并且字符串值应该显示在文本视图中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-12-23 22:37:27

我的建议是使用广播。我认为时机需要粘性广播。也许是这样的:

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();

    // create the intent to send and give it the data
    Intent i = new Intent("updateTextView");
    i.putExtra("textFromList", mystring);
    sendStickyBroadcast(i);

    int i=1;//the tab where I want to go, and MyActivity is its content 
    taba.ChangeTab(i);
}

然后在 MyActivity 中,编写这样的内容:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // your onCreate method
    registerReceiver(updateTextReceiver, new IntentFilter("updateTextView"));
}

protected void onDestroy() {
    super.onDestroy();
    // your onDestroy method
    unregisterReceiver(updateTextReceiver);
}

private BroadcastReceiver updateTextReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        tv = (TextView)findViewById(R.id.tvid);
        String myString = intent.getStringExtra("textFromList");
        if (myString != null) {
            tv.setText(myString);
        }
    }

};

对于粘性广播,清单中需要额外的权限。我不确定,但如果您交换发送广播和更改选项卡的顺序,也许您可​​以发送常规广播。

My suggestion would be to use a broadcast. I think that the timing would require a sticky broadcast. Maybe something like 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();

    // create the intent to send and give it the data
    Intent i = new Intent("updateTextView");
    i.putExtra("textFromList", mystring);
    sendStickyBroadcast(i);

    int i=1;//the tab where I want to go, and MyActivity is its content 
    taba.ChangeTab(i);
}

And then in MyActivity, write something like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // your onCreate method
    registerReceiver(updateTextReceiver, new IntentFilter("updateTextView"));
}

protected void onDestroy() {
    super.onDestroy();
    // your onDestroy method
    unregisterReceiver(updateTextReceiver);
}

private BroadcastReceiver updateTextReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        tv = (TextView)findViewById(R.id.tvid);
        String myString = intent.getStringExtra("textFromList");
        if (myString != null) {
            tv.setText(myString);
        }
    }

};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文