选项卡,列表视图和意图

时间:2011-11-25 15:48:59

标签: android listview tabs android-intent

我有三个标签TabA和TabB。 TabA显示值为ok。现在在Tab2中我有listview显示值。每当我单击listitem值时,该值必须传递给下一个活动,并且每次单击listItem时都必须根据该值创建新选项卡,应生成选项卡。我的问题是在listitem的click事件中,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));
    }

}

1 个答案:

答案 0 :(得分:0)

从匿名内部类中,您不能引用在其外部声明的非最终局部变量(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);