在Fragment中插入ListView

时间:2016-01-31 21:45:22

标签: android listview fragment

我试图找出如何在片段(在tablayout和viewpager中)中插入listview。当我不评论lstOpciones.setAdapter(adaptador)时,该应用程序崩溃了。

目前的代码就是这个:

主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.appbar);
    //Use NoActionBar theme in styles
    setSupportActionBar(toolbar);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new MainFragmentPageAdapter(
            getSupportFragmentManager()));

    TabLayout tabLayout = (TabLayout) findViewById(R.id.appbartabs);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setupWithViewPager(viewPager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

}

MainFragmentPageAdapter类:

public class MainFragmentPageAdapter extends FragmentPagerAdapter {
private final int NUMERO_PAGINAS = 3;
private final String tabTitles[] = new String[] {"Llamadas", "Chats", "Contactos"};
public MainFragmentPageAdapter(FragmentManager fm)
{
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment f = null;
    switch(position)
    {
        case 0:
            f = FragmentLlamadas.newInstance();
            break;
        case 1:
            f = FragmentChats.newInstance();
            break;
        case 2:
            f = FragmentContactos.newInstance();
            break;
    }
    return f;
}

@Override
public int getCount() {
    return NUMERO_PAGINAS;
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}

}

Fragment Chats class:

public class FragmentChats extends Fragment {
public FragmentChats() {
    //Tiene que estar vacío
}
public static FragmentChats newInstance() {
    return new FragmentChats();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final String[] datos =
            new String[]{"Elem1","Elem2","Elem3","Elem4","Elem5"};

    ArrayAdapter<String> adaptador =
            new ArrayAdapter<String>(getActivity().getApplicationContext(),
                    android.R.layout.simple_list_item_1, datos);

    ListView lstOpciones = (ListView)getActivity().findViewById(R.id.chatlist);
    lstOpciones.setAdapter(adaptador);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_chats, container, false);
}

}

非常感谢

1 个答案:

答案 0 :(得分:0)

像这样改变你的方法。您在错误的位置引用了listView。我假设ListView chatlist位于布局fragment_chats

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_chats, container, false);
    final String[] datos =
        new String[]{"Elem1","Elem2","Elem3","Elem4","Elem5"};

    ArrayAdapter<String> adaptador =
        new ArrayAdapter<String>(getActivity().getApplicationContext(),
                android.R.layout.simple_list_item_1, datos);

    ListView lstOpciones = (ListView)view.findViewById(R.id.chatlist);
    lstOpciones.setAdapter(adaptador);

    return view;
}