我的活動中有一個RecyclerView,我從服務器的JSON數據中填充它。但是我想將點擊位置的數據發送到另一個活動,然後我可以編輯它們並將它們發送回服務器。但是,當我將數據發送到另一個活動的所有JSON數組被髮送到其他活動和未來活動的幾個實例是created.Here是我的活動包含RecyclerView:發送被點擊到另一個活動的位置的JSON數據
public class ActivityAllUsers extends AppCompatActivity {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ProgressBar progressBar;
String GET_JSON_DATA_HTTP_URL = "http://shayea.tk/sof/get_allusers.php";
String JSON_USERNAME = "usr_name";
String JSON_EMAIL = "usr_email";
String JSON_PHONE = "usr_phone";
String JSON_ORG = "org_name";
String JSON_STATUS ="usr_status";
String JSON_USRTYPE ="prv_name";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
JSON_DATA_WEB_CALL();
progressBar.setVisibility(View.VISIBLE);
}
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(final JSONArray array){
for(int i = 0; i<array.length(); i++) {
final GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setusername(json.getString(JSON_USERNAME));
GetDataAdapter2.setEmail(json.getString(JSON_EMAIL));
GetDataAdapter2.setPhone_number(json.getString(JSON_PHONE));
GetDataAdapter2.setPrivilege(json.getString(JSON_USRTYPE));
GetDataAdapter2.setOrganization(json.getString(JSON_ORG));
GetDataAdapter2.setStatus(json.getString(JSON_STATUS));
Log.e("JSON ME :" , json.toString());
final String username = (json.getString(JSON_USERNAME));
final String email = (json.getString(JSON_USERNAME));
final String phone = (json.getString(JSON_USERNAME));
final String usertype = (json.getString(JSON_USERNAME));
recyclerView.addOnItemTouchListener(
new RecyclerTouchListener(getApplicationContext(), recyclerView,
new RecyclerTouchListener.OnTouchActionListener() {
@Override
public void onClick(View view, int position) {
Intent intent = new Intent(ActivityAllUsers.this,RegisterNewCompany.class);
intent.putExtra("username" , username);
intent.putExtra("email" , email);
intent.putExtra("phone" , phone);
intent.putExtra("usertype" , usertype);
startActivity(intent);
}
@Override
public void onRightSwipe(View view, int position) {
}
@Override
public void onLeftSwipe(View view, int position) {
}
}));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
,這裏是我的RecyclerView適配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
holder.UsernameTV.setText(getDataAdapter1.getusername());
holder.EmailTV.setText(String.valueOf(getDataAdapter1.getEmail()));
holder.PhoneNumberTextView.setText(getDataAdapter1.getPhone_number());
holder.PrivilegeTV.setText(getDataAdapter1.getPrivilege());
holder.OrganizationTV.setText(getDataAdapter1.getOrganization());
holder.statusTV.setText(getDataAdapter1.getStatus());
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView UsernameTV;
public TextView EmailTV;
public TextView PhoneNumberTextView;
public TextView PrivilegeTV;
public TextView OrganizationTV;
public TextView statusTV;
public ViewHolder(View itemView) {
super(itemView);
UsernameTV = (TextView) itemView.findViewById(R.id.textView2) ;
EmailTV = (TextView) itemView.findViewById(R.id.textView4) ;
PhoneNumberTextView = (TextView) itemView.findViewById(R.id.textView6) ;
PrivilegeTV = (TextView) itemView.findViewById(R.id.textView8) ;
OrganizationTV = (TextView) itemView.findViewById(R.id.textView10);
statusTV = (TextView) itemView.findViewById(R.id.textView12);
}
}
}
,這裏是我的GetDataAdapter類,它包含getter和setter方法:
public class GetDataAdapter {
String username;
String phone_number;
String privilege;
String Email;
String organization;
String status;
public String getusername() {
return username;
}
public void setusername(String username1) {
this.username = username1;
}
public String getEmail() {
return Email;
}
public void setEmail(String email1) {
this.Email = email1;
}
public String getPrivilege() {
return privilege;
}
public void setPrivilege(String privilege1) {
this.privilege = privilege1;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization1) {
this.organization = organization1;
}
public String getStatus() {
return status;
}
public void setStatus(String status1) {
this.status = status1;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number1) {
this.phone_number = phone_number1;
}
}
我已經檢查了大部分問題與這種但是它們的實施是不同的。 任何幫助,將不勝感激
我將'recyclerView.addOnItemTouchListener'移出循環,現在它不會打開活動的多個實例,但我無法通過此代碼將數據發送給其他活動'clickedItem = array .getJSONObject(position);'你能引導我一點點或編輯代碼點擊一個項目 – Shayea