Основная активность
public class UserFormActivity extends AppCompatActivity implements UserOptionAdapter.PriceChangeNotifier{
List<UserFormModel> userFormModels = new ArrayList<>();
List<Question> questionList = new ArrayList<>();
List<Instruction> instructionList = new ArrayList<>();
List<Ticketinfo> ticketinfoList = new ArrayList<>();
TextView tv_ticket_name,tv_ticket_qty,tv_total_price,event_name;
private RecyclerView recyclerView;
private UserFormAdapter mAdapter;
private UserTicketInfoAdapter utadapter;
ImageView iv_back;
Button btn_submit_form;
public Dialog dialog;
String event_Name;
String qty;
int size;
JSONArray mainArray = new JSONArray();
HashMap<Integer, List<Question>> hashMap = new HashMap<>();
HashMap<String, UserOptionAdapter.Model> hashMapArrShows = new HashMap<String, UserOptionAdapter.Model>();
@Override
public void onPriceChange(String price, HashMap<String, UserOptionAdapter.Model> selectedTicketMap) {
Toast.makeText(this, "Total price is " + price, Toast.LENGTH_LONG).show();
hashMapArrShows = selectedTicketMap;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_form);
initView();
event_Name = getIntent().getStringExtra("EVENT_NAME");
if(event_Name.equals("")){
}else{
event_name.setText(event_Name.toString());
}
iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), EventBookingPageActivity.class);
startActivity(intent);
}
});
mAdapter = new UserFormAdapter(this, instructionList);
recyclerView = findViewById(R.id.rv_instruction);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
utadapter = new UserTicketInfoAdapter(this, ticketinfoList);
recyclerView = findViewById(R.id.rv_questions);
RecyclerView.LayoutManager qLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(qLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(utadapter);
UserForm("","");
btn_submit_form.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
size = hashMap.size();
Toast.makeText(UserFormActivity.this, "LIST SIZE : " + size, Toast.LENGTH_SHORT).show();
try {
size = hashMapArrShows.size();
Toast.makeText(UserFormActivity.this, "LIST SIZE : " + size, Toast.LENGTH_SHORT).show();
qty = String.valueOf(hashMapArrShows.get("ticket_id"));
for (Integer key : UserQuestionAdapter.hashMap.keySet()) {
List<Option> list = UserQuestionAdapter.hashMap.get(key);
JSONObject mainObject = new JSONObject();
mainObject.put("qid", list.get(0).getId());
JSONArray ticketArray = new JSONArray();
for (int a = 0; a < list.size(); a++) {
JSONObject mainTicket = new JSONObject();
mainTicket.put("question_id", list.get(a).getQuestionId());
mainTicket.put("question_type_id", list.get(a).getQuestionTypeId());
mainTicket.put("text", list.get(a).getStatus());
mainTicket.put("status", list.get(a).getText());
ticketArray.put(mainTicket);
}
mainObject.put("options", ticketArray);
mainArray.put(mainObject);
}
Log.d("PARSED ARRAT :", String.valueOf(mainArray));
Toast.makeText(UserFormActivity.this, "VALUE : " + mainArray, Toast.LENGTH_SHORT).show();
}catch (Exception e) {
String msg = e.getMessage();
Toast.makeText(UserFormActivity.this, "Error : " + msg, Toast.LENGTH_SHORT).show();
}
}
});
}
public void initView(){
iv_back = findViewById(R.id.iv_back);
tv_ticket_name = findViewById(R.id.ticket_name);
tv_ticket_qty = findViewById(R.id.ticket_qty);
tv_total_price = findViewById(R.id.total_price);
event_name = findViewById(R.id.tv_event_name);
btn_submit_form = findViewById(R.id.btn_submit_form);
}
public void openDailog (String content , String header) {
dialog = new Dialog(UserFormActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_splashloading);
dialog.show();
}
public void UserForm(String eventId,String bookingid){
openDailog("", "");
Retrofit rf = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = rf.create(Api.class);
Call<List<UserFormModel>> call = api.get_booking_form("2635","303");
call.enqueue(new Callback<List<UserFormModel>>() {
@Override
public void onResponse(Call<List<UserFormModel>> call, Response<List<UserFormModel>> response) {
if (!response.isSuccessful()) {
dialog.dismiss();
Toast.makeText(UserFormActivity.this, "No Internet Connection!", Toast.LENGTH_SHORT).show();
} else if (response.isSuccessful()) {
dialog.dismiss();
Result feeds = response.body().get(0).getResult();
int status = feeds.getStatus();
if (status == 200){
Toast.makeText(UserFormActivity.this, "RESPONSE : 200", Toast.LENGTH_SHORT).show();
List<Ticketinfo> ticketinfo = response.body().get(0).getTicketinfo();
tv_ticket_name.setText(ticketinfo.get(0).getTicketName().toString());
tv_ticket_qty.setText(ticketinfo.get(0).getQty().toString());
tv_total_price.setText(ticketinfo.get(0).getTotalPrice().toString());
if (response.body().get(0).getInstruction().size() > 0){
List<Instruction> instructions = response.body().get(0).getInstruction();
instructionList.addAll(instructions);
}
mAdapter.notifyDataSetChanged();
List<Ticketinfo> qtlist = response.body().get(0).getTicketinfo();
ticketinfoList.addAll(qtlist);
utadapter.notifyDataSetChanged();
}else if(status == 500){
Toast.makeText(UserFormActivity.this, "RESPONSE : 500", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<List<UserFormModel>> call, Throwable t) {
dialog.dismiss();
Log.d("Failure response : ", t.getMessage());
}
});
}
}
public class UserOptionAdapter extends RecyclerView.Adapter<UserOptionAdapter.MyViewHolder>{
Context context;
List<Option> optionList ;
PriceChangeNotifier priceChangeNotifier;
public int topPosition;
private RadioButton lastCheckedRB = null;
UserOptionAdapter io;
public UserOptionAdapter(Context context, List<Option> optionList,int topPosition,PriceChangeNotifier priceChangeNotifier) {
this.context = context;
this.optionList = optionList;
this.priceChangeNotifier = priceChangeNotifier;
this.topPosition = topPosition;
}
@Override
public UserOptionAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate
(R.layout.option_single_row, null);
return new UserOptionAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final UserOptionAdapter.MyViewHolder holder, final int position) {
holder.tv_options.setTag(position);
if (optionList.get(position).getQuestionTypeId().equals("1")){
holder.radioboxId.setVisibility(View.VISIBLE);
holder.radioOne.setText(optionList.get(position).getText());
}else if(optionList.get(position).getQuestionTypeId().equals("2")){
holder.checkboxId.setVisibility(View.VISIBLE);
holder.chkone.setText(optionList.get(position).getText());
}else if(optionList.get(position).getQuestionTypeId().equals("3")){
holder.textboxId.setVisibility(View.VISIBLE);
holder.tv_options.setText(optionList.get(position).getText());
}
Option currentOption = optionList.get(position);
Model model = new UserOptionAdapter.Model();
model.setId(optionList.get(position).getId());
model.setText(optionList.get(position).getText());
selectedTicketListData.put(optionList.get(position).getId(), model);
optionList.set(position, currentOption);
UserQuestionAdapter.hashMap.put(Integer.valueOf(holder.tv_options.getTag().toString()), optionList);
holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
if (lastCheckedRB != null) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
});
}
public class MyViewHolder extends RecyclerView.ViewHolder {
EditText tv_options;
CheckBox chkone;
RadioButton radioOne;
Button btn;
RadioGroup rg;
LinearLayout textboxId,checkboxId,radioboxId;
public MyViewHolder(@NonNull final View itemView) {
super(itemView);
tv_options = itemView.findViewById(R.id.tv_options);
chkone = itemView.findViewById(R.id.chkOne);
radioOne = itemView.findViewById(R.id.radioOne);
textboxId = itemView.findViewById(R.id.textboxId);
checkboxId = itemView.findViewById(R.id.checkboxId);
radioboxId = itemView.findViewById(R.id.radioboxId);
rg = itemView.findViewById(R.id.radioSex);
tv_options.setTag(topPosition);
tv_options.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Option currentOption = optionList.get(getAdapterPosition());
currentOption.setText(s.toString());
UserOptionAdapter.Model model = new UserOptionAdapter.Model();
model.setId(currentOption.getId());
model.setText(currentOption.getText());
selectedTicketListData.put(currentOption.getText(), model);
optionList.set(getAdapterPosition(), currentOption);
UserQuestionAdapter.hashMap.put(Integer.valueOf(tv_options.getTag().toString()), optionList);
Toast.makeText(context, "Changed", Toast.LENGTH_SHORT).show();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
public interface PriceChangeNotifier {
void onPriceChange(String price, HashMap<String, UserOptionAdapter.Model> selectedTicketMap);
}
public class Model {
String id,question_id,question_type_id,text,status;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getQuestion_id() {
return question_id;
}
public void setQuestion_id(String question_id) {
this.question_id = question_id;
}
public String getQuestion_type_id() {
return question_type_id;
}
public void setQuestion_type_id(String question_type_id) {
this.question_type_id = question_type_id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
public static HashMap<String, Model> selectedTicketListData = new HashMap<>();
@Override
public int getItemCount() {
return optionList.size();
}
}
Всякий раз, когда я нажимаю на текст, он скрывает мою другую опцию. Я хочу получить UserOptionAdapter.Model в Userform, как вызвать его и отправить в json? Мне нужна быстрая помощь. Будем высоко оценены.