2014-12-06 85 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擴展SQLiteOpenHelper {

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); 
} 

}

這是java 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中的項目

希望它有幫助!祝你好運!