Ion C
Ion C

Reputation: 323

Add data to a listview on fragment

I have an fragment on activity, with an listview element on it. Can't add data to listview. Can someone tell me what is wrong?

public class MainActivity extends FragmentActivity {

    public String[] listS ={"item1","item2","item3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        MainFragment f = new MainFragment();
        ft.replace(R.id.fragcontainer, f);
        ft.commit();    
        //here the problem occurs
        ListView lv = (ListView)findViewById(R.id.listf);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
    }

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

Fragment's class:

public class MainFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
        return inflater.inflate(R.layout.fragment_layout, container, false);
    }

}

Upvotes: 3

Views: 10383

Answers (4)

kesava
kesava

Reputation: 132

Please use the list view in the design windows as below. I faced the exact problem and now i could able to find the view in source code.

The main issue is IDE is appending "android" infront of ID

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

Upvotes: 0

noni
noni

Reputation: 2947

The problem is that you are asking to the activity for that ListView

"this."findViewById(...

You can ask the fragment for the view, or managing the content of the listView inside your Fragment class.

Upvotes: 0

NAP-Developer
NAP-Developer

Reputation: 3796

When u add code for define your class,

public class MainFragment extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis) {
            View view = inflater.inflate(R.layout.my_fragment, container, false);
            ListView listView = (ListView) view.findViewById(R.id.list);
            return view;
        }
    }

Upvotes: 0

Brian Dupuis
Brian Dupuis

Reputation: 8176

There's a number of problems with this method, but the root of it is that the FragmentTransaction replace() method that adds the fragment that, I assume, contains the ListView you're trying to find is an asynchronous process. The fragment isn't added to the Activity's view hierarchy immediately but some time later.

If you want to add things to a ListView in a Fragment, why not do it from within the MainFragment subclass' onCreateView() method instead? That's how it's intended to work.

EDIT: A typical onCreateView():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    ListView lv = (ListView) view.findViewById(R.id.listf);
    lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));             
    return view;
}

Upvotes: 6

Related Questions