2014-12-06 3 views
-1

SQLite에서 편집 텍스트가 포함 된 양식에서 직원 정보를 가져온 데이터베이스를 만들었으며 다른 활동의 목록보기에 직원의 이름을 표시하려고합니다. 단일 이름을 클릭하면 내 앱이 다음 활동에서 완전한 정보를 표시하게하고, 친절하게도 목록보기를 만들고 데이터베이스에서 이름을 가져 와서 클릭 할 수있게 만들어 emloyee의 전체 정보를 엽니 다. . 여기 SQLite 데이터베이스에서 목록보기에 단일 항목을 표시하는 방법

이 내 InformationDataBase 어댑터 클래스

public class InformationDataBaseAdapter { 

    public static final String ID_COLUMN = "id"; 
    public static final String NAME_COLUMN2 = "name"; 
    public static final String FNAME_COLUMN = "fname"; 
    public static final String EMPLOYEE_DOB = "dob"; 
    public static final String EMPLOYEE_INTEREST = "interest"; 
    public static final String EMPLOYEE_ADDRESS = "address"; 
    public static final String EMPLOYEE_CONTACT = "contact"; 

    static final String DATABASE_NAME = "information.db"; 
    static final String EMPLOYEE_TABLE = "employee"; 
    static final int DATABASE_VERSION = 1; 




    static final String CREATE_EMPLOYEE = "CREATE TABLE " 
      + EMPLOYEE_TABLE + " (" + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + NAME_COLUMN2 + " TEXT NOT NULL, " + FNAME_COLUMN + " TEXT NOT NULL, " + EMPLOYEE_INTEREST + " TEXT, " 
      + EMPLOYEE_DOB + " DATE, " + EMPLOYEE_ADDRESS + " TEXT, " + EMPLOYEE_CONTACT + " INTEGER);"; 


    public SQLiteDatabase db; 
    private final Context context; 
    private DbHelper dbHelper; 

