Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. public class BreakfastFragment extends ListFragment {
  2.  
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
  5. View v = inflater.inflate(R.layout.row_layout, container, false);
  6. TextView txt = (TextView) v.findViewById(R.id.label);
  7. Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/typewriter.ttf");
  8. txt.setTypeface(font);
  9. return v;
  10. }
  11.  
  12. @Override
  13. public void onActivityCreated(Bundle savedInstanceState) {
  14. super.onActivityCreated(savedInstanceState);
  15.  
  16. ArrayList<Map<String, String>> list = buildData(); // List that contains Arrays of the form String, String
  17. String[] from = { "name", "purpose" };
  18. int[] to = { R.id.label, R.id.price };
  19.  
  20. SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.row_layout, from, to); //A SimpleAdapter(context, data source, layout for each row, from, to)
  21. setListAdapter(adapter);
  22. }
  23. private ArrayList<Map<String, String>> buildData() {
  24. ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
  25. list.add(putData("Greek Yoghourt & Fruit","£2.25"));
  26. list.add(putData("Fruit Cobbler", "£2.25"));
  27. list.add(putData("Honey Roasted Fruit & Yoghurt", "£2.25")); //PutData is defined below
  28. list.add(putData("Fruit Fool", "£2.25"));
  29. list.add(putData("Bircher", "£2.25"));
  30. list.add(putData("Porridge","£1.95"));
  31. list.add(putData("Stewed Fruit Topping","£0.50"));
  32. list.add(putData("Candied Pralines","£0.50"));
  33. list.add(putData("House Bacon", "£3.75"));
  34. list.add(putData("Simply Bacon","£3.75"));
  35. list.add(putData("House Mushroom", "£3.50"));
  36. list.add(putData("Peanut Butter & Homemade Jam", "£3.50"));
  37. list.add(putData("Eggs on toast","£4.00"));
  38. list.add(putData("House Eggs","£4.75"));
  39. list.add(putData("Spanish Eggs","£4.75"));
  40. list.add(putData("Smoked Salmon Eggs","£4.75"));
  41. list.add(putData("The Full Breakfast","£5.25"));
  42. list.add(putData("Chorizo Hash", "£5.25"));
  43. return list;
  44. }
  45. private HashMap<String, String> putData(String name, String purpose) {
  46. HashMap<String, String> item = new HashMap<String, String>();
  47. item.put("name", name);
  48. item.put("purpose", purpose);
  49. return item;
  50. }
  51. }
  52.  
  53. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  54. android:layout_width="fill_parent"
  55. android:layout_height="fill_parent"
  56. android:orientation="vertical" >
  57.  
  58. <ListView
  59. android:id="@+id/listView1"
  60. android:layout_width="match_parent"
  61. android:layout_height="wrap_content" >
  62. </ListView>
  63.  
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66. android:orientation="horizontal"
  67. android:layout_width="fill_parent"
  68. android:layout_height="wrap_content"
  69. >
  70.  
  71. <TextView
  72. android:id="@+id/label"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:layout_gravity="center_vertical"
  76. android:padding="10dp"
  77. />
  78.  
  79. <LinearLayout
  80. android:orientation="vertical"
  81. android:layout_width="0dp"
  82. android:layout_height="wrap_content"
  83. android:layout_gravity="center_vertical"
  84. android:paddingRight="5sp"
  85. android:layout_weight="1"
  86. >
  87.  
  88. <TextView android:id="@+id/price"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_gravity="end"
  92. android:textSize="16sp" /> <!-- layout_gravity is what places prices on the right! -->
  93.  
  94. </LinearLayout>
  95.  
  96. <LinearLayout
  97. android:layout_width="0dp"
  98. android:layout_height="0dp"
  99. android:layout_gravity="center_vertical"
  100. android:paddingRight="5sp"
  101. >
  102.  
  103. <ListView
  104. android:id="@android:id/list"
  105. android:layout_width="0dp"
  106. android:layout_height="0dp"
  107. android:layout_gravity="end"
  108. />
  109. </LinearLayout>
  110.  
  111. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement