我有一个这样的Android微调器
第一个是项目名称,第二个是他们各自的项目ID。我想在选择项目名称时将相应的项目ID发送到服务器,我也不想显示项目ID。如何解决这个问题是我的代码:
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;
}
}
答案 0 :(得分:3)
有几种方法可以实现这一目标。我认为最简单的方法是为您的微调器项创建一个模型类。
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.
}
}
在活动课程中,您可以创建ArrayAdapter
。
private ArrayAdapter adapter;
private List<Item> items;
在onCreate
内初始化列表和适配器并设置适配器。
items= new ArrayList<>();
adapter = new ArrayAdapter<Item>(getActivity(), android.R.layout.simple_spinner_item, items);
service_need.setAdapter(adapter);
将json解析替换为:
// 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();
}
然后在adapter.notifyDataSetChanged()
内拨打onPostExecute()
。
在onClick
中,您可以从微调器中获取客户ID。
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String customer_id=customerList.get(spinner_cat.getSelectedItemPosition()).getiD();
答案 1 :(得分:1)
要隐藏ID,您不得将其添加到要传递给微调器的数组中。
使用结构创建具有属性名称和id的对象。
private static class Items
{
public String name;
public String id;
}
您可以添加setter和getters方法。 创建Items对象列表。从服务器设置name和id的每个对象值。 为微调器创建一个字符串数组,按顺序包含Items对象的名称。 在微调器的选择事件中,从所选位置获取列表中Items对象的id,并使用它发送到服务器。