    public InformationDataBaseAdapter(Context _context) { 
     context = _context; 
     dbHelper = new DbHelper(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    public InformationDataBaseAdapter open() throws SQLException { 
     db = dbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     db.close(); 
    } 

    public SQLiteDatabase getDatabaseInstance() { 
     return db; 
    } 

    public void insertEntry(String Name, String FName, String Interest ,String DOB,String Address,String Contact) { 
     ContentValues newValues = new ContentValues(); 
     newValues.put("NAME", Name); 
     newValues.put("Fname", FName); 
     newValues.put("Interest", Interest); 
     newValues.put("DOB", DOB); 
     newValues.put("ADDRESS", Address); 
     newValues.put("CONTACT", Contact); 

     db.insert("EMPLOYEE_TABLE", null, newValues); 

    } 

    public int deleteEntry(String Name) { 

     String where = "NAME=?"; 
     int numberOFEntriesDeleted = db.delete("INFORMATION", where, 
       new String[] { Name }); 
     return numberOFEntriesDeleted; 
    } 

    public String getSinlgeEntry(String Name) { 
     Cursor cursor = db.query("EMPLOYEE", null, " NAME=?", 
       new String[] { Name }, null, null, null); 
     if (cursor.getCount() < 1) // UserName Not Exist 
     { 
      cursor.close(); 
      return "NOT EXIST"; 
     } 
     cursor.moveToFirst(); 
     String info = cursor.getString(cursor.getColumnIndex("INFO")); 
     cursor.close(); 
     return info; 
    } 

    public void updateEntry(String ID, String Name, String FName, String Interest ,String DOB,String Address,String Contact) { 
     ContentValues updatedValues = new ContentValues(); 

     updatedValues.put("NAME", Name); 
     updatedValues.put("Fname", FName); 
     updatedValues.put("Interest", Interest); 
     updatedValues.put("DOB", DOB); 
     updatedValues.put("ADDRESS", Address); 
     updatedValues.put("CONTACT", Contact);; 

     String where = "NAME = ?"; 
     db.update("INFORMTION", updatedValues, where, new String[] { Name }); 
    } 
} 

인 코드 입니다 그리고 이것은

공용 클래스 DbHelper가 {

public DbHelper(Context context, String name, CursorFactory factory, 
     int version) { 
    super(context, name, factory, version); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 
    db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE); 
    db.execSQL(InformationDataBaseAdapter.CREATE_EMPLOYEE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int _oldVersion, int _newVersion) { 

    // TODO Auto-generated method stub  
    db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE"); 
    onCreate(db); 
} 

}

SQLiteOpenHelper를 확장 내 데이터베이스 헬퍼 클래스

그리고 이것은 자바 cl입니다 정보 양식 활동의 엉덩이

공용 클래스 정보 그것은 많은 코드를 필요로의 활동 {

TextView Tv1,Tv2,Tv3,Tv4,Tv5,Tv6,Tv7; 
EditText Etname,EtFname,Etinterest,EtDOB,EtAddress,EtContact; 
Button Bsubmit; 

InformationDataBaseAdapter informationDataBaseAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.information); 

    informationDataBaseAdapter=new InformationDataBaseAdapter(this); 
    informationDataBaseAdapter=informationDataBaseAdapter.open(); 

    Tv1=(TextView)findViewById(R.id.InfoTV1); 
    Tv2=(TextView)findViewById(R.id.InfoTV2); 
    Tv3=(TextView)findViewById(R.id.InfoTV3); 
    Tv4=(TextView)findViewById(R.id.InfoTV4); 
    Tv5=(TextView)findViewById(R.id.InfoTV5); 
    Tv6=(TextView)findViewById(R.id.InfoTV6); 
    Tv7=(TextView)findViewById(R.id.InfoTV7); 
    Etname=(EditText)findViewById(R.id.InfoET1); 
    EtFname=(EditText)findViewById(R.id.InfoET2); 
    Etinterest=(EditText)findViewById(R.id.InfoET3); 
    EtDOB=(EditText)findViewById(R.id.InfoET4); 
    EtAddress=(EditText)findViewById(R.id.InfoET5); 
    EtContact=(EditText)findViewById(R.id.InfoET6); 
    Bsubmit=(Button)findViewById(R.id.bsubmitInfo); 

    Bsubmit.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String Name=Etname.getText().toString(); 
      String FName=EtFname.getText().toString(); 
      String Interest=Etinterest.getText().toString(); 
      String DOB=EtDOB.getText().toString(); 
      String Address=EtAddress.getText().toString(); 
      String Contact=EtContact.getText().toString(); 

      // check if any of the fields are vaccant 
      if(Name.equals("")||FName.equals("")||Interest.equals("")||DOB.equals("")||Address.equals("")||Contact.equals("")) 
      { 

         AlertDialog.Builder alertBuilder=new AlertDialog.Builder(Information.this); 
         alertBuilder.setTitle("Invalid Data"); 
         alertBuilder.setMessage("Please, Enter valid data"); 
         alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 

          } 
         }); 
         alertBuilder.create().show(); 

      } 
      else 
      { 
       // Save the Data in Database 
       informationDataBaseAdapter.insertEntry(Name,FName,Interest,DOB,Address,Contact); 
       Toast.makeText(getApplicationContext(), "Information successfully added", Toast.LENGTH_LONG).show(); 
       Intent i2= new Intent(Information.this,Menu.class); 
       startActivity(i2); 

      } 
     } 
    }); 
} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    informationDataBaseAdapter.close(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 
} 

}

답변

0

을 확장합니다. 나는 당신에게 프로그램 구조에 대한 나의 비전을주고 싶다. 어쨌든 자신이 원하는 것을하기 위해 일해야합니다.

  1. Android에서 데이터베이스 작업은 ContentProvider https://developer.android.com/reference/android/content/ContentProvider.html을 통해 이루어져야합니다. 여기 개발자 가이드 https://developer.android.com/guide/topics/providers/content-provider-basics.html이 있습니다. 제 생각으로 github에서 프로젝트를 다운로드하고 어떻게 작동하는지보십시오. 여기에서 모든 코드를 복제하고 https://github.com/bazli/android_packages_apps_AlarmClock/blob/donut/src/com/android/alarmclock/AlarmProvider.java을 수정하십시오.

  2. 데이터베이스에서 데이터를로드하려면 CursorLoader https://developer.android.com/reference/android/content/CursorLoader.html을 사용하십시오. 로더 정보는 여기 https://developer.android.com/guide/components/loaders.html에서 읽을 수 있습니다.

  3. 사용 SimpleCursorAdapter https://developer.android.com/reference/android/support/v4/widget/SimpleCursorAdapter.html는 데이터베이스에서 데이터를 처리하고있는 ListView에서 그것을 보여

  4. 난 당신이 그래서 당신은 단지 일부에 사용자가 클릭 한 후 새 활동을 시작해야하므로, 그것은 초기 당신이 조각을 사용하는 것 같아요 ListView의 항목

희망 하시겠습니까? 행운을 빕니다!

관련 문제