jeudi 23 avril 2015

Android: How to set the value of single table field to a String variable?

I'm new to Android programming. I'm making an app that takes the value of a single field Phone on a specific row (where id=1) from a table my_table and assigns it to a String number. And then print that string. But the app keeps crashing after taking the data.

Where do I move from here? Where am I wrong? What am I missing?

Below is the code.

MainActivity.java

public class MainActivity extends Activity {

SQLiteDatabase db;
EditText pin, phone, email;

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

    ImageButton btn=(ImageButton)findViewById(R.id.btn);
    pin = (EditText)findViewById(R.id.pin);
    phone = (EditText)findViewById(R.id.phone);
    email = (EditText)findViewById(R.id.email);

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            SaveData();
            Toast.makeText(MainActivity.this,"Info saved!", Toast.LENGTH_SHORT).show();
        }

    });

}

public void SaveData(){

    db = this.openOrCreateDatabase("my_database",MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS my_table(id INTEGER PRIMARY KEY AUTOINCREMENT, Pin varchar,"
            + " Phone varchar, EMail varchar);");
    String querry = "INSERT INTO my_table (Pin,Phone,EMail) VALUES ('"+pin.getText().toString()+"','"+phone.getText().toString()+"','"+email.getText().toString()+"')";
    db.execSQL(querry);


    Display();
}

void Display(){
    Intent Intent=new Intent(MainActivity.this,Output.class);
    startActivity(Intent);
}
}

Output.java

public class Output extends Activity{

TextView phone;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.output);

    db = this.openOrCreateDatabase("my_database",MODE_PRIVATE, null);
    Cursor csr =  db.rawQuery( "SELECT Phone FROM my_table WHERE id = 1", null); 
    String number = csr.toString();
    db.execSQL(number);

    phone.setText(number);

}
}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.ali.readonefield"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="22" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Output"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

Aucun commentaire:

Enregistrer un commentaire