Android Spinner';s的随机项目id未正确获取
我有一个像这样的安卓旋转器 第一个是项目名称,第二个是各自的项目id。我想在选择项目名称时将各自的项目id发送到服务器,但我不想同时显示项目id。如何解决此问题,我的代码如下:Android Spinner';s的随机项目id未正确获取,android,android-layout,android-arrayadapter,android-volley,android-spinner,Android,Android Layout,Android Arrayadapter,Android Volley,Android Spinner,我有一个像这样的安卓旋转器 第一个是项目名称,第二个是各自的项目id。我想在选择项目名称时将各自的项目id发送到服务器,但我不想同时显示项目id。如何解决此问题,我的代码如下: public class Form extends BaseActivity implements AdapterView.OnItemClickListener { private static final String REGISTER_URL = "http://url/Service.asmx/Gen
public class Form extends BaseActivity implements AdapterView.OnItemClickListener {
private static final String REGISTER_URL = "http://url/Service.asmx/GenerateTicket";
public static final String PREFS_NAME = "MyPrefsFile";
public static final String CUSTOMERID = "customerId";
public static final String USERNAME = "name";
public static final String HOUSENO = "houseNo";
public static final String LOCALITY = "areaName";
public static final String SERVICE = "serviceId";
public static final String MOBILE = "mobile";
public static final String EMAIL = "email";
public static final String PROBLEM = "jobBrief";
private ProgressDialog pDialog;
Spinner spin;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
// ArrayList<String> listItems = new ArrayList<>();
// ArrayAdapter<String> adapter;
private List<Item> customerList = new ArrayList<Item>();
private SpinAdapter adapter;
private List<Item> items;
private EditText editname, houseNo, mobile, email, problem;
Spinner service_need;
AutoCompleteTextView autoCompView;
String obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_form, frameLayout);
autoCompView = (AutoCompleteTextView) findViewById(R.id.colony);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
editname = (EditText) findViewById(R.id.name);
houseNo = (EditText) findViewById(R.id.houseNo);
//
mobile = (EditText) findViewById(R.id.mobile);
email = (EditText) findViewById(R.id.email);
problem = (EditText) findViewById(R.id.problem);
Button submit = (Button) findViewById(R.id.submit);
pDialog = new ProgressDialog(this);
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
//
assert submit != null;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editname.getText().toString().trim().equals("")) {
Toast.makeText(getApplicationContext(),
"Please enter your name", Toast.LENGTH_SHORT)
.show();
} else if (houseNo.toString().trim().equals("")) {
Toast.makeText(Form.this, "Please enter your house no", Toast.LENGTH_SHORT).show();
} else if (autoCompView.toString().trim().equals("")) {
Toast.makeText(Form.this, "Please enter your locality", Toast.LENGTH_SHORT).show();}
// } else if (service_need.getSelectedItem().toString().trim().equals("Select Service")) {
// Toast.makeText(Form.this, "Please Select item", Toast.LENGTH_SHORT).show();
//
// }
else if (mobile.toString().trim().equals("")) {
Toast.makeText(Form.this, "Please enter your mobile number", Toast.LENGTH_SHORT).show();
}
else if (mobile.getText().length() < 10) {
Toast.makeText(getApplicationContext(),
"Please enter valid mobile number", Toast.LENGTH_SHORT)
.show();
}
else if (problem.toString().trim().equals("")) {
Toast.makeText(Form.this, "Please describe your problem", Toast.LENGTH_SHORT).show();
}
else if (!email.getText().toString().trim().equals(""))
{
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
email.getText().toString().trim()).matches()) {
Toast.makeText(getApplicationContext(),
"Please enter valid e-mail id", Toast.LENGTH_SHORT)
.show();
}
else {
if (isInternetPresent) {
registerUser();
pDialog.setMessage("Loading...");
pDialog.show();
Toast.makeText(getApplicationContext(),
"connecting...", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(Form.this, "Unable to connect the server, please check your data settings", Toast.LENGTH_LONG)
.show();
}
}
}
else {
// Do your stuff
if (isInternetPresent) {
registerUser();
pDialog.setMessage("Loading...");
pDialog.show();
Toast.makeText(getApplicationContext(),
"connecting...", Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(Form.this, "Unable to connect the server, please check your data settings", Toast.LENGTH_LONG)
.show();
}
}
}
});
//
spin = (Spinner) findViewById(R.id.service_need);
adapter = new SpinAdapter(this, customerList);
spin.setAdapter(adapter);
}
private void registerUser() {
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final String value=(mSharedPreference.getString("customerId", "Default_Value"));
final String customer_id = value.trim();
final String username = editname.getText().toString().trim();
final String house = houseNo.getText().toString().trim();
final String local_area = autoCompView.getText().toString().trim();
**Something wrong with this line**
**final String service = spin.getSelectedItem().toString().trim();**
final String mobile_no = mobile.getText().toString().trim();
final String email_id = email.getText().toString().trim();
final String prob = problem.getText().toString().trim();
Toast.makeText(Form.this, username.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(Form.this, service.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(Form.this, local_area.toString(), Toast.LENGTH_LONG).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hidePDialog();
try {
//String result="";
//Do it with this it will work
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject person = jsonarray.getJSONObject(i);
String excep = person.getString("Exception");
String message1 = person.getString("Message");
String job = person.getString("JobNo");
if (excep.equalsIgnoreCase("True")) {
Toast.makeText(Form.this, excep, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(Form.this, excep, Toast.LENGTH_LONG)
.show();
editname.setText("");
// if email and mb is valid than login
Intent i1 = new Intent(Form.this, Suceessful.class);
i1.putExtra("job_id", job);
startActivity(i1);
finish();
Toast.makeText(Form.this, excep.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(Form.this, message1.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(Form.this, job.toString(), Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Form.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(CUSTOMERID,customer_id);
params.put(USERNAME, username);
params.put(HOUSENO, house);
params.put(LOCALITY, local_area);
params.put(SERVICE, service);
params.put(MOBILE, mobile_no);
params.put(EMAIL, email_id);
params.put(PROBLEM, prob);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
adapter.notifyDataSetChanged();
}
protected Void doInBackground(Void... params) {
InputStream is = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://url/Service.asmx/GetServiceList");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result += line;
}
is.close();
//result=sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = jArray.getJSONObject(i);
Item customer = new Item();
customer.setiD(obj.getString("ServiceId"));
customer.setsText(obj.getString("ServiceName"));
// adding movie to movies array
customerList.add(customer);
}
} catch (JSONException e) {
e.printStackTrace();
}
// adapter.notifyDataSetChanged();
return null;
}
protected void onPostExecute(Void result) {
customerList.addAll(items);
adapter.notifyDataSetChanged();
}
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.refresh);
item.setVisible(false);
return true;
}
}
公共类表单扩展BaseActivity实现AdapterView.OnItemClickListener{
专用静态最终字符串寄存器\u URL=”http://url/Service.asmx/GenerateTicket";
公共静态最终字符串PREFS\u NAME=“MyPrefsFile”;
公共静态最终字符串CUSTOMERID=“CUSTOMERID”;
公共静态最终字符串USERNAME=“name”;
公共静态最终字符串HOUSENO=“HOUSENO”;
公共静态最终字符串LOCALITY=“areaName”;
公共静态最终字符串SERVICE=“serviceId”;
公共静态最终字符串MOBILE=“MOBILE”;
公共静态最终字符串EMAIL=“EMAIL”;
公共静态最终字符串问题=“jobBrief”;
私人对话;
旋转器旋转;
//Internet连接状态的标志
布尔值isInternetPresent=false;
//连接检测器类
连接检测器cd;
//ArrayList listItems=新的ArrayList();
//阵列适配器;
private List customerList=new ArrayList();
专用适配器;
私人清单项目;
私人编辑文本编辑姓名、户号、手机、电子邮件、问题;
纺纱机服务需求;
自动完成文本视图自动压缩视图;
字符串对象;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
GetLayoutFlater().充气(R.layout.activity\u表单,frameLayout);
autoCompView=(AutoCompleteTextView)findViewById(R.id.colony);
setAdapter(新的GooglePlacesAutocompleteAdapter(这个,R.layout.list_项));
setOnItemClickListener(这个);
editname=(EditText)findViewById(R.id.name);
houseNo=(EditText)findViewById(R.id.houseNo);
//
mobile=(EditText)findviewbyd(R.id.mobile);
email=(EditText)findviewbyd(R.id.email);
问题=(EditText)findViewById(R.id.problem);
按钮提交=(按钮)findViewById(R.id.submit);
pDialog=新建进度对话框(此对话框);
cd=新连接检测器(getApplicationContext());
//获取Internet状态
isInternetPresent=cd.isConnectingToInternet();
//
断言提交!=null;
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(editname.getText().toString().trim().equals(“”){
Toast.makeText(getApplicationContext(),
“请输入您的姓名”,吐司。长度(短)
.show();
}else if(houseNo.toString().trim()等于(“”){
Toast.makeText(Form.this,“Please enter your house no”,Toast.LENGTH_SHORT.show();
}else if(autoCompView.toString().trim().equals(“”){
Toast.makeText(Form.this,“请输入您的位置”,Toast.LENGTH_SHORT.show();}
//}else if(service_need.getSelectedItem().toString().trim().equals(“选择服务”)){
//Toast.makeText(Form.this,“请选择项目”,Toast.LENGTH_SHORT.show();
//
// }
else if(mobile.toString().trim().equals(“”){
Toast.makeText(Form.this,“请输入您的手机号码”,Toast.LENGTH_SHORT.show();
}
else if(mobile.getText().length()<10){
Toast.makeText(getApplicationContext(),
“请输入有效的手机号码”,Toast.LENGTH\u SHORT)
.show();
}
else if(problem.toString().trim().equals(“”){
Toast.makeText(Form.this,“请描述您的问题”,Toast.LENGTH_SHORT.show();
}
如果(!email.getText().toString().trim().equals(“”),则为else
{
如果(!android.util.Patterns.EMAIL_ADDRESS.matcher)(
email.getText().toString().trim()).matches()){
Toast.makeText(getApplicationContext(),
“请输入有效的电子邮件id”,Toast.LENGTH\u SHORT)
.show();
}
否则{
如果(isInternetPresent){
registerUser();
设置消息(“加载…”);
pDialog.show();
Toast.makeText(getApplicationContext(),
“连接…”,吐司。长度(短)
.show();
}否则{
Toast.makeText(Form.this,“无法连接服务器,请检查数据设置”,Toast.LENGTH\u LONG)
.show();
}
}
}
否则{
//做你的事
如果(isInternetPresent){
registerUser();
设置消息(“加载…”);
pDialog.show();
Toast.makeText(getApplicationContext(),
“连接…”,吐司。长度(短)
.show();
}
否则{
Toast.makeText(Form.this,“无法连接服务器,请检查数据设置”,Toast.LENGTH\u LONG)
.show();
}
}
}
});
//
spin=(微调器)findviewbyd(R.id.service\u需要);
public class Item implements Serializable {
public String sText;
public int iD;
public int getiD() {
return iD;
}
public void setiD(int iD) {
this.iD = iD;
}
public Item(String sText, int iD) {
this.sText = sText;
this.iD=iD;
}
public String getsText() {
return sText;
}
public void setsText(String sText) {
this.sText = sText;
}
@Override
public boolean equals(Object o) {
Item item = (Item) o;
if (item.getiD()==iD)
return true;
else
return false;
}
@Override
public String toString() {
return this.sText; // What to display in the Spinner list.
}
}
private ArrayAdapter adapter;
private List<Item> items;
items= new ArrayList<>();
adapter = new ArrayAdapter<Item>(getActivity(), android.R.layout.simple_spinner_item, items);
service_need.setAdapter(adapter);
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
// add interviewee name to arraylist
items.add(new Item(jsonObject.getString("ServiceName"),jsonObject.getString("ServiceId")));
}
} catch (JSONException e) {
e.printStackTrace();
}
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String customer_id=customerList.get(spinner_cat.getSelectedItemPosition()).getiD();
private static class Items
{
public String name;
public String id;
}