help me in this code

+1 naresh gattu · December 30, 2014
package com.example.blooddonation;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class NewDonar extends Activity implements OnClickListener{

EditText editname,editemail,editphnumber,editaddress;
Button editbutton;
Spinner editdistrict,editgroup;
String[] pathdist={"select District","Anantapur","chittor","Kadapa","Kurnool","Nellore","Prakasham","Guntur","Krishna",
"EastGodavari","West Godavari","Vishakaptnam","Vijya Nagaram","Sri Kakulam" };
String[] blgroup={"Select Blood Group","A1 Negative","A1 Positive","A1B Negative","A1B Positive",
"A2 Negative", "A2 Positive", "A2B Negative", "A2B Positive", "B Negative","B Positive",
"B1 Positive", "O Negative", "O Positive"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_donar);
        editname=(EditText)findViewById(R.id.ename);
        editemail=(EditText)findViewById(R.id.eemail);
        editphnumber=(EditText)findViewById(R.id.ephnumber);
        editaddress=(EditText)findViewById(R.id.eaddress);
        
        editbutton=(Button)findViewById(R.id.eadddonar);
        editbutton.setOnClickListener(this);
        
        editdistrict=(Spinner)findViewById(R.id.edistrict);
    ArrayAdapter<String> adapterdist=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,pathdist);
        adapterdist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        editdistrict.setAdapter(adapterdist);
        editdistrict.setOnItemSelectedListener((OnItemSelectedListener) this);
        
        editgroup=(Spinner)findViewById(R.id.egroup);
        ArrayAdapter<String> adaptergrop=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,blgroup);
        adaptergrop.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        editgroup.setAdapter(adaptergrop);
        editgroup.setOnItemSelectedListener((OnItemSelectedListener) this);
       
    }
    
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.eadddonar:

boolean didItWork=true;
try{
String donarname=editname.getText().toString();
String email=editemail.getText().toString();
String phno=editphnumber.getText().toString();
String addr=editaddress.getText().toString();
String district = editdistrict.getSelectedItem().toString().trim();
String bloodgroup = editgroup.getSelectedItem().toString().trim();



DonationHelper entry=new DonationHelper(NewDonar.this);
entry.open();
entry.createEntry(donarname,email,phno,addr,district,bloodgroup);
entry.close();
}
catch(Exception e){
didItWork=false;
}
finally{
if(didItWork){
Dialog d=new Dialog(this);
d.setTitle("HEY");
TextView tv=new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
}

}

}







package com.example.blooddonation;


import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DonationHelper {

public static final String KEY_NAME="Donar_Name";
public static final String KEY_EMAIL="email";
public static final String KEY_PH="phnumber";
public static final String KEY_ADD="address";
public static final String KEY_DIST="district";
public static final String KEY_GROUP="bloodgroup";


private static final String DATABASE_NAME="BloodDonationDb";
public static final String DATABASE_TABLE="DonationTable";
public static final int DATABASE_VERSION=1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
super(context,DATABASE_NAME, null,DATABASE_VERSION);


}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_NAME + " TEXT PRIMARY KEY, " + 
KEY_EMAIL + " TEXT NOT NULL, " + 
KEY_PH + " TEXT NOT NULL, " + 
 
KEY_DIST + " VARCHAR, " +

KEY_GROUP + " VARCHAR, " + 
KEY_ADD + " TEXT NOT NULL);"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

db.execSQL("DROP IF EXISTS"+ DATABASE_TABLE);
onCreate(db);
}

}
public DonationHelper(Context c){
ourContext=c;
}
public DonationHelper open()throws SQLException{
ourHelper=new DbHelper(ourContext);
ourDatabase =ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}

public long createEntry(String donarname, String email, String phno,
String addr, String district, String bloodgroup) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_EMAIL,donarname);
cv.put(KEY_EMAIL,email);
cv.put(KEY_PH,phno);
cv.put(KEY_ADD,addr);
cv.put(KEY_DIST,district);
cv.put(KEY_GROUP,bloodgroup);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
}




some one help me in passing spinner query to the execSQL query ,or any other way which suite for this code and also help me in to code for this program to generate view say based on selecting blood group,and district the database details say name ,email id ,ph number,blood group,

Post a Reply

Replies

Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · December 30, 2014
please place your codes in the code tag. will be easier to read and understand.
  • 1

Java / Android Development

107,228 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator