我想将Spinner初始文本设置为SELECT ONE?怎么可能?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<ModelClass> arrayList;
RecyclerView.LayoutManager layoutManager;
EditText edit_name, edit_desc;
Spinner spinner;
Button btm_save, btn_delete;
String Name, Description, Spinner;
SqliteHelper sqliteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_name = findViewById(R.id.edit_name);
edit_desc = findViewById(R.id.edit_desc);
btm_save = findViewById(R.id.btn_save);
btn_delete = findViewById(R.id.btn_view);
spinner = findViewById(R.id.spinner);
sqliteHelper = new SqliteHelper(this);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Options, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
recyclerView = findViewById(R.id.recylerView);
arrayList = new ArrayList<>();
adapter = new Adapter(this, arrayList);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
edit_name.addTextChangedListener(textWatcher);
edit_desc.addTextChangedListener(textWatcher);
btm_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Name = edit_name.getText().toString();
Description = edit_desc.getText().toString();
Spinner = spinner.getSelectedItem().toString();
if (!ValidateUser(Name)) {
if (!TextUtils.isEmpty(Spinner)) {
ModelClass modelClass = new ModelClass(Name, Spinner, Description);
arrayList.add(modelClass);
adapter.notifyItemInserted(arrayList.size());
InsertData();
} else {
Toast.makeText(MainActivity.this, "Please Select Experience", Toast.LENGTH_SHORT).show();
}
}else
{
Toast.makeText(MainActivity.this, "User name Already Exits", Toast.LENGTH_SHORT).show();
}
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*RetriveData();*/
Integer deletedRows = sqliteHelper.deleteData(edit_name.getText().toString());
if (deletedRows > 0 )
{
Toast.makeText(MainActivity.this, "Record Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Record Not Found", Toast.LENGTH_SHORT).show();
}
}
});
}
private void InsertData() {
boolean InsertSuccessfully = sqliteHelper.InsertData(Name, Description, Spinner);
if (InsertSuccessfully) {
Toast.makeText(this, "Record Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Record Not Inserted", Toast.LENGTH_SHORT).show();
}
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Name = edit_name.getText().toString();
Description = edit_desc.getText().toString();
btm_save.setEnabled(!Name.isEmpty() && !Description.isEmpty());
}
@Override
public void afterTextChanged(Editable editable) {
}
};
public boolean ValidateUser(String name)
{
SQLiteDatabase database = sqliteHelper.getWritableDatabase();
Cursor cursor = database.query(SqliteHelper.TABLE_NAME, null, SqliteHelper.COL_2 + "=?", new String[]{name},
null, null, null);
int i = cursor.getCount();
if (i>0)
{
return true;
}else
{
return false;
}
}
}
答案 0 :(得分:0)
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,R.layout.spinner_item,plantsList){
@Override
public boolean isEnabled(int position){
if(position == 0)
{
// Disable the first item from Spinner
// First item will be use for hint
return false;
}
else
{
return true;
}
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0){
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
}
else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
添加提示消息作为微调器数组中的第一个元素。为此添加create ArrayAdapter。这将使初始元素不可选择,并显示为灰色。