gpt4 book ai didi

android - 如何让 android 微调器从 tblContacts 中选择

转载 作者:行者123 更新时间:2023-11-30 02:08:24 26 4
gpt4 key购买 nike

我正在使用 jdbc 1.2.8 连接到 MS SQL 数据库,并且我有一个包含几个联系人姓名的 tblContacts。

我现在想做的是让我的微调器 {select * from tblContacts} 并像普通微调器一样填充列表。

我被困在这里,我真的不知道现在该做什么,感谢帮助,谢谢。

此时我有了这个。


 Spinner staticSpinner = (Spinner) findViewById(R.id.spinner);


ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.brew_array,
android.R.layout.simple_spinner_item);


.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


staticSpinner.setAdapter(staticAdapter);

Spinner dynamicSpinner = (Spinner) findViewById(R.id.spinner);

String[] items = new String[] { "Android", "AndroidOld", "AndroidNew" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);

dynamicSpinner.setAdapter(adapter);

dynamicSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

最佳答案

要从数据库生成数据,您需要使用类似于以下内容的内容:

    private void loadSpinnerData() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());

List<String> lables = db.getAllLabels();

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String label = parent.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

在一个单独的类中,您需要使用类似于此的内容来创建数据库处理程序代码:

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "dbname";
private static final String TABLE_LABELS = "table_name";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "column_name";

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

onCreate(db);
}

public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();

// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LABELS;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

cursor.close();
db.close();

// returning lables
return labels;
}
}

关于android - 如何让 android 微调器从 tblContacts 中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com