Moeez
Moeez

Reputation: 478

Xamarin how to assign a string to edit text ?

I am new to mobile development. I am working on android in xamarin using visual studio 2015. I am getting a null reference exception when i am assigning a string to an edit text. Bellow is the code

void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        click_Employee = e.Position + 1;
        ICursor c = dbHelper.getSingleEntry(click_Employee);
        c.MoveToFirst();
        name = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_NAME));
        email = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_EMAIL));
        phone = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_PHONE));
        designation =     c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_DESIGNATION));
        dName.Text = name;
        dEmail.Text = email;
        dPhone.Text = phone;
        dDesignation.Text = designation;
    }

The exception i am getting is at point dName.text = name. For better understanding please see the bellow code

ListView list; 
    EditText dName, dEmail, dPhone, dDesignation;
    String name, email, phone, designation;
    SQLiteHelper dbHelper;
    int click_Employee;
    Button upBtn;

 private void initialize()
    {
        list = (ListView)FindViewById(Resource.Id.listView1);
        dName = (EditText)FindViewById(Resource.Id.eName);
        dEmail = (EditText)FindViewById(Resource.Id.eEmail);
        dPhone = (EditText)FindViewById(Resource.Id.ePhone);
        dDesignation = (EditText)FindViewById(Resource.Id.eDesignation);
        upBtn = (Button)FindViewById(Resource.Id.updateEmploy);
    }

Bellow is my axml from where the data is coming in update layout when a record is selected

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:divider="#000"
            android:dividerHeight="1dp" />
        <TextView
            android:id="@+id/eName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />
        <TextView
            android:id="@+id/eEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email" />
        <TextView
            android:id="@+id/ePhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone" />
        <TextView
            android:id="@+id/eDesignation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Designation" />
    </LinearLayout></ScrollView></LinearLayout>

For more understanding please see this link i am using and following it as a sample code

Also i have done the recommends in this link but result is the same

Any help would be appreciated

Upvotes: 0

Views: 1288

Answers (1)

Jake
Jake

Reputation: 356

You've set the items as TextViews in the layout but you're casting them to EditTexts in your class.

<EditText
        android:id="@+id/eName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone" />

You should also do typing rather than casting as shown below.

dName = FindViewById<EditText>(Resource.Id.eName);

Upvotes: 1

Related Questions