如何在文本视图中获取微调值
how to get spinner values in textview
我在警报对话框微调器中得到 json 响应,现在我希望用户 selected edittext 中的项目用逗号分隔,但是当我 运行 我能够select 只有一项,而且在我的编辑文本中它只显示一项...
public class MainActivity extends Activity {
JSONParser jsonParser = new JSONParser();
JSONArray country_list=null;
private static String BUSINESS_URL = "";
private static final String BUSINESS_ID="id";
private static final String BUSINESS_NAME="name";
ArrayList<HashMap<String,String>> data;
private TextView splang;
private String cname;
private String business_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splang=(TextView)findViewById(R.id.txtvw);
new LoadBusiness().execute();
}
class LoadBusiness extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
ArrayAdapter<String> adaptercountry ;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
// pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(BUSINESS_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
country_list = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < country_list.length(); i++) {
JSONObject c = country_list.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(BUSINESS_ID, c.getString(BUSINESS_ID));
map.put(BUSINESS_NAME,c.getString(BUSINESS_NAME));
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
String[] arrConuntry=new String[data.size()];
for(int index=0;index<data.size();index++){
HashMap<String, String> map=data.get(index);
arrConuntry[index]=map.get(BUSINESS_NAME);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adaptercountry = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_multiple_choice,
arrConuntry);
splang.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View w) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Select")
.setAdapter(adaptercountry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
splang.setText(adaptercountry.getItem(which).toString());
try {
cname=country_list.getJSONObject(which).getString("id");
Log.d("Response: ", "> " + cname);
business_id=cname.toString();
System.out.println("Business_id"+business_id);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dialog.dismiss();
}
}).create().show();
}
});
}
}
定义从多项选择对话框中获得回调的自定义界面。
public interface MultipleItemSelectListener {
public void onSelected(String value,String ids);
}
自定义多项选择对话框方法。
参数
内容:您的activity内容参考。
标题:对话框标题。
jsonArrayString :您的 jsonarray 响应为字符串。
MultipleItemSelectListener : 自定义项目选择监听器。
public void getMultiSelectionDialog(final Context context,final String title, String jsonString, final String value, final MultipleItemSelectListener target) {
final ArrayList<String> values = new ArrayList<String>();
final ArrayList<String> ids = new ArrayList<String>();
final String BUSINESS_ID="id";
final String BUSINESS_NAME="name";
try {
JSONArray jsonArray = new JSONArray(jsonString);
int size = jsonArray.length();
for (int i = 0; i < size; i++) {
values.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_NAME).trim());
ids.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_ID).trim());
}
} catch (JSONException e) {
e.printStackTrace();
}
final boolean[] selections = new boolean[values.size()];
final StringBuilder newValue = new StringBuilder();
final StringBuilder newIds = new StringBuilder();
AlertDialog alert = null;
if (value.length() > 0) {
String[] oldValue = value.split(",");
int size = values.size();
for (int i = 0; i < size; i++) {
int len = oldValue.length;
for (int j = 0; j < len; j++) {
if (values.get(i).trim().equalsIgnoreCase(oldValue[j].trim())) {
selections[i] = true;
break;
}
}
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMultiChoiceItems(values.toArray(new CharSequence[values.size()]), selections, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
selections[which] = isChecked;
}
});
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = selections.length;
for (int i = 0; i < size; i++) {
if (selections[i]) {
newValue.append(newValue.length() > 0 ? "," + values.get(i) : values.get(i));
newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i));
}
}
target.onSelected(newValue.toString(),newIds.toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
int size = selections.length;
for (int i = 0; i < size; i++) {
if (selections[i]) {
newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i));
}
}
target.onSelected(value,newIds.toString());
}
});
alert = builder.create();
alert.show();
}
如何获取多选对话框:
class LoadBusiness extends AsyncTask<Void,Void,String> {
Context context;
public LoadBusiness(Context context){
this.context=context;
}
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(Void... params) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(BUSINESS_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
return jsonStr;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
setTextViewWithMultipleSelectionDialog(context,result);
}
}
public void setTextViewWithMultipleSelectionDialog(final Context context,final String jsonArrayString){
splang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getMultiSelectionDialog(context,"Countries",jsonArrayString,splang.getText().toString(),new MultipleItemSelectListener() {
@Override
public void onSelected(String value) {
splang.setText(value);
}
});
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splang=(TextView)findViewById(R.id.txtvw);
new LoadBusiness(this).execute();
}
我在警报对话框微调器中得到 json 响应,现在我希望用户 selected edittext 中的项目用逗号分隔,但是当我 运行 我能够select 只有一项,而且在我的编辑文本中它只显示一项...
public class MainActivity extends Activity {
JSONParser jsonParser = new JSONParser();
JSONArray country_list=null;
private static String BUSINESS_URL = "";
private static final String BUSINESS_ID="id";
private static final String BUSINESS_NAME="name";
ArrayList<HashMap<String,String>> data;
private TextView splang;
private String cname;
private String business_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splang=(TextView)findViewById(R.id.txtvw);
new LoadBusiness().execute();
}
class LoadBusiness extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
ArrayAdapter<String> adaptercountry ;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
// pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(BUSINESS_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
country_list = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < country_list.length(); i++) {
JSONObject c = country_list.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(BUSINESS_ID, c.getString(BUSINESS_ID));
map.put(BUSINESS_NAME,c.getString(BUSINESS_NAME));
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
String[] arrConuntry=new String[data.size()];
for(int index=0;index<data.size();index++){
HashMap<String, String> map=data.get(index);
arrConuntry[index]=map.get(BUSINESS_NAME);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adaptercountry = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_multiple_choice,
arrConuntry);
splang.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View w) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Select")
.setAdapter(adaptercountry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
splang.setText(adaptercountry.getItem(which).toString());
try {
cname=country_list.getJSONObject(which).getString("id");
Log.d("Response: ", "> " + cname);
business_id=cname.toString();
System.out.println("Business_id"+business_id);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dialog.dismiss();
}
}).create().show();
}
});
}
}
定义从多项选择对话框中获得回调的自定义界面。
public interface MultipleItemSelectListener {
public void onSelected(String value,String ids);
}
自定义多项选择对话框方法。
参数
内容:您的activity内容参考。
标题:对话框标题。
jsonArrayString :您的 jsonarray 响应为字符串。
MultipleItemSelectListener : 自定义项目选择监听器。
public void getMultiSelectionDialog(final Context context,final String title, String jsonString, final String value, final MultipleItemSelectListener target) {
final ArrayList<String> values = new ArrayList<String>();
final ArrayList<String> ids = new ArrayList<String>();
final String BUSINESS_ID="id";
final String BUSINESS_NAME="name";
try {
JSONArray jsonArray = new JSONArray(jsonString);
int size = jsonArray.length();
for (int i = 0; i < size; i++) {
values.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_NAME).trim());
ids.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_ID).trim());
}
} catch (JSONException e) {
e.printStackTrace();
}
final boolean[] selections = new boolean[values.size()];
final StringBuilder newValue = new StringBuilder();
final StringBuilder newIds = new StringBuilder();
AlertDialog alert = null;
if (value.length() > 0) {
String[] oldValue = value.split(",");
int size = values.size();
for (int i = 0; i < size; i++) {
int len = oldValue.length;
for (int j = 0; j < len; j++) {
if (values.get(i).trim().equalsIgnoreCase(oldValue[j].trim())) {
selections[i] = true;
break;
}
}
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMultiChoiceItems(values.toArray(new CharSequence[values.size()]), selections, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
selections[which] = isChecked;
}
});
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = selections.length;
for (int i = 0; i < size; i++) {
if (selections[i]) {
newValue.append(newValue.length() > 0 ? "," + values.get(i) : values.get(i));
newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i));
}
}
target.onSelected(newValue.toString(),newIds.toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
int size = selections.length;
for (int i = 0; i < size; i++) {
if (selections[i]) {
newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i));
}
}
target.onSelected(value,newIds.toString());
}
});
alert = builder.create();
alert.show();
}
如何获取多选对话框:
class LoadBusiness extends AsyncTask<Void,Void,String> {
Context context;
public LoadBusiness(Context context){
this.context=context;
}
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(Void... params) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(BUSINESS_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
return jsonStr;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
setTextViewWithMultipleSelectionDialog(context,result);
}
}
public void setTextViewWithMultipleSelectionDialog(final Context context,final String jsonArrayString){
splang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getMultiSelectionDialog(context,"Countries",jsonArrayString,splang.getText().toString(),new MultipleItemSelectListener() {
@Override
public void onSelected(String value) {
splang.setText(value);
}
});
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splang=(TextView)findViewById(R.id.txtvw);
new LoadBusiness(this).execute();
}