How to get the Listview reference of a Listfragment?

Asked

Viewed 94 times

2

I’m with a ListFragment trying to access the ListView in which the Adapter inserted the objects, but the findViewById is returning null, anyone has any idea?

The XML Fragment code:

<?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 code of the Adapter:

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 code of the Fragment:

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;
}

1 answer

1


To take advantage of all the features of Listfragment Listview should have the id thus declared:

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

or

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

This way it is possible to obtain the reference to Listview using the method getListView():

ListView listaTipos = getListView();

It should also implement the onCreateView() listfragment.
Anything like that:

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

Substitute R.layout.list_fragment by name of your layout.

  • Thank you very much!

  • But I still can’t use Listview, setOnItemClickListener doesn’t work, if Listview is right? what could be the problem?

  • That’s another problem. It’s best to ask another question with the updated code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.