我的要求是创建一个员工管理系统。我有名字,手机号码1& 2和家。我可以将它们添加到数据库中。当我按下"显示他们"我想显示所有员工的姓名。按钮,当我点击名称时,它将转到另一个意图并显示移动号码1& 2和家。
我创建了: - Contact.java
public class Contact {
//private variables
int id;
String name;
String mobile1;
String mobile2;
String home;
String about;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name, String mobile1,String mobile2,String home,String about){
this.id = id;
this.name = name;
this.mobile1 = mobile1;
this.mobile2 = mobile2;
this.home = home;
this.about = about;
}
// constructor
public Contact(String name, String mobile1,String mobile2,String home,String about){
this.name = name;
this.mobile1 = mobile1;
this.mobile2 = mobile2;
this.home = home;
this.about = about;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getMobile1() {
return mobile1;
}
public String getMobile2() {
return mobile2;
}
public String getHome() {
return home;
}
public String getAbout() {
return about;
}
public void setId(int id) {
this.id = id;
}`public void setName(String name) {
this.name = name;
}
public void setMobile1(String mobile1) {
this.mobile1 = mobile1;
}
public void setMobile2(String mobile2) {
this.mobile2 = mobile2;
}
public void setHome(String home) {
this.home = home;
}
public void setAbout(String about) {
this.about = about;
}}`
DatabaseHandler.java: -
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
static final String DATABASE = "base2";
static final int VERSION = 1;
static final String TABLE = "emp";
static final String C_ID = "_id";
static final String C_Name= "name";
static final String C_Mobile1 = "mobile1";
static final String C_Mobile2 = "mobile2";
static final String C_Home= "home";
static final String C_About= "about";
public DatabaseHandler(Context context) {
super(context,DATABASE, null, VERSION);
//3rd argument to be passed is CursorFactory instance
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Create Employee table with following fields
// _ID, ENAME, DESIGNATION and SALARY
db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_Name + " text, "
+ C_Mobile1 + " integer, " + C_Mobile2 + " integer, " + C_Home + " text," + C_About + " text )");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
// Create tables again
onCreate(db);
}
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_Name, contact.getName()); // Contact Name
values.put(C_Mobile1, contact.getMobile1()); // Contact Phone
values.put(C_Mobile2, contact.getMobile2());
values.put(C_Home, contact.getHome());
values.put(C_About, contact.getAbout());
// Inserting Row
db.insert(TABLE, null, values);
//2nd argument is String containing nullColumnHack
db.close(); // Closing database connection
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
// contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setMobile1(cursor.getString(2));
contact.setMobile2(cursor.getString(3));
contact.setHome(cursor.getString(4));
contact.setAbout(cursor.getString(5));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_Name, contact.getName());
values.put(C_Mobile1, contact.getMobile1());
values.put(C_Mobile2, contact.getMobile2());
values.put(C_Home, contact.getHome());
values.put(C_About, contact.getAbout());
// updating row
return db.update(TABLE, values, C_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE, C_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}}
这里我可以添加数据,但问题是按员工姓名获取数据。从名称我想显示与名称对应的所有细节。我创建了以下内容,它只显示了一个名称。
List.java: -
public class List1 extends Activity {
String selected_ID = "";
DatabaseHandler helper;
SQLiteDatabase db;
SimpleCursorAdapter adapter;
ListView lv;
Cursor c;
ArrayList<String> list;
String s1,s2,s3,s4,s5,s6;
List<Contact> contacts = helper.getAllContacts();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.llist);
helper = new DatabaseHandler(this);
lv=(ListView) findViewById(R.id.listView1);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
list = new ArrayList<String>();
for (Contact cn : contacts) {
s2=cn.getName();
s3=cn.getMobile1();
s4=cn.getMobile2();
s5=cn.getHome();
s6=cn.getAbout();
}
Bundle bundle = new Bundle();
bundle.putString("name",s2 );
bundle.putString("mob",s3 );
bundle.putString("mob1",s4 );
bundle.putString("home",s5 );
bundle.putString("about",s6 );
Intent intent = new Intent(getApplicationContext(), Details.class);
intent.putExtras(bundle);
startActivity(intent);
}
});fetchData();}
private void fetchData() {`private void fetchData() {
db = helper.getReadableDatabase();
c = db.query(DatabaseHandler.TABLE, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.listname,
c,
new String[] { DatabaseHandler.C_Name,DatabaseHandler.
C_Mobile1,DatabaseHandler.
C_Mobile2,DatabaseHandler.
C_Home,DatabaseHandler.C_About},
new int[] { R.id.name },0);lv.setAdapter(adapter);}`
和Details.java是: -
public class Details extends Activity{
public Bundle getBundle = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
TextView txt2 = (TextView) findViewById(R.id.tx2);
TextView txt3 = (TextView) findViewById(R.id.tx3);
TextView txt4 = (TextView) findViewById(R.id.tx4);
TextView txt5 = (TextView) findViewById(R.id.tx5);
TextView txt6 = (TextView) findViewById(R.id.tx6);
getBundle = this.getIntent().getExtras();
String n2 = getBundle.getString("name");
String n3 = getBundle.getString("mob");
String n4 = getBundle.getString("mob1");
String n5 = getBundle.getString("home");
String n6 = getBundle.getString("about");
txt2.setText(n2);
txt3.setText(n3);
txt4.setText(n4);
txt5.setText(n5);
txt6.setText(n6);
}}
问题出在哪里?有没有人有任何建议?