Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set a Custom Listview on ListFragment

I'm trying to set a Custom Listview layout on a ListFragment, I've been reasearching everywhere and i still cant get it to work. I'm new programming with android, so I'll really appreciate your help.

This is my listFragment class

public class ListCins  extends ListFragment {
public static ArrayList<Cin> cins;
private ListView listView;
private pListAdapter mAdapter;

public static ListCins newInstance() {
ListCins f = new ListCins();
return f;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState);
//Los fragments soportan presentar un msj cuando la lista esta vacia.
setEmptyText("Nothing yet, but working");    

}


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




cins = new ArrayList<Cin>();
cins.add(new Cin("Agenter", 1));
cins.add(new Cin("Ora", 2));
cins.add(new Cin("Vistia", 2));
cins.add(new Cin("Bluel", 2));


mAdapter = new pListAdapter(this.getActivity(), cins);
setListAdapter(mAdapter);


}



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {


super.onViewCreated(view, savedInstanceState);      
setEmptyText("Nothing yet 2, but working");
registerForContextMenu(getListView());

setHasOptionsMenu(true);
}






@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Toast mTosat = Toast.makeText(this.getActivity(),"clicked:"+String.valueOf(position)+" "+cins.get(position).nombre , 200);
mTosat.show();


}



}

And this is the list_fragment.xml i want to apply to the listfragment.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">


<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />


<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"        />
</LinearLayout>  

I also have a custom arrayAdapter, but i dont know if it has something to do with the custom listview layout.

Thanks in advance...

like image 412
PaulNunezM Avatar asked Jan 27 '14 06:01

PaulNunezM


2 Answers

You need to override onCreateView and inflate/return your custom layout. The ListFragment should handle the rest for you. This is described on the Screen Layout section of http://developer.android.com/reference/android/app/ListFragment.html

like image 161
NasaGeek Avatar answered Nov 17 '22 07:11

NasaGeek


Thanks NasaGeek, I was trying to inflate it just like they said, like this:

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

but it wasen't working, then i found the problem. For some reason this method makes the code goes like crazy.

setEmptyText("Nothing yet, but working");   

If you could explain me why, it will be awesome.

Thanks.

like image 20
PaulNunezM Avatar answered Nov 17 '22 08:11

PaulNunezM