简体   繁体   English

添加标签视图android

[英]Add tab view android

I have made a simple contact apps and now I wish to add tab view to this app. 我做了一个简单的联系人应用程序,现在我想向该应用程序添加标签视图。 I am following the tutorial here . 我在这里关注教程。 Below is part of the source code for my MainActivity.jave: 以下是MainActivity.jave的部分源代码:

public class MainActivity extends ListActivity {

    private ListView contactListView;
    private CursorAdapter contactListViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contactListView = getListView(); // get ListView id
        contactListView.setOnItemClickListener(viewContactListListener);

        String[] from = new String[] { "familyName" }; // built an String array
                                                        // named "from"
        int[] to = new int[] { R.id.contactTextView };
        contactListViewAdapter = new SimpleCursorAdapter(MainActivity.this,
                R.layout.contact_list_item, null, from, to);
        setListAdapter(contactListViewAdapter); // set adapter
    }

I tried to change "public class MainActivity extends ListActivity" to "public class MainActivity extends TabActivity implements OnTabChangeListener{". 我试图将“公共类MainActivity扩展ListActivity”更改为“公共类MainActivity扩展TabActivity实现OnTabChangeListener {”。 However, I get the error "The method getListView() is undefined for the type MainActivity" and other similar errors. 但是,我收到错误消息“方法MainList类型的getListView()未定义”和其他类似错误。 I need suggestions on how to fix this. 我需要有关如何解决此问题的建议。 Thanks for any help. 谢谢你的帮助。

ListActivity provides some help methods to manage ListView so if you want to change your parent activity from ListActivity to TabActivity you need to handle ListActivity behaviour yourself. ListActivity提供了一些管理ListView帮助方法,因此,如果要将父活动从ListActivityTabActivity ,则需要自己处理ListActivity行为。 Basically you need to get and store somewhere the ListView object. 基本上,您需要获取和存储ListView对象的某个位置。 Something like mListView = (ListView) findViewById(android.R.id.list) on your onCreate method and then implement missing method onCreate方法上类似mListView = (ListView) findViewById(android.R.id.list) ,然后实现缺少的方法

ListView getListView()
{
    return mListView;
}

Also setting your adapter will be a bit different. 设置适配器也有所不同。 Instead of calling setListAdapter(contactListViewAdapter); // set adapter 而不是调用setListAdapter(contactListViewAdapter); // set adapter setListAdapter(contactListViewAdapter); // set adapter there should be contactListView.setAdapter(contactListViewAdapter); setListAdapter(contactListViewAdapter); // set adapter应该有contactListView.setAdapter(contactListViewAdapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM