- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含两列 (id,name) 的表。 Toast 应在从微调器中选择 id 时显示来自 sqlite 的“名称”详细信息。
例如:Sqlite 表 1,899,Chris 和 2,890,David。
每当我从微调器中选择值 899 时,Toast 应显示 Chris,如果我选择微调器 890,则 Toast 应显示 David。需要建议。
代码:SpinnerEx4Activity.Java
package com.bar.example.androidspinnerexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleCursorAdapter;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.content.Intent;
import java.util.HashMap;
import java.util.List;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.EditText;
import java.util.LinkedList;
import android.view.inputmethod.InputMethodManager;
public class SpinnerEx4Activity extends Activity implements
AdapterView.OnItemSelectedListener {
Spinner s1,s2;
Button btnAdd;
EditText inputLabel;
DatabaseHandler dbhndlr; //<<<<< Single instance for Database handler
Cursor spinner1csr;
Cursor spinner2csr; //<<<<< Cursor for spinner (close in onDestroy)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
btnAdd = (Button) findViewById(R.id.btn_add);
s1.setOnItemSelectedListener(this);
dbhndlr = new DatabaseHandler(this); //<<<< Instantiate Databasehandler
//loadSpinnerData(); //<<<< commented out
altLoadSpinnerData();
altLoadSpinnerData1();//<<<< Load via cursor
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String label = inputLabel.getText().toString();
if (label.trim().length() > 0) {
// database handler commeneted out, use dbhndlr instance instead
// inserting new label into database
dbhndlr.insertLabel(label);
// making input filed text to blank
inputLabel.setText("");
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
// loading spinner with newly added data
//loadSpinnerData(); //<<<< commeneted out
altLoadSpinnerData();
altLoadSpinnerData1();
} else {
Toast.makeText(getApplicationContext(), "Please enter label name",
Toast.LENGTH_SHORT).show();
}
}
});
}
// New method to utilise Cursor adapter
private void altLoadSpinnerData() {
spinner1csr = dbhndlr.getAllLabelsAsCursor();
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, // Layout to show 2 items
spinner1csr, // Cursor
new String[]{DatabaseHandler.KEY_NAME},
new int[]{android.R.id.text1},// Views into which data is shown
0
);
s1.setAdapter(sca);
}
private void altLoadSpinnerData1 () {
// get the cursor
spinner2csr = dbhndlr.getAllLabelsAsCursor();
// Instantiaie Simple Cursor Adapter
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, // Layout to show 2 items
spinner2csr, // Cursor
new String[]{DatabaseHandler.KEY_ID}, // Source data
new int[]{android.R.id.text2}, // Views into which data is shown
0
);
s2.setAdapter(sca);
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " " +
spinner2csr.getString(
spinner2csr.getColumnIndex(DatabaseHandler.KEY_ID)),
Toast.LENGTH_SHORT
).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}}
和Databasehandler.java
package com.bar.example.androidspinnerexample;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "spinnerExample";
// Labels table name
private static final String TABLE_LABELS = "labels";
// Labels Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
// Create tables again
onCreate(db);
}
/**
* Inserting new lable into lables table
* */
public void insertLabel(String label){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, label);
// Inserting Row
db.insert(TABLE_LABELS, null, values);
db.close(); // Closing database connection
}
/**
* Getting all labels
* returns list of labels
* */
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);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
}
而activity_spinner_ex4.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="@string/lblAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
/>
<!-- Select Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="@string/lblSubAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
<EditText android:id="@+id/input_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"/>
<!-- Add Button -->
<Button android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Label"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"/>
</LinearLayout>
将微调器和复选框值插入到另一个表中。我能够插入所有数据。但是所有复选框文本都在保存而不是选定的文本。你能帮忙吗。
主要 Activity 。
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//String label = inputLabel.getText().toString();
String SaveString="No";
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
String message1= ((Cursor) s1.getSelectedItem()).getString(2);
String message2= ((Cursor) s2.getSelectedItem()).getString(1);
String message = inputLabel.getText().toString();
// String message1 = s1.getSelectedItem().toString();
// String message2 = s2.getSelectedItem().toString();
String message10 = s3.getSelectedItem().toString();
String message4 = ck1.getText().toString();
String message5 = ck2.getText().toString();
String message6 = ck3.getText().toString();
String message7 = ck4.getText().toString();
String message9 = ck6.getText().toString();
String message3 = ck7.getText().toString();
String message8 = ck8.getText().toString();
db.insertLabel(message1,message2,message5,message6,message7,message9,message3,message4,message8,message10);
if (ck1.isChecked())
{ SaveString="Yes";
}
else
{ SaveString="No";
}
if (ck2.isChecked())
{ SaveString="Yes";
}
else
{ SaveString="No";
}
if (message.trim().length() > 0) {
// database handler commeneted out, use dbhndlr instance instead
// inserting new label into database
// making input filed text to blank
inputLabel.setText("");
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
// loading spinner with newly added data
spinner1csr = dbhndlr.getAllLabelsAsCursor();
spinner2csr = dbhndlr.getByRowid(spinner1_selected);
sca.swapCursor(spinner1csr);
sca2.swapCursor(spinner2csr);
} else {
Toast.makeText(getApplicationContext(), "Please enter label name",
Toast.LENGTH_SHORT).show();
}
}
});
}
数据库。
public void insertLabel(String message1, String message2,String message3,String message4,String message5,String message6,String message7,String message8,String message9,String message10){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_1, message1);
values.put(KEY_2, message2);
values.put(KEY_10,message10);
values.put(KEY_3,message3);
values.put(KEY_4,message4);
values.put(KEY_5,message5);
values.put(KEY_6,message6);
values.put(KEY_7,message7);
values.put(KEY_9,message9);
values.put(KEY_8,message8);
// Inserting Row
db.insert(TABLE_LABELS2, null, values);
db.close(); // Closing database connection
}
最佳答案
public class SpinnerEx4Activity extends Activity {
Spinner s1,s2;
Button btnAdd;
EditText inputLabel;
DatabaseHandler dbhndlr;
Cursor spinner1csr, spinner2csr;
SimpleCursorAdapter sca, sca2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
btnAdd = (Button) findViewById(R.id.btn_add);
inputLabel = (EditText) findViewById(R.id.input_label);
dbhndlr = new DatabaseHandler(this);
// If no data in database then load data for testing purposes only
if (DatabaseUtils.queryNumEntries(
dbhndlr.getWritableDatabase(),
DatabaseHandler.TABLE_LABELS) < 1)
{
dbhndlr.insertlabel("899","Chris");
dbhndlr.insertlabel("890","David");
}
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsAsCursor();
spinner2csr = dbhndlr.getAllLabelsAsCursor();
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,spinner1csr,
new String[]{DatabaseHandler.KEY_ID},
new int[]{android.R.id.text1},
0
);
//Steup Adapter for Spinner2
sca2 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner2csr,
new String[]{DatabaseHandler.KEY_NAME},
new int[]{android.R.id.text1},
0
);
// Set the Adapters to the Spinners
s1.setAdapter(sca);
s2.setAdapter(sca2);
// Set Spinner1 OnSelectedItemListener
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " - " +
spinner1csr.getString(
spinner1csr.getColumnIndex(DatabaseHandler.KEY_NAME)),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Set Spinner2 OnSelectedItemListener
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " " +
spinner2csr.getString(
spinner2csr.getColumnIndex(DatabaseHandler.KEY_ID)),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String label = inputLabel.getText().toString();
if (label.trim().length() > 0) {
// database handler commeneted out, use dbhndlr instance instead
// inserting new label into database
dbhndlr.insertLabel(label);
// making input filed text to blank
inputLabel.setText("");
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
// loading spinner with newly added data
//loadSpinnerData(); //<<<< commeneted out
spinner1csr = dbhndlr.getAllLabelsAsCursor();
spinner2csr = dbhndlr.getAllLabelsAsCursor();
sca.swapCursor(spinner1csr);
sca2.swapCursor(spinner2csr);
//altLoadSpinnerData();
//altLoadSpinner2Data();
} else {
Toast.makeText(getApplicationContext(), "Please enter label name",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
here i am talking now about select spinner s1 value(ex:123) then spinner s2 should different value(Mike Trae) as per table.
即微调器应该排除另一个微调器中的选定值(微调器是相互依赖的)
public class SpinnerEx4Activity extends Activity {
Spinner s1,s2;
Button btnAdd;
EditText inputLabel;
DatabaseHandler dbhndlr;
Cursor spinner1csr, spinner2csr;
SimpleCursorAdapter sca, sca2;
long spinner1_selected = 0, spinner2_selected = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
btnAdd = (Button) findViewById(R.id.btn_add);
inputLabel = (EditText) findViewById(R.id.input_label);
dbhndlr = new DatabaseHandler(this);
// If no data in database then load data for testing purposes only
if (DatabaseUtils.queryNumEntries(
dbhndlr.getWritableDatabase(),
DatabaseHandler.TABLE_LABELS) < 1)
{
dbhndlr.insertlabel("899","Chris");
dbhndlr.insertlabel("890","David");
}
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsExceptedSelected(spinner2_selected);
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,spinner1csr,
new String[]{DatabaseHandler.KEY_NAME},
new int[]{android.R.id.text1},
0
);
// Set the Adapters to the Spinners
s1.setAdapter(sca);
// Set Spinner1 OnSelectedItemListener
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " - " +
spinner1csr.getString(
spinner1csr.getColumnIndex(DatabaseHandler.KEY_NAME)) +
" - " + spinner1csr.getString(spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID))
,
Toast.LENGTH_SHORT).show();
spinner1_selected = id;
spinner2csr = dbhndlr.getAllLabelsExceptedSelected(spinner1_selected);
sca2.swapCursor(spinner2csr);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Steup Adapter for Spinner2
spinner2csr = dbhndlr.getAllLabelsExceptedSelected(spinner1_selected);
sca2 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner2csr,
new String[]{DatabaseHandler.KEY_NAME},
new int[]{android.R.id.text1},
0
);
s2.setAdapter(sca2);
// Set Spinner2 OnSelectedItemListener
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " - " +
spinner2csr.getString(
spinner2csr.getColumnIndex(DatabaseHandler.KEY_NAME)) +
" - " + spinner2csr.getString(spinner2csr.getColumnIndex(DatabaseHandler.KEY_ID)),
Toast.LENGTH_SHORT).show();
spinner2_selected = id;
spinner1csr = dbhndlr.getAllLabelsExceptedSelected(spinner2_selected);
sca.swapCursor(spinner1csr);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String label = inputLabel.getText().toString();
if (label.trim().length() > 0) {
// database handler commeneted out, use dbhndlr instance instead
// inserting new label into database
dbhndlr.insertLabel(label);
// making input filed text to blank
inputLabel.setText("");
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
// loading spinner with newly added data
//loadSpinnerData(); //<<<< commeneted out
spinner1csr = dbhndlr.getAllLabelsExceptedSelected(spinner2_selected);
spinner2csr = dbhndlr.getAllLabelsExceptedSelected(spinner1_selected);
sca.swapCursor(spinner1csr);
sca2.swapCursor(spinner2csr);
//altLoadSpinnerData();
//altLoadSpinner2Data();
} else {
Toast.makeText(getApplicationContext(), "Please enter label name",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onDestroy() {
spinner1csr.close();
spinner2csr.close();
super.onDestroy();
}
}
public class DatabaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
public static final String DATABASE_NAME = "spinnerExample";
// Labels table name
public static final String TABLE_LABELS = "labels"; //<<<< Made public
// Labels Table Columns names
public static final String KEY_ID = "id"; //<<<< Made public
public static final String KEY_NAME = "name"; //<<<< made public
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
// Create tables again
onCreate(db);
}
/**
* Inserting new lable into lables table
* */
public void insertLabel(String label){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, label);
// Inserting Row
db.insert(TABLE_LABELS, null, values);
db.close(); // Closing database connection
}
// Added for adding new data
public void insertlabel(String id, String label) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_ID,id);
cv.put(KEY_NAME,label);
db.insert(TABLE_LABELS,null,cv);
db.close();
}
// Added to get Cursor for Simple CursorAdapter
public Cursor getAllLabelsAsCursor() {
String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
return this.getWritableDatabase().query(TABLE_LABELS,columns,null,null,null,null,null);
}
public Cursor getAllLabelsExceptedSelected(long selected) {
String[] columns = new String[]{"rowid AS _id, *"};
String whereclause = "rowid <> ?";
String[] whereargs = new String[]{String.valueOf(selected)};
return this.getWritableDatabase().query(TABLE_LABELS,
columns,
whereclause,
whereargs,
null,
null,
null
);
}
}
But i want it one-way only Spinner1 selection first column(Database) should show in spinner 2(Second Column)database.
No need from spinner 2 to spinner.
1.Also note that my table having 1000 datas*(rows)*.
ex: Database:
{ dbhndlr.insertlabel("9001234","Chris")
dbhndlr.insertlabel("9001235","Cdedd");
dbhndlr.insertlabel("9003457","Dcdtt");
dbhndlr.insertlabel("9001231","Chrdis");
dbhndlr.insertlabel("9003451","Ddavid");}ex: If spinner 1 select 9001231 then spinner 2 should show Chrdis and if spinner 1 selected 9001234 then spinner2 should show Chris.
1) 添加新方法 getByRowid
到 Databasehandler.java :-
public class DatabaseHandler extends SQLiteOpenHelper {
// Database Version
public static final int DATABASE_VERSION = 1;
// Database Name
public static final String DATABASE_NAME = "spinnerExample";
// Labels table name
public static final String TABLE_LABELS = "labels"; //<<<< Made public
// Labels Table Columns names
public static final String KEY_ID = "id"; //<<<< Made public
public static final String KEY_NAME = "name"; //<<<< made public
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
// Create tables again
onCreate(db);
}
/**
* Inserting new lable into lables table
* */
public void insertLabel(String label){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, label);
// Inserting Row
db.insert(TABLE_LABELS, null, values);
db.close(); // Closing database connection
}
// Added for adding new data
public void insertlabel(String id, String label) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_ID,id);
cv.put(KEY_NAME,label);
db.insert(TABLE_LABELS,null,cv);
db.close();
}
// Added to get Cursor for Simple CursorAdapter
public Cursor getAllLabelsAsCursor() {
String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
return this.getWritableDatabase().query(TABLE_LABELS,columns,null,null,null,null,null);
}
public Cursor getAllLabelsExceptedSelected(long selected) {
String[] columns = new String[]{"rowid AS _id, *"};
String whereclause = "rowid <> ?";
String[] whereargs = new String[]{String.valueOf(selected)};
return this.getWritableDatabase().query(TABLE_LABELS,
columns,
whereclause,
whereargs,
null,
null,
null
);
}
public Cursor getByRowid(long id) {
String[] columns = new String[]{"rowid AS _id, *"};
return this.getWritableDatabase().query(
TABLE_LABELS,
columns,
"rowid=?",
new String[]{String.valueOf(id)},
null,null,null
);
}
}
修改后的SpinnerEx4Acitivity.java:-
public class MainActivity extends Activity {
Spinner s1,s2;
Button btnAdd;
EditText inputLabel;
DatabaseHandler dbhndlr;
Cursor spinner1csr, spinner2csr;
SimpleCursorAdapter sca, sca2;
long spinner1_selected = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
btnAdd = (Button) findViewById(R.id.btn_add);
inputLabel = (EditText) findViewById(R.id.input_label);
dbhndlr = new DatabaseHandler(this);
// If no data in database then load data for testing purposes only
if (DatabaseUtils.queryNumEntries(
dbhndlr.getWritableDatabase(),
DatabaseHandler.TABLE_LABELS) < 1)
{
dbhndlr.insertlabel("899","Chris");
dbhndlr.insertlabel("890","David");
}
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsAsCursor();
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,spinner1csr,
new String[]{DatabaseHandler.KEY_ID},
new int[]{android.R.id.text1},
0
);
// Set the Adapters to the Spinners
s1.setAdapter(sca);
// Set Spinner1 OnSelectedItemListener
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " - " +
spinner1csr.getString(
spinner1csr.getColumnIndex(DatabaseHandler.KEY_NAME)) +
" - " + spinner1csr.getString(spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID))
,
Toast.LENGTH_SHORT).show();
spinner1_selected = id;
spinner2csr = dbhndlr.getByRowid(spinner1_selected);
sca2.swapCursor(spinner2csr);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Steup Adapter for Spinner2
spinner2csr = dbhndlr.getByRowid(spinner1_selected);
sca2 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner2csr,
new String[]{DatabaseHandler.KEY_NAME},
new int[]{android.R.id.text1},
0
);
s2.setAdapter(sca2);
// Set Spinner2 OnSelectedItemListener
/* Not needed
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"You Selected: " + id + " - " +
spinner2csr.getString(
spinner2csr.getColumnIndex(DatabaseHandler.KEY_NAME)) +
" - " + spinner2csr.getString(spinner2csr.getColumnIndex(DatabaseHandler.KEY_ID)),
Toast.LENGTH_SHORT).show();
spinner2_selected = id;
spinner1csr = dbhndlr.getAllLabelsExceptedSelected(spinner2_selected);
sca.swapCursor(spinner1csr);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
*/
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String label = inputLabel.getText().toString();
if (label.trim().length() > 0) {
// database handler commeneted out, use dbhndlr instance instead
// inserting new label into database
dbhndlr.insertLabel(label);
// making input filed text to blank
inputLabel.setText("");
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
// loading spinner with newly added data
spinner1csr = dbhndlr.getAllLabelsAsCursor();
spinner2csr = dbhndlr.getByRowid(spinner1_selected);
sca.swapCursor(spinner1csr);
sca2.swapCursor(spinner2csr);
} else {
Toast.makeText(getApplicationContext(), "Please enter label name",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onDestroy() {
spinner1csr.close();
spinner2csr.close();
super.onDestroy();
}
}
关于android - Second Spinner 结果基于来自 Sqlite(Magic Code)的 First Spinner 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48304909/
目前,我正在为网络开发类(class)做作业。 这些是说明:第一行和首字下沉样式Jakob 希望文章的第一行以小写大写字母显示。转到 First Line and Drop Cap Styles 部分
.first() 方法是在 jQuery 1.4 中添加的。 :first 选择器自 1.0 以来就已存在。 来自文档: :first The :first pseudo-class is equiv
我正在审查现有的 ASP.NET MVC (5.2.3) EF (6.1.3) 项目。 该项目使用 ASP.NET Identity,我检查了 web.config 中的 2 个连接字符串,一个用于
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
for(auto it = M.begin(); it!=M.end();it++) { coutfirstsecondsecond == 1) return it->firs
我试图从第二个循环中获取循环的第一项。 我知道我得到了这样的@key @../key 但@first 似乎不像@../first 那样工作 有什么想法吗? 问候 最佳答案 首先,无论是否在嵌套 blo
var tab1 = $('.tabs a:first-child').attr('href'); alert(tab1); .. 尽管同一页面上有两个 div.switch,但仅匹配一个。第二个位于
我想知道如何将节点*变量 NODE 分配给结构内的数据? struct node { int info; struct node *link; }; typedef struct nod
我有两个段落包含在一个 div 中。我想让第一段的文字变大一点,但使用 :first-child 并不能像我所说的那样工作。看不出有什么问题。
我有一个 ul li 列表 Parent child1 child2
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
这有什么区别吗: myList.Where(item => item == 0).First(); 还有这个: myList.First(item => item == 0); 后者对我来说更有意义,
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
当我遇到一个奇怪的情况时,我正在研究 CSS 选择器。 如果我使用 :first-child 伪元素,我需要在它前面加上一个空格才能工作,否则它将无法工作。然而 :first-letter 伪元素的情
请考虑以下字符串数组: let strings = ["str1", "str2", "str10", "str20"] 假设需要获取包含 5 个字符的第一个元素 (String),我可以使用 fil
让我们假设我们要开始新项目 - 包含一些业务逻辑的应用程序、ASP.NET 上的用户界面、WPF 或两者。我们想使用 ORM 或 DAL 代码生成器并在 .NET 类中实现我们的业务逻辑。我们可以通过
我有一种树系统。我想做的是给所有 parent 一个 margin ,除了第一个。这是我的 HTML: Test
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
我是一名优秀的程序员,十分优秀!