【问题标题】:tab, listview and intent选项卡、列表视图和意图
【发布时间】:2011-11-25 15:48:59
【问题描述】:

我有三个 2 选项卡 TabA 和 TabB。 TabA 显示值它没问题。现在在 Tab2 我有显示值的列表视图。每当我单击 listitem 值时,该值必须传递给下一个活动,并且每次单击 listItem 时都必须根据该值创建新选项卡,应该生成选项卡。我的问题是在 listitem intent = new Intent() 的点击事件中不受支持。我的代码如下

public class FriendLists extends TabActivity {
       //Declarations

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendtab);

        connection=XMPPLogic.getInstance().getConnection();
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Reusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

            lv=(ListView) findViewById(R.id.footerlist);

        adapter = new ArrayAdapter<String>(this,R.layout.listitems,R.id.list_content, my_own_listarray);
        lv.setAdapter(adapter);
        //end of listing friends
        final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick( AdapterView<?> parent, View v, int position, long id){
      //problem is here when i try to put intent and pass String S it creates a problem                     
                String S=group.get(position).toString();

//              spec = tabHost.newTabSpec(S).setIndicator(S,
//                        res.getDrawable(R.drawable.tab_friendlist))
//                    .setContent(null);
//              tabHost.addTab(spec);

                // set the message to display
                alertbox.setMessage(S).show();  

            }});

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this,Friends.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("friendlist").setIndicator("Friend List",
                          res.getDrawable(R.drawable.tab_friendlist))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

        tabHost.addTab(tabHost.newTabSpec("onlinefriends").setIndicator("Online Friends",
                res.getDrawable(R.drawable.tab_friendlist))
                .setContent(R.id.footerlist));
    }

}

【问题讨论】:

  • “不支持”是什么意思?它不编译还是产生异常?
  • 构造函数 Intent(new AdapterView.OnItemClickListener(){}, Class) 未定义...这是我声明 intent=new Intent(this,destination.class) 时显示的消息..

标签: android listview tabs android-intent


【解决方案1】:

在匿名内部类内部,您不能引用在其外部声明的非最终局部变量 (intent)。

我假设您只需要一个临时变量来保存您想要传递给setContent 的意图。只需在onItemClick 中声明即可:

Intent intent = new Intent( /* do your thing */ );
spec = tabHost.newTabSpec(S).setIndicator(S,
           res.getDrawable(R.drawable.tab_friendlist))
       .setContent(intent);
tabHost.addTab(spec);

【讨论】:

  • 感谢您的回复,但我尝试了这个但会产生问题...构造函数 Intent(new AdapterView.OnItemClickListener(){}, Class) 未定义
  • 查看constructors of Intent。您正在传递不受支持的参数。
猜你喜欢
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
相关资源
最近更新 更多