我有一个recyclerview,其中包含具有edittext,datepicker和Spinner的cardViews。每个卡片视图都有(+)按钮,可用于将新的卡片视图添加到回收站视图。我面临的问题是我想从recyclerview中所有可用的卡中获取值,包括将新添加的卡添加到对象类型数组列表中。目前,我可以添加新的空白卡,但无法将用户从该卡中输入的值捕获到列表中。
我想将上面粉红色标记的值保存到recyclerview列表中,以便可以使用rest api将其保存到服务器。
EmpEducationInfoFormActivity.java
package abc.com.abcpos.forms;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import abc.com.abcpos.Adapters.EducationInfoAdapter;
import abc.com.abcpos.Commons.NumericKeyBoardTransformationMethod;
import abc.com.abcpos.Commons.SavingUrls;
import abc.com.abcpos.Commons.Validations;
import abc.com.abcpos.Handlers.HttpHandler;
import abc.com.abcpos.MainActivity;
import abc.com.abcpos.Models.EmpEducationDetailsModel;
import abc.com.abcpos.Models.EmpPersonalDetails;
import abc.com.abcpos.R;
import abc.com.abcpos.WelcomeActivity;
public class EmpEducationInfoFormActivity extends AppCompatActivity {
Toolbar toolbar;
boolean connected=false;
String jsonStr;
NumericKeyBoardTransformationMethod numKeyBoard;
EmpEducationDetailsModel empEducationDetailsModelObject;
private ArrayList<EmpEducationDetailsModel.EmpEducationDetails> empEducationDetailsModelLists;
private ArrayList<EmpEducationDetailsModel.EmpEducationDetails> saveEducationDetailsModelLists;
private RecyclerView empEducationDetailsRecyclerView;
private RecyclerView.Adapter empEducationDetailsAdapter;
private RecyclerView.LayoutManager empEducationDetailsLayoutManager;
Button saveBtn,cancelBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emp_education_info_form);
initViews();
initListeners();
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()== NetworkInfo.State.CONNECTED||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()==NetworkInfo.State.CONNECTED){
connected=true;
}else{
connected=false;
}
new GetContacts().execute();
}
private void initViews(){
toolbar=(Toolbar) findViewById(R.id.toolbarEmpEducationInfoFormActivity);
toolbar.setTitle("Education Details");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);
saveBtn=(Button)findViewById(R.id.saveBtn);
cancelBtn=(Button)findViewById(R.id.cancelBtn);
numKeyBoard=new NumericKeyBoardTransformationMethod();
empEducationDetailsModelLists=new ArrayList<EmpEducationDetailsModel.EmpEducationDetails>();
empEducationDetailsRecyclerView=(RecyclerView)findViewById(R.id.recyclerView_EducationDetailsActivity);
empEducationDetailsRecyclerView.setHasFixedSize(true);
}
private void initListeners(){
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(EmpEducationInfoFormActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
JSONObject jo1=new JSONObject();
JSONArray ja1=new JSONArray();
for(int j=0;j<saveEducationDetailsModelLists.size();j++){
try {
jo1.put("userId",empEducationDetailsModelLists.get(j).getUserId());
jo1.put("education",saveEducationDetailsModelLists.get(j).getEducation());
jo1.put("university",saveEducationDetailsModelLists.get(j).getUniversity());
jo1.put("marks",saveEducationDetailsModelLists.get(j).getMarks());
jo1.put("passingYear",saveEducationDetailsModelLists.get(j).getPassingYear());
jo1.put("stream",saveEducationDetailsModelLists.get(j).getStream());
jo1.put("grade",saveEducationDetailsModelLists.get(j).getGrade());
ja1.put(j,jo1);
}catch (Exception e){
e.printStackTrace();
}
}
Sender s = new Sender(EmpEducationInfoFormActivity.this, SavingUrls.sending_personal_det_url,ja1);
s.execute();
}catch (Exception e){
e.printStackTrace();
}
//checkValidation();
/*Intent i = new Intent(EmpEducationInfoFormActivity.this,MainActivity.class);
SharedPreferences myPrefs = getSharedPreferences("submitFlags", Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putBoolean("educationInfoFlag", true);
prefsEditor.commit();
startActivity(i);*/
}
});
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
public void sendAllDataToServer(ArrayList<EmpEducationDetailsModel.EmpEducationDetails> mEmpEducationDetailsList) {
saveEducationDetailsModelLists=mEmpEducationDetailsList;
}
private class GetContacts extends AsyncTask<Object, Object, ArrayList<EmpEducationDetailsModel.EmpEducationDetails>> {
ProgressDialog dialog = new ProgressDialog(EmpEducationInfoFormActivity.this);
@Override
protected void onPreExecute() {
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Loading...");
dialog.show();
super.onPreExecute();
}
@Override
protected ArrayList<EmpEducationDetailsModel.EmpEducationDetails> doInBackground(Object... objects) {
try{
HttpHandler sh = new HttpHandler();
String url = "http://202.38.173.190:8080/TelephoneDirectoryApi/getAllCategoryData";
Date date = new Date();
if (connected) {
/*jsonStr = sh.makeServiceCall(url);*/
jsonStr="[{\"userId\":\"kbg-000002\",\"education\":\"Graduation\",\"university\":\"sdsa\",\"marks\":\"321\",\"passingYear\":\"3232\"," +
"\"stream\":\"dsdsa\",\"grade\":\"d\",\"empEducationList\":null},{\"userId\":\"kbg-000002\",\"education\":\"Post Graduation\"," +
"\"university\":\"sdsa\",\"marks\":\"321\",\"passingYear\":\"3232\",\"stream\":\"dsdsa\",\"grade\":\"d\",\"empEducationList\":null}]";
if (jsonStr != null) {
try {
ArrayList<EmpEducationDetailsModel.EmpEducationDetails> empEducationList = new ArrayList<EmpEducationDetailsModel.EmpEducationDetails>();
JSONArray eduArray = new JSONArray(jsonStr);
for (int i = 0; i < eduArray.length(); i++) {
JSONObject jsonobject = eduArray.getJSONObject(i);
String userId = jsonobject.getString("userId");
String education = jsonobject.getString("education");
String university = jsonobject.getString("university");
String marks = jsonobject.getString("marks");
String passingYear = jsonobject.getString("passingYear");
String stream = jsonobject.getString("stream");
String grade = jsonobject.getString("grade");
empEducationList.add(new EmpEducationDetailsModel.EmpEducationDetails(userId,education,university,marks,passingYear,stream,grade));
}
empEducationDetailsModelLists.addAll(empEducationList);
}catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}else{
Toast.makeText(EmpEducationInfoFormActivity.this,
"Sorry! You are not connected to internet.", Toast.LENGTH_LONG).show();
}
return empEducationDetailsModelLists;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<EmpEducationDetailsModel.EmpEducationDetails> empEducationDetails) {
super.onPostExecute(empEducationDetails);
if(dialog != null && dialog.isShowing()){
dialog.dismiss();
}
empEducationDetailsAdapter=new EducationInfoAdapter(empEducationDetailsModelLists,EmpEducationInfoFormActivity.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(EmpEducationInfoFormActivity.this);
empEducationDetailsRecyclerView.setLayoutManager(layoutManager);
empEducationDetailsRecyclerView.setAdapter(empEducationDetailsAdapter);
}
}
@Override
protected void onResume() {
super.onResume();
}
private class Sender extends AsyncTask<Void,Void,String>{
Context c;
String urlAddress;
ProgressDialog pd = new ProgressDialog(EmpEducationInfoFormActivity.this);
JSONArray jsonArray;
public Sender(Context c, String urlAddress, JSONArray jsonArray) {
this.c = c;
this.urlAddress = urlAddress;
this.jsonArray = jsonArray;
}
@Override
protected void onPreExecute() {
pd.setCanceledOnTouchOutside(false);
pd.setMessage("Loading...");
pd.show();
super.onPreExecute();
}
@Override
protected String doInBackground(Void... voids) {
return this.send();
}
private String send() {
try {
URL url = new URL(SavingUrls.sending_edu_det_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = jsonArray.toString() ;
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
if (s != null) {
Toast.makeText(c, s, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(c, "Unsuccessful " + s, Toast.LENGTH_LONG).show();
}
}
}
}
EducationInfoAdapter.java
package abc.com.abcpos.Adapters;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import abc.com.abcpos.Commons.NumericKeyBoardTransformationMethod;
import abc.com.abcpos.Commons.Validations;
import abc.com.abcpos.Models.EmpEducationDetailsModel;
import abc.com.abcpos.R;
import abc.com.abcpos.forms.EmpEducationInfoFormActivity;
public class EducationInfoAdapter extends RecyclerView.Adapter<EducationInfoAdapter.EducationViewHolder>{
private ArrayList<EmpEducationDetailsModel.EmpEducationDetails> mEmpEducationDetailsList;
Context ctx;
Validations validationsObj =new Validations();
public EducationInfoAdapter(ArrayList<EmpEducationDetailsModel.EmpEducationDetails> mEmpEducationDetailsList,Context ctx) {
this.ctx = ctx;
this.mEmpEducationDetailsList = mEmpEducationDetailsList;
}
public void addItem(){
mEmpEducationDetailsList.add(new EmpEducationDetailsModel.EmpEducationDetails());
notifyItemInserted(mEmpEducationDetailsList.size() - 1);
}
@NonNull
@Override
public EducationInfoAdapter.EducationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_educationinfo, parent, false);
return new EducationViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final EducationInfoAdapter.EducationViewHolder holder, final int position) {
try{
holder.nameOfBoardEditText.setText(mEmpEducationDetailsList.get(position).getUniversity());
holder.marksObtained.setText(mEmpEducationDetailsList.get(position).getMarks());
holder.stream.setText(mEmpEducationDetailsList.get(position).getStream());
holder.grade.setText(mEmpEducationDetailsList.get(position).getGrade());
holder.passingYear.setText(mEmpEducationDetailsList.get(position).getPassingYear());
if (mEmpEducationDetailsList.get(position).getEducation().toString() != null) {
int spinnerPosition = holder.adapter.getPosition(mEmpEducationDetailsList.get(position).getEducation().toString());
holder.educationSpinner.setSelection(spinnerPosition);
}
if(ctx instanceof EmpEducationInfoFormActivity){
((EmpEducationInfoFormActivity)ctx).sendAllDataToServer(this.mEmpEducationDetailsList);
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mEmpEducationDetailsList.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
public List<EmpEducationDetailsModel.EmpEducationDetails> retrieveEduData()
{
return mEmpEducationDetailsList;
}
public class EducationViewHolder extends RecyclerView.ViewHolder {
EditText passingYear,marksObtained,stream,grade,nameOfBoardEditText;
Spinner educationSpinner;
String[]paths = {"Select Education", "Matriculation", "Higher Secondary", "Graduation","Post Graduation","Technical","Professional"};
NumericKeyBoardTransformationMethod numKeyBoard;
TextInputLayout educationDetailsTextInputLayout,educationDetailsTextInputLayout1,educationDetailsTextInputLayout2,
educationDetailsTextInputLayout3,educationDetailsTextInputLayout4,educationDetailsTextInputLayout5;
Button addEmpEducation,delEmpEducation;
ArrayAdapter<String> adapter;
public EducationViewHolder(@NonNull View itemView) {
super(itemView);
passingYear=(EditText) itemView.findViewById(R.id.passingYear);
marksObtained=(EditText) itemView.findViewById(R.id.marksObtained);
stream=(EditText) itemView.findViewById(R.id.stream);
grade=(EditText) itemView.findViewById(R.id.grade);
nameOfBoardEditText=(EditText) itemView.findViewById(R.id.nameOfBoardEditText);
marksObtained.setTransformationMethod(numKeyBoard);
educationDetailsTextInputLayout=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout);
educationDetailsTextInputLayout1=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout1);
educationDetailsTextInputLayout2=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout2);
educationDetailsTextInputLayout3=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout3);
educationDetailsTextInputLayout4=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout4);
educationDetailsTextInputLayout5=(TextInputLayout) itemView.findViewById(R.id.educationDetailsTextInputLayout5);
educationSpinner=(Spinner) itemView.findViewById(R.id.educationSpinner);
addEmpEducation=(Button) itemView.findViewById(R.id.addEmpEducation);
delEmpEducation=(Button) itemView.findViewById(R.id.delEmpEducation);
passingYear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
validationsObj.dateDialog(passingYear,(Activity) v.getContext(),event);
//mEmpEducationDetailsList.get(getAdapterPosition()).setPassingYear(String.valueOf(passingYear.getText()));
return true;
}
});
adapter = new ArrayAdapter<String>(itemView.getContext(),
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
educationSpinner.setAdapter(adapter);
delEmpEducation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEmpEducationDetailsList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(), mEmpEducationDetailsList.size());
}
});
addEmpEducation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*mEmpEducationDetailsList.add(new EmpEducationDetailsModel.EmpEducationDetails());
notifyItemInserted(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(), mEmpEducationDetailsList.size());*/
addItem();
}
});
}
}
}
答案 0 :(得分:0)
我了解您的问题,首先您必须使用界面来删除和添加卡,必须使用界面来更新 EmpEducationInfoFormActivity.java的 Arraylist 类,之后您必须调用 empEducationDetailsAdapter.notifyDataSetChanged() ;;
对于卡的删除,从特定位置的数组列表中删除记录,然后调用 empEducationDetailsAdapter.notifyDataSetChanged();它将刷新recyclerview。
对于卡的添加,在最后一个位置在数组列表中添加新数据,然后调用 empEducationDetailsAdapter.notifyDataSetChanged();它将刷新recyclerview。