package com.afifzafri.studentsdb;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import org.w3c.dom.Text;

public class SearchData extends AppCompatActivity {

    // initialize object
    DBHelper mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_data);

        mydb = new DBHelper(this);

        final EditText sid = (EditText)findViewById(R.id.editSearch);
        ImageButton btnSearch = (ImageButton)findViewById(R.id.btnSearch);
        final TextView resID = (TextView)findViewById(R.id.resStudID);
        final TextView resName = (TextView)findViewById(R.id.resName);
        final TextView resIC = (TextView)findViewById(R.id.resIC);
        final TextView resDOB = (TextView)findViewById(R.id.resDOB);
        final TextView resAddress = (TextView)findViewById(R.id.resAddress);
        final TextView resProgram = (TextView)findViewById(R.id.resProgram);
        final TextView resPhone = (TextView)findViewById(R.id.resPhone);
        final TextView resEmail = (TextView)findViewById(R.id.resEmail);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studid = sid.getText().toString();

                // access cursor data
                Cursor cursor = mydb.searchData(studid);
                cursor.moveToFirst();
                String id = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ID));
                String name = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME));
                String ic = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_IC));
                String dob = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DOB));
                String address = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ADDRESS));
                String program = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PROGRAM));
                String phone = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PHONE));
                String email = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_EMAIL));

                // close cursor
                if (!cursor.isClosed())  {
                    cursor.close();
                }

                //append all the data
                resID.setText(id);
                resName.setText(name);
                resIC.setText(ic);
                resDOB.setText(dob);
                resAddress.setText(address);
                resProgram.setText(program);
                resPhone.setText(phone);
                resEmail.setText(email);
            }
        });
    }
}

@@ -1664,24 +1664,255 @@
 eToFirst();%0A
+%0A                // check if cursor return data or not (data found in db or not)%0A                if(cursor.getCount() %3C 1)%0A                %7B%0A                    // empty%0A                %7D%0A                else%0A                %7B%0A    
             
@@ -2000,24 +2000,28 @@
             
+    
 String name 
@@ -2093,32 +2093,36 @@
                 
+    
 String ic = curs
@@ -2190,24 +2190,28 @@
             
+ 
+   
 String dob =
@@ -2269,24 +2269,28 @@
 LUMN_DOB));%0A
+    
             
@@ -2384,32 +2384,36 @@
                 
+ 
+   
 String program =
@@ -2487,32 +2487,36 @@
                 
+    
 String phone = c
@@ -2590,24 +2590,28 @@
             
+  
+  
 String email
@@ -2690,134 +2690,8 @@
     
-       
- // close cursor%0A                if (!cursor.isClosed())  %7B%0A                    cursor.close();%0A                %7D%0A%0A    
     
@@ -2716,24 +2716,28 @@
 ll the data%0A
+    
             
@@ -2775,16 +2775,20 @@
         
+    
 resName.
@@ -2814,24 +2814,28 @@
             
+    
 resIC.setTex
@@ -2857,16 +2857,20 @@
         
+ 
+   
 resDOB.s
@@ -2882,16 +2882,20 @@
 t(dob);%0A
+    
         
@@ -2943,24 +2943,28 @@
             
+  
+  
 resProgram.s
@@ -2992,24 +2992,28 @@
             
+    
 resPhone.set
@@ -3041,16 +3041,20 @@
         
+   
+ 
 resEmail
@@ -3070,16 +3070,164 @@
 email);%0A
+                %7D%0A%0A                // close cursor%0A                if (!cursor.isClosed())  %7B%0A                    cursor.close();%0A                %7D%0A
         

check if data found or not