Create listview in a Fragment on android

1

I am creating an application in which I include a listview , in a Fragment . Said listview I had already executed in a normal activity and all very well, now that I want to run in a fragment I get several errors.

This is my Java code of the fragment

public class Fragmento01 extends Fragment {

ListView listView;
String[] elementos = {"jose", "pedro", "maria", "miguel", "luis", "daniel", "elena", "Laura", "Sofia"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    listView = (ListView) findViewById(R.id.listview);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,elementos);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_SHORT).show();

        }
    });
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragmento01, container, false);
}

}

this is my fragment xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jorge.appciudadanoconsciente.FragmentoInicio"
android:orientation="vertical">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listview"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"

    android:dividerHeight="10dp"
    />

</LinearLayout>
    
asked by Jorge1023 03.11.2016 в 01:55
source

3 answers

2

The error you are making is that you are looking for the Id of your listview as if it were part of onCreate of a Activity .

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

    return inflater.inflate(R.layout.fragment_fragmento01, container, false);
}

@Override
public void onActivityCreated(Bundle state) {
    super.onActivityCreated(state);
    listView = (ListView)getView().findViewById(R.id.listview);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_expandable_list_item_1,elementos);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity().getApplicationContext(),String.valueOf(position),Toast.LENGTH_SHORT).show();

    }
});
}

Here we look for the ID of your listview from getView and not looking for the Id in a layout. The view is returned and configured in onCreateView

    
answered by 03.11.2016 / 02:15
source
1

Change this:

listView = (ListView) findViewById(R.id.listview);

By:

listView = (ListView)getActivity().findViewById(R.id.listView);
    
answered by 03.11.2016 в 02:00
0

As it says sioesi, you are looking for the id of the view in a layout that does not contain it

I usually work like this and it works perfect:

public class test extends Fragment {
private View v;
private EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v=inflater.inflate(R.layout.fragment_test, container, false);
    editText = v.findViewById(R.id.editText2);
    return v;
}

} that way you "save" the reference to the view in the variable v to be able to use it to initialize the components within the correct view

v=inflater.inflate(R.layout.fragment_test, container, false);

Then, get the reference you need from the variable "v". That way you can initialize all the controls that are inside the xml associated with that fragment and you can use them as you usually do.

editText = v.findViewById(R.id.editText2);

at the end do not forget to return the view that is stored in the variable "v"

return v;
    
answered by 28.07.2018 в 16:39