0

I am trying to implement a simple ListView into a Fragment. I have seen many tutorials on how to do so but for some reason my adaptation does not work and I cannot figure out why.

the goal: to have a fragment with one listview.

I understand there are many questions about this and I have seen most of them but mine does not work and I have tried many variations with no success.

From what I have gathered ListFragment and AppCompatActivity are the most appropiate:

//code for fragment.
package com.example.student.hwapp;

import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class TasksFragment extends ListFragment {

public TasksFragment(){}

public String[] list = {"1","2","3","4","5","6"};
ListAdapter theAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View rootView = inflater.inflate(R.layout.tasks_fragment,container,false);

    theAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, list);

    setListAdapter(theAdapter);

    return rootView;

    }

  public static TasksFragment newInstance() {
    TasksFragment fragment = new TasksFragment();
    return fragment;
  }
}

the XML for tasks_fragment (as you can see the list id is list) :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    tools:listitem="@android:layout/simple_list_item_2"
    android:choiceMode="multipleChoice"
    android:footerDividersEnabled="false"
    android:clickable="false" />

</RelativeLayout>

and finally the Activity that holds the fragment:

package com.example.student.hwapp;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainMenu extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

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

    mSectionsPagerAdapter = new       SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch(position){
            case 0:
                return TasksFragment.newInstance();
            case 1:
                return ClassFragment.newInstance();
        }
        return null;
    }


    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
        }
        return null;
    }
}

When I test the TasksFragment as a normal activity, it actually works but when I make it a fragment it doesn't.

Thank you for your help, sorry for the very specific and repetitive question but I really could not find a solution, I'm probably missing something very simple.

EDIT: 14203/com.example.student.hwapp E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

But I DO have a ListView and its id IS list as I showed in the code.

Allicnam
  • 88
  • 1
  • 8
  • you should post logcat errors. – Jay Rathod Feb 19 '16 at 16:27
  • Exception is telling exactly what is wrong.... The id... Also there is no need to override onCreateView in ListFragment – Selvin Feb 19 '16 at 16:31
  • If you're just starting the code, use RecyclerView instead. When used with an AppCompatActivity and a CoordinatorLayout, you get lots good stuff for free. – Daniel Nugent Feb 19 '16 at 16:35
  • I love when people have problems with reading... No, id is wrong... Search for similar questions to get an answer first. – Selvin Feb 19 '16 at 16:37
  • Selvin, could you explain please what is it that is wrong with the id? Also, could you make it an answer so that if that is the solution I can mark it? – Allicnam Feb 19 '16 at 16:38
  • 1
    @Allicnam change `android:id="@+id/list"` in `tasks_fragment` to `android:id="@android:id/list"` - Removes the error `14203/com.example.student.hwapp E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'` – Elvis Chweya Feb 19 '16 at 16:42
  • Yes, the viewPager is in the onCreate method in MainMenu. Hold on a sec I'll go try to change the id. – Allicnam Feb 19 '16 at 16:44
  • Yes that solved it thank you Elvis and Selvin. also, sorry for the silly mistake. – Allicnam Feb 19 '16 at 16:46

1 Answers1

1

Your android:id="@+id/list" is not the same as what is being asked for in the error message, which is android:id="@android:id/list". The former is YOUR object; Android needs its OWN object. Refer to this, among many others.

Community
  • 1
  • 1
DSlomer64
  • 4,234
  • 4
  • 53
  • 88