2014-04-20 93 views
0

我想開發在android系統SQlite的一個小項目獲取數據..Android的ListView控件從SQLite數據庫

我想存儲EN員工信息數據庫.. 的字段名稱,城市,郵件ID和電話號碼..

然後,我想在列表視圖中獲取數據..它會顯示員工名稱.. 當我點擊名稱(另一個意圖)它會顯示該名稱的所有信息。

我已創建DB輔助類的,我可以存儲information..But我無法檢索由name..can任何數據庫幫我?????

public class DBHelper extends SQLiteOpenHelper { 
static final String DATABASE = "contactapp.db"; 
static final int VERSION = 1; 
static final String TABLE = "contact"; 

static final String C_ID = "_id"; 
static final String C_ENAME = "ename"; 
static final String C_city = "city"; 
static final String C_phno = "ph_no"; 


public DBHelper(Context context) { 
    super(context, DATABASE, null, VERSION); 

} 


@Override 
public void onCreate(SQLiteDatabase db) { 


    db.execSQL("CREATE TABLE " + TABLE + " (" + C_ID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, " 
      + C_city + " text, " + C_phno + " integer)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 


    db.execSQL("Drop table " + TABLE); 


    onCreate(db); 
} 

}

和存儲姓名,城市和SQLite數據庫電話號碼management.java ..

public class Management extends Activity { 
EditText tname,tcity,tph_no; 
Button button1; 
DBHelper helper; 
SQLiteDatabase db; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.management); 
    helper = new DBHelper(this); 
    tname = (EditText) findViewById(R.id.tname); 
    tcity = (EditText) findViewById(R.id.tcity); 
    tph_no = (EditText) findViewById(R.id.tph_no); 
    button1=(Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener() { 


    @Override 
    public void onClick(View arg0) { 


     Context context = getApplicationContext(); 
     int s2=Integer.parseInt(tph_no.getText().toString()); 

      ContentValues values = new ContentValues(); 
      values.put(DBHelper.C_ENAME, tname.getText().toString()); 
      values.put(DBHelper.C_city, tcity.getText().toString()); 
      values.put(DBHelper.C_phno, s2); 


      db = helper.getWritableDatabase(); 
      db.insert(DBHelper.TABLE, null, values); 
      db.close(); 

      Toast.makeText(context, "Employee Added Successfully", 
        Toast.LENGTH_LONG).show(); 




    } 
    }); 
} 

}

現在我想要檢索/取只作爲list..when我點擊它命名爲會去另一個意圖,將顯示名稱(再次),城市和電話號碼的名字..

+0

然後發佈一些有用的代碼。 –

+0

份額無論你迄今所做的 –

+0

的,你需要自定義'ListView'和'Adapter'來說,這[鏈接](http://androidtuts4u.blogspot.in/2013/02/android-list-view-using-定製adapter.html)將幫助你.. – Akshay

回答

1

結帳this link

  1. SimpleCursorAdapter 如果您直接與數據庫進行操作,您可以使用SimpleCursorAdapter到 定義數據爲您的ListView。
相關問題