Populate list of custom view using ListFragment
I solved my problem (as system32 suggests on comment) creating a CustomArrayAdapter class, and setting it as the adapter for my listView.
First I changed android:id="@+id/listView1"
to android:id="@android:id/list"
in fragment.xml.
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Menu> {
Context context;
public CustomArrayAdapter(Context context, int textViewResourceId, List<Menu> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtMenuName;
TextView txtMenuDesc;
TextView txtPrice;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Menu rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.txtMenuName = (TextView) convertView.findViewById(R.id.menu_name);
holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.description);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtMenuDesc.setText(rowItem.getDescription());
holder.txtMenuName.setText(rowItem.getName());
holder.txtPrice.setText(String.valueOf(rowItem.getPrice()) + " TL");
//holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
Then I use it in my Fragment class
public static class Fragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
private ListView listView;
private ArrayList<Menu> menuItems;
private CustomArrayAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int num = getArguments().getInt(ARG_SECTION_NUMBER);
// GlobalList is a class that holds global variables, arrays etc
// getMenuCategories returns global arraylist which is initialized in GlobalList class
menuItems = GlobalList.getMenuCategories().get(num).getMenu();
mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, menuItems);
listView.setAdapter(mAdapter);
}
}