如何在 Android Spinner 中设置默认文本
How to set default Text in Android Spinner
我在微调器中显示值。我想显示像 "Select Table" 这样的默认文本。这是我的代码
JSONArray tablearray = tablenamejson.getJSONArray("data");
for (int i = 0; i < tablearray.length(); i++) {
JSONObject jsonObject = tablearray.getJSONObject(i);
String table_id = jsonObject.getString(TAG_TABLE_ID);
String table_name = jsonObject.getString(TAG_TABLE_NAME);
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
TableData tables = new TableData();
tables.setTblId(table_id);
tables.setTblName(table_name);
tableDatas.add(tables);
}
adapter = new TableAdapter(tableDatas, getActivity());
spinner.setAdapter(adapter);
您可以使用微调器 .setSelection() 来实现此目的。
String myString = "Select Table"; //default value for spinner
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter();
int spinpos= myAdap.getPosition(myString);
//set the default according to value
spinner.setSelection(spinpos);
首先你应该声明
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
在 forloop
之外,因为您要在 list
中添加所有条目
首先在 list
中添加默认值,然后写入 forloop
以添加 json array
中的所有值,然后使用 spinner.setSelection(0);
方法在微调器中显示默认值您已将它添加到 array
中的第一个位置
代码如下
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
//for default value
TableData tables = new TableData();
tables.setTblId(0);
tables.setTblName("Select");
tableDatas.add(tables);
JSONArray tablearray = tablenamejson.getJSONArray("data");
for (int i = 0; i < tablearray.length(); i++) {
JSONObject jsonObject = tablearray.getJSONObject(i);
String table_id = jsonObject.getString(TAG_TABLE_ID);
String table_name = jsonObject.getString(TAG_TABLE_NAME);
TableData tables = new TableData();
tables.setTblId(table_id);
tables.setTblName(table_name);
tableDatas.add(tables);
}
adapter = new TableAdapter(tableDatas, getActivity());
spinner.setAdapter(adapter);
spinner.setSelection(0);
我在微调器中显示值。我想显示像 "Select Table" 这样的默认文本。这是我的代码
JSONArray tablearray = tablenamejson.getJSONArray("data");
for (int i = 0; i < tablearray.length(); i++) {
JSONObject jsonObject = tablearray.getJSONObject(i);
String table_id = jsonObject.getString(TAG_TABLE_ID);
String table_name = jsonObject.getString(TAG_TABLE_NAME);
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
TableData tables = new TableData();
tables.setTblId(table_id);
tables.setTblName(table_name);
tableDatas.add(tables);
}
adapter = new TableAdapter(tableDatas, getActivity());
spinner.setAdapter(adapter);
您可以使用微调器 .setSelection() 来实现此目的。
String myString = "Select Table"; //default value for spinner
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter();
int spinpos= myAdap.getPosition(myString);
//set the default according to value
spinner.setSelection(spinpos);
首先你应该声明
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
在 forloop
之外,因为您要在 list
首先在 list
中添加默认值,然后写入 forloop
以添加 json array
中的所有值,然后使用 spinner.setSelection(0);
方法在微调器中显示默认值您已将它添加到 array
代码如下
ArrayList<TableData> tableDatas = new ArrayList<TableData>();
//for default value
TableData tables = new TableData();
tables.setTblId(0);
tables.setTblName("Select");
tableDatas.add(tables);
JSONArray tablearray = tablenamejson.getJSONArray("data");
for (int i = 0; i < tablearray.length(); i++) {
JSONObject jsonObject = tablearray.getJSONObject(i);
String table_id = jsonObject.getString(TAG_TABLE_ID);
String table_name = jsonObject.getString(TAG_TABLE_NAME);
TableData tables = new TableData();
tables.setTblId(table_id);
tables.setTblName(table_name);
tableDatas.add(tables);
}
adapter = new TableAdapter(tableDatas, getActivity());
spinner.setAdapter(adapter);
spinner.setSelection(0);