How to get the reference to the ListView from a ListFragment?

2

I have a ListFragment trying to access the ListView where Adapter has inserted the objects, but findViewById is returning null , does anyone have any ideas?

The fragment code in XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:id="@+id/listViewTipos"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</ListView>
<RadioGroup
    android:id="@+id/rgTipo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<RadioButton
    android:id="@+id/rbTipo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>

The Adapter code:

public class TipoAdapter extends ArrayAdapter {
    private ArrayList<Tipo> tipos;
    private Context context;
    private int mResource;
    public TipoAdapter(Context context,int resource,ArrayList<Tipo> tipos) {
        super(context,resource,tipos);
        this.tipos = tipos;
        this.context = context;
        mResource= resource;
    }

    @Override
    public int getCount() {
        return tipos.size();
    }

    @Override
    public Tipo getItem(int i) {
        return tipos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            view= LayoutInflater.from(context).inflate(R.layout.fragment_tipo_trilha,viewGroup);
            holder= new ViewHolder();
            holder.rb=(RadioButton) view.findViewById(R.id.rbTipo);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        Tipo b = getItem(i);
        holder.rb.setText(b.tipNome);
        return view;
    }

    private static class ViewHolder{
        RadioButton rb;
        public ViewHolder(){
        }
    }
}

The fragment code:

public class TiposFragment extends ListFragment implements ListView.OnItemClickListener{
private ArrayList<Tipo> tipos;
private TipoAdapter tipoAdapter;
private int selecionado;

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    try {
        Bundle bundle = getArguments();
        if(bundle!=null){
            tipos=bundle.getParcelableArrayList("tipos");
        }


    }catch (Exception e){
        Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
    }

    if(tipos!=null) {
        try {
            tipoAdapter = new TipoAdapter(getContext(), R.layout.fragment_tipo_trilha, tipos);
            setListAdapter(tipoAdapter);
            ListView listaTipos = (ListView) getView().findViewById(R.id.listViewTipos);//returning null
            listaTipos.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listaTipos.setOnItemClickListener(this);
        }catch (Exception e){
            Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
        }
    }

}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    selecionado=tipos.get(i).tipCod;
}
    
asked by anonymous 04.05.2017 / 17:15

1 answer

1

To take advantage of all the features of ListFragment the ListView should have id declared as follows:

android:id="@id/android:list"

or

android:id="@android:id/list"

So you can get the reference to the ListView using the getListView () :

ListView listaTipos = getListView();

You should also implement the onCreateView() method of the ListFragment. Anything like this:

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

Replace R.layout.list_fragment with the name of the layout of your fragment.

    
04.05.2017 / 22:05