1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| public class MoviesFragment extends ListFragment {
MyArrayAdapter adapter;
ArrayList<MyClass> myList = new ArrayList<MyClass>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.row_layouts, container, false);
MyClassAdapter adapter = new MyClassAdapter(getActivity(), R.layout.row_layouts, myList);
MyClass food = new MyClass("Greek Yoghourt & Fruit", "£2.25");
MyClass food1 = new MyClass("Fruit Cobbler", "£2.25");
MyClass food2 = new MyClass("Honey Roasted Fruit & Yoghurt", "£2.25");
MyClass food3 = new MyClass("Fruit Fool", "£2.25");
MyClass food4 = new MyClass("Porridge", "£2.25");
MyClass food5 = new MyClass("Stewed Fruit Topping", "£1.95");
MyClass food6 = new MyClass("Candied Pralines", "£0.50");
MyClass food7 = new MyClass("House Bacon", "£0.50");
MyClass food8 = new MyClass("Simply Bacon", "£3.75");
MyClass food9= new MyClass("House Mushroom", "£3.75");
MyClass food10 = new MyClass("Peanut Butter & Homemade Jam", "£3.50");
MyClass food11 = new MyClass("Eggs on toast", "£4.00");
MyClass food12 = new MyClass("House Eggs", "£4.75");
MyClass food13 = new MyClass("Spanish Eggs", "£4.75");
MyClass food14 = new MyClass("Smoked Salmon Eggs", "£4.75");
MyClass food15 = new MyClass("The Full Breakfast", "£5.25");
MyClass food16 = new MyClass("Chorizo Hash", "£5.25");
adapter.add(food);
adapter.add(food1);
adapter.add(food2);
adapter.add(food3);
adapter.add(food4);
adapter.add(food5);
adapter.add(food6);
adapter.add(food7);
adapter.add(food8);
adapter.add(food9);
adapter.add(food10);
adapter.add(food11);
adapter.add(food12);
adapter.add(food13);
adapter.add(food14);
adapter.add(food15);
adapter.add(food16);
setListAdapter(adapter);
return mainView;
}
public class MyClass {
public String plat;
public String prix;
public MyClass(String plat, String prix) {
this.plat = plat;
this.prix = prix;
}
}
public class MyClassAdapter extends ArrayAdapter<MyClass> {
ViewHolder viewHolder = new ViewHolder();
public class ViewHolder {
TextView itemView1;
TextView itemView2;
}
public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) {
super(context, textViewResourceId, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
MyClass mClass = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layouts, parent, false);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.label);
TextView tvHome = (TextView) convertView.findViewById(R.id.price);
// Populate the data into the template view using the data object
tvName.setText(mClass.plat);
tvHome.setText(mClass.prix);
tvName.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/typewriter.ttf"));
tvHome.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/typewriter.ttf"));
// Return the completed view to render on screen
return convertView;
}
}
public class MyArrayAdapter extends ArrayAdapter<HashMap<String, String>> {
public MyArrayAdapter(Context context, ArrayList<Map<String,String>> list) {
super(context, R.layout.row_layouts, R.id.price);
}
public MyArrayAdapter(Context context, HashMap<String, String>[] items) {
super(context, R.layout.row_layouts, R.id.price, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView1 = (TextView) view.findViewById(R.id.label);
textView1.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/typewriter.ttf"));
TextView textView2 = (TextView) view.findViewById(R.id.price);
textView2.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/typewriter.ttf"));
return view;
}
}
} |
Partager