初始化listadapter时出错

时间:2013-09-18 22:16:08

标签: android android-listview

我正在尝试显示可以放入联系人的组列表(放在arraylist中)。我在实现ListAdapter以显示屏幕上的联系人组时遇到问题。

到目前为止我写的代码。

package awad865.se206.contactmanager;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FragmentTab1 extends Fragment implements ActionBar.TabListener {

    private Fragment mFragment;
    private ListView listView;
    private Button button1;
    private Button button2;
    private Button button3;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //Get the view from fragment1.xml
        getActivity().setContentView(R.layout.fragment1);

        listView =(ListView) getActivity().findViewById(R.id.main_group_listview);
        button1=(Button) getActivity().findViewById(R.id.button_search);
        button2=(Button) getActivity().findViewById(R.id.button_addgroup);
        button3=(Button) getActivity().findViewById(R.id.button_options);


        setUpListView();
    }



    private void setUpListView(){
        List<String> displayList = new ArrayList<String>();
        displayList.add("Coworkers");
        displayList.add("Family");
        displayList.add("Friends");

        ListAdapter listAdapter = new ArrayAdapter<String>(FragmentTab1.this,
                android.R.layout.simple_list_item_1, displayList);
        listView.setAdapter(listAdapter);


    }



    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mFragment = new FragmentTab1();
        //Attach fragment1.xml layout
        ft.add(android.R.id.content, mFragment);
        ft.attach(mFragment);

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        //Remove fragment1.xml layout
        ft.remove(mFragment);
    }
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }



}

每当我写下代码行时:

ListAdapter listAdapter = new ArrayAdapter<String>(FragmentTab1.this,android.R.layout.simple_list_item_1, displayList);

我收到错误说:

  

“构造函数   ArrayAdapter(FragmentTab1.this,android.R.layout.simple_list_item_1,   displayList)未定义。“

我不知道如何解决这个问题。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

试试这个:

ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, displayList);

FragmentTab1.this不会为您提供上下文,但会getActivity()