我正在开发一款用于求职的Android应用程序。我使用刷卡卡来查看像Tinder这样的工作。在这里,我正在使用PHP JSON获取所有工作。 每个工作都有不同的身份。如果要将作业用户ID和作业ID发送到服务器。用户ID可以使用共享首选项发送到服务器。但是当卡向右滑动时如何发送工作ID。我尝试了很多方法。但它不会起作用。请帮我。帮助将不胜感激..
try {
JSONArray jsonArray = new JSONArray(data);
arrayList.clear();
for (int i = 0; i<jsonArray.length(); i++){
getData pd = new getData();
JSONObject jsonObject = jsonArray.getJSONObject(i);
pd.setJob_title(jsonObject.getString("job_title"));
pd.setJob_type(jsonObject.getString("job_type"));
pd.setJob_description(jsonObject.getString("job_description"));
pd.setAddress(jsonObject.getString("address"));
pd.setCity(jsonObject.getString("city"));
pd.setJob_id(jsonObject.getString("job_id"));
arrayList.add(pd);
}
模型类
public class getData {
public String job_title = "";
public String job_description = "";
public String address = "";
public String job_type="";
public String city="";
public String job_id="";
public String getJob_description() {
return job_description;
}
public void setJob_description(String job_description) {
this.job_description = job_description;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getJob_type() {
return job_type;
}
public void setJob_type(String job_type) {
this.job_type = job_type;
}
public String getJob_title() {
return job_title;
}
public void setJob_title(String job_title) {
this.job_title = job_title;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getJob_id() {
return job_id;
}
public void setJob_id(String job_id) {
this.job_id = job_id;
}
}
适配器类
TextView tv_title = (TextView)v.findViewById(R.id.cardviewjobheading);
TextView tv_description= (TextView)v.findViewById(R.id.cardviewJobDescription);
TextView tv_jobtype = (TextView)v.findViewById(R.id.cardviewJobTime);
TextView tv_companyadress = (TextView)v.findViewById(R.id.cardviewcompanyname);
TextView tv_companycity = (TextView)v.findViewById(R.id.cardviewcompanyaddress);
TextView tv_jobid=(TextView)v.findViewById(R.id.cardviewjobid);
tv_title .setText(data.get(position).getJob_title().toString());
tv_jobtype.setText(data.get(position).getJob_type().toString());
tv_description.setText(data.get(position).getJob_description().toString());
tv_companyadress.setText(data.get(position).getAddress().toString());
tv_companycity.setText(data.get(position).getCity().toString());
tv_jobid.setText(data.get(position).getJob_id().toString());
String id=data.get(position).getJob_id().toString();
// Toast.makeText(context,id,Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = context.getSharedPreferences(Config.SHARED_PREF_NAME, MODE_PRIVATE).edit();
editor.putString("jobId",id);
editor.apply();
主要活动
@Override
public void cardSwipedRight( int position) {
saveinformation();
}
private void saveinformation() {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_APPLYJOB,
new Response.Listener<String>() {
@Override
public void onResponse(String ServerResponse) {
// Hiding the progress dialog after all task complete.
pDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(ServerResponse);
if (jsonObject.getInt("success") == 0) {
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} else if (jsonObject.getInt("success") == 1) {
// Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
MaterialToast materialToast=new MaterialToast(MainActivity.this);
materialToast.show(jsonObject.getString("message"), ContextCompat.getDrawable(MainActivity.this,R.drawable.toast_drawable),
ContextCompat.getColor(MainActivity.this,R.color.colorAccent),
Gravity.CENTER_HORIZONTAL);
} else
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
pDialog.dismiss();
MaterialToast materialToast=new MaterialToast(MainActivity.this);
materialToast.show("Check Internet Connection", ContextCompat.getDrawable(MainActivity.this,R.drawable.toast_drawable),
ContextCompat.getColor(MainActivity.this,R.color.colorAccent),
Gravity.CENTER_HORIZONTAL);
}
}) {
@Override
protected Map<String, String> getParams() {
// Creating Map String Params.
Map<String, String> params = new HashMap<String, String>();
// Adding All values to Params.
SharedPreferences prefs = getApplicationContext().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String userid = prefs.getString("userId","");
String jobid = prefs.getString("jobId","");
params.put("user_id",userid);
params.put("job_id",jobid);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
答案 0 :(得分:0)
不要使用SharedPreferance,只需使用您在cardSwipedRight()中收到的位置即可。使用该位置,使用arraylist.get(position)从arraylist中获取特定项。 然后使用返回的对象,并获取两个ID。 然后将这些值传递给该排球方法。
**完全没有为此目的使用SharedPreferance。