这里我将值和部门的值添加到数据库中,部门值将显示在spinner中。现在我想根据动态微调器中的选定值从sqlite数据库填充listview。这是我的代码
Home.java
public class home extends Activity implements OnItemSelectedListener{
Button vstudent;
ListView lv;
DBhelper dbhelper;
EditText et;
TextView memID_tv, memName_tv,memdep_tv;
Spinner spinner;
ArrayList array_list;
String cls;
private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> user_name = new ArrayList<String>();
private ArrayList<String> user_department = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// create a instance of SQLite Database
lv = (ListView) findViewById(R.id.listView1);
spinner=(Spinner)findViewById(R.id.spinner1);
dbhelper = new DBhelper(this);
loadSpinnerData();
displayData();
// Attach The Data From DataBase Into ListView Using Crusor Adapter
}
private void displayData() {
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM "
+ DBhelper.CONTACTS_TABLE_NAME, null);
userId.clear();
user_name.clear();
user_department.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_ID)));
user_name.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_NAME)));
user_department.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_DEPARTMENT)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(home.this,userId, user_name, user_department);
lv.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
mCursor.close();
}
private void loadSpinnerData() {
// database handler
// Spinner Drop down elements
List<String> lables = dbhelper.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), label, Toast.LENGTH_LONG).show();
// Showing selected spinner item
System.out.print(label);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}}
这是我的dbhelper类
public class DBhelper extends SQLiteOpenHelper {
// TABLE INFORMATTION
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_DEPARTMENT = "department";
private HashMap hp;
public DBhelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,department text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact(String name, String department)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("department", department);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public Cursor readData() {
SQLiteDatabase db = this.getReadableDatabase();
String[] allColumns = new String[] { DBhelper.CONTACTS_COLUMN_ID,
DBhelper.CONTACTS_COLUMN_NAME,DBhelper.CONTACTS_COLUMN_DEPARTMENT };
Cursor c = db.query(DBhelper.CONTACTS_TABLE_NAME, allColumns, null,
null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String department)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("department", department);
db.update("contacts", contentValues, "id = ? ", new String[] {
Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + CONTACTS_TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(2));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
//Help me to find appropriate codes for this.thank you in advance.
答案 0 :(得分:0)
它非常适用于sqlite数据库项目,因为我们将根据spinner中的选定值获得动态列表视图。这里我只是将函数displaydata中的所有代码导出到微调器的onitemslected函数中,并且它工作得很好。这里。是我的答案代码
private void loadSpinnerData() {
// database handler
// Spinner Drop down elements
List<String> lables = dbhelper.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), label, Toast.LENGTH_LONG).show();
// Showing selected spinner item
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor mCursor=db.rawQuery("SELECT * FROM contacts WHERE department='"+label+"'", null);
userId.clear();
user_name.clear();
user_department.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_ID)));
user_name.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_NAME)));
user_department.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_DEPARTMENT)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(home.this,userId, user_name, user_department);
lv.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
mCursor.close();
System.out.print(label);
}