简体   繁体   中英

How to access ListView in Fragment

This is my code

public class OrderAllFragment extends ListFragment {
    private ArrayList<Order> orders1;
    private ArrayList<String> orders;
    private ListView orderListView;
    public OrderAllFragment() {

        orders1 = new ArrayList<>();
        orders = new ArrayList<>();
        for (int i=0; i<10; ++i){
            Order o = new Order(i, 1+10);
            orders1.add(o);
            orders.add("dummy" + i);
        }
    }


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

        OrderAdapter adapter = new OrderAdapter (getActivity(), orders1);
        orderListView = (ListView) getActivity().findViewById(R.id.listViewOrder);
        orderListView.setAdapter(adapter);

        //ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, orders);
        //setListAdapter(listAdapter);
    }

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

    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {

        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }
}

It works fine for string. That is.

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

But this line gives Null Pointer exception.

 orderListView.setAdapter(adapter);

This is XML file.

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="30dip"
        android:layout_marginTop="50dp"
        />


    <ListView
        android:id="@+id/listViewOrder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="30dip"
        android:layout_marginTop="50dp"
        />

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Games Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

Can somebody help me on what i am doing wrong?

Delete the onCreate method, & use this instead:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_all_order, container, false);
    OrderAdapter adapter = new OrderAdapter (getActivity(), orders1);
    orderListView = (ListView) v.findViewById(R.id.listViewOrder);
    orderListView.setAdapter(adapter);
    return v;
}

Here:

orderListView.setAdapter(adapter);

Line causing issue because orderListView is null .

Reason is onCreate method is called before onCreateView and calling findViewById for accessing View's from Fragment layout will return null due to Views are not ready to access.

And in current code calling findViewById using getActivity() means accessing View from Fragment container Activity instead of Fragment layout because getActivity() method return Context of Activity instead of Fragment which is required to access Views from Fragment.

Use onViewCreated or onCreateView for accessing Views from Fragment layout.like:

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        orderListView = (ListView) view.findViewById(R.id.listViewOrder);
        orderListView.setAdapter(adapter);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM