Advertisement
joshua93

Student_details.java

Dec 19th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. package com.example.faceattendancev20;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.AdapterView;
  13. import android.widget.BaseAdapter;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16.  
  17. import java.util.ArrayList;
  18.  
  19. import javax.security.auth.Subject;
  20.  
  21. public class Student_details extends Activity
  22. {
  23.     private ListView StudentDetail;
  24.     private AttendanceDatabase mDatabase;
  25.  
  26.     private static final String TAG = "Student_details";
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState)
  30.     {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_student_details);
  33.  
  34.         StudentDetail = (ListView)findViewById(R.id.detailStudent);
  35.  
  36.         mDatabase = new AttendanceDatabase(Student_details.this);
  37.  
  38.         StudentAdapter adapter = new StudentAdapter(mDatabase.getStudent());
  39.  
  40.         StudentDetail.setAdapter(adapter);
  41.         StudentDetail.setOnItemClickListener(adapter);
  42.     }
  43.  
  44.     @Override
  45.     protected void onResume()
  46.     {
  47.         super.onResume();
  48.     }
  49.  
  50.     @Override
  51.     public boolean onCreateOptionsMenu(Menu menu)
  52.     {
  53.         // Inflate the menu; this adds items to the action bar if it is present.
  54.         //getMenuInflater().inflate(R.menu.mainmenu, menu);
  55.         return true;
  56.     }
  57.  
  58.     @Override //here
  59.     public boolean onOptionsItemSelected(MenuItem item)
  60.     {
  61.         super.onOptionsItemSelected(item);
  62.         switch(item.getItemId())
  63.         {
  64.             case R.id.detailStudent:
  65.                 Bundle dataBundle = new Bundle();
  66.                 dataBundle.putInt("id", 0);
  67.                 Log.d(TAG,"id");
  68.                 Intent intent = new Intent(getApplicationContext(),AddStudentActivity.class);
  69.                 intent.putExtras(dataBundle);
  70.                 startActivity(intent);
  71.                 return true;
  72.             default:
  73.                 return super.onOptionsItemSelected(item);
  74.  
  75.         }
  76.  
  77.     }
  78.  
  79.  
  80.     public class StudentAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
  81.     {
  82.         private ArrayList<Student> mList;
  83.  
  84.         public StudentAdapter(Cursor data)
  85.         {
  86.             mList = new ArrayList<Student>();
  87.  
  88.             data.moveToFirst();
  89.  
  90.             do
  91.             {
  92.                 String studentID = data.getString(data.getColumnIndex(AttendanceDatabase.COLUMN_STUDENT_ID));
  93.                 String studentName = data.getString(data.getColumnIndex(AttendanceDatabase.COLUMN_STUDENT_NAME));
  94.  
  95.                 Student student = new Student(0, studentID, studentName);
  96.  
  97.                 mList.add(student);
  98.             } while (data.moveToNext());
  99.         }
  100.  
  101.         @Override
  102.         public int getCount()
  103.         {
  104.             return mList.size();
  105.         }
  106.  
  107.         @Override
  108.         public Student getItem(int position)
  109.         {
  110.             return mList.get(position);
  111.         }
  112.  
  113.         @Override
  114.         public long getItemId(int position)
  115.         {
  116.             return getItem(position).id;
  117.         }
  118.  
  119.         @Override
  120.         public View getView(int position, View convertView, ViewGroup parent)
  121.         {
  122.             Student student = getItem(position);
  123.  
  124.             if (convertView == null)
  125.                 convertView = getLayoutInflater().inflate(R.layout.item_student_details, null);
  126.  
  127.             if (student != null)
  128.             {
  129.                 TextView studentIDTextView = (TextView)convertView.findViewById(R.id.txtStudentID);
  130.                 TextView studentNameTextView = (TextView)convertView.findViewById(R.id.txtStudentName);
  131.  
  132.                 studentIDTextView.setText(student.studentID);
  133.                 studentNameTextView.setText(student.studentName);
  134.             }
  135.  
  136.             return convertView;
  137.         }
  138.  
  139.         @Override
  140.         public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  141.         {
  142.             Student student = getItem(position);
  143.             Log.d(TAG, String.format("Student Clicked: %s - %s", student.studentID, student.studentName));
  144.  
  145.  
  146.             int id_To_search = position + 1;
  147.             Bundle bundle=new Bundle();
  148.             bundle.putInt("id",id_To_search);
  149.             Intent intent=new Intent(getApplicationContext(), AddStudentActivity.class);
  150.             intent.putExtras(bundle);
  151.             startActivity(intent);
  152.  
  153.  
  154.         }
  155.     }
  156.  
  157.     public class Student
  158.     {
  159.         int id;
  160.         String studentID;
  161.         String studentName;
  162.  
  163.         public Student(int id, String studentID, String studentName)
  164.         {
  165.             this.id = id;
  166.             this.studentID = studentID;
  167.             this.studentName = studentName;
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement