Android


SUBMITTED BY: Guest

DATE: Dec. 27, 2013, 5 a.m.

FORMAT: Text only

SIZE: 5.8 kB

HITS: 888

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.TextView;
  8. public class StudentAdapter extends ArrayAdapter<Student> {
  9. Context context;
  10. int layoutResourseId;
  11. Student data[] = null;
  12. public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
  13. super(context, layoutResourceId, data);
  14. this.context = context;
  15. this.layoutResourseId = layoutResourceId;
  16. this.data = data;
  17. // TODO Auto-generated constructor stub
  18. }
  19. @Override
  20. public View getView(int position, View convertView, ViewGroup parent) {
  21. View row = convertView;
  22. StudentHolder holder = null;
  23. if(row == null)
  24. {
  25. LayoutInflater inflater = ((Activity)context).getLayoutInflater();
  26. row = inflater.inflate(layoutResourseId, parent, false);
  27. holder = new StudentHolder();
  28. //Associate the Items in the holder with a view in the XML file.
  29. holder.tvStNumber = (TextView)row.findViewById(R.id.tvStNumber);
  30. row.setTag(holder);
  31. } else
  32. {
  33. holder = (StudentHolder) row.getTag();
  34. }
  35. //Setting the text for the items
  36. Student item =data[position];
  37. holder.tvStNumber.setText(item.GetStudentNumber());
  38. return row;
  39. }
  40. //Student Holder
  41. static class StudentHolder
  42. {
  43. TextView tvStNumber;
  44. TextView tvStDescription;
  45. }
  46. }
  47. listView1.setOnItemClickListener(new OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
  50. try{
  51. String listValue = ((TextView)view).getText().toString(); //This is where the exception is happening. The below text should have been commented out.
  52. // String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening.
  53. Toast.makeText(getApplicationContext(), "You click" + position, Toast.LENGTH_LONG).show();
  54. String d = "Test toast.";
  55. }catch(Exception e)
  56. {
  57. Log.e("Error Message", e.getMessage());
  58. }
  59. }
  60. });
  61. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  62. xmlns:tools="http://schemas.android.com/tools"
  63. android:layout_width="match_parent"
  64. android:layout_height="match_parent"
  65. android:orientation="vertical"
  66. tools:ignore="HardcodedText"
  67. >
  68. <ScrollView
  69. android:layout_width="fill_parent"
  70. android:layout_height="wrap_content"
  71. >
  72. <LinearLayout
  73. android:id="@+id/linearylayout1"
  74. android:layout_width="match_parent"
  75. android:layout_height="wrap_content"
  76. android:orientation="horizontal"
  77. >
  78. <ListView
  79. android:id="@+id/lvStudent"
  80. android:layout_width="match_parent"
  81. android:layout_height="144dp"
  82. android:descendantFocusability="beforeDescendants"
  83. >
  84. </ListView>
  85. </LinearLayout>
  86. </ScrollView>
  87. </LinearLayout>
  88. ?xml version="1.0" encoding="utf-8"?>
  89. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  90. android:layout_width="fill_parent"
  91. android:layout_height="fill_parent"
  92. android:orientation="vertical"
  93. android:focusable="false"
  94. >
  95. <TextView
  96. android:id="@+id/tvStNumber"
  97. android:layout_width="100sp"
  98. android:layout_height="wrap_content"
  99. android:layout_alignParentLeft="true"
  100. android:layout_alignParentTop="true"
  101. android:layout_marginLeft="10sp"
  102. android:layout_marginTop="5sp"
  103. android:text="EE#" />
  104. <TextView
  105. android:id="@+id/tvStDescription"
  106. android:layout_width="100sp"
  107. android:layout_height="wrap_content"
  108. android:layout_alignParentTop="true"
  109. android:layout_marginLeft="10sp"
  110. android:layout_toRightOf="@+id/tvStNumber"
  111. android:text="Item Description" />
  112. </RelativeLayout>
  113. String listValue = (((TextView) ((RelativeLayout) v).getChildAt(0)).getText().toString());
  114. String listValue = ((TextView)view).getText().toString();
  115. listView1.setOnItemClickListener(new OnItemClickListener() {
  116. @Override
  117. public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
  118. try{
  119. String listValue = parent.getItemAtPosition(position).GetStudentNumber(); //This is where the exception is happening. The below text should have been commented out.
  120. // String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening.
  121. Toast.makeText(getApplicationContext(), "You click" + position, Toast.LENGTH_LONG).show();
  122. String d = "Test toast.";
  123. }catch(Exception e)
  124. {
  125. Log.e("Error Message", e.getMessage());
  126. }
  127. }
  128. });

comments powered by Disqus