英文:
How to create a new card after taking input from dialog box
问题
基本上,点击按钮后会打开一个对话框,我在其中捕获信息。一旦我在对话框中点击“完成”选项,我希望创建一个包含该信息的新卡片。我已经实现了用于实现上述目标的可回收视图,但出于某种原因它不起作用。有人可以告诉我出了什么问题吗?
这是我的 Adapter 代码:
package com.example.android.teleconsultation_doctor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterManageSlots extends RecyclerView.Adapter<AdapterManageSlots.ManageSlotsViewHolder> {
private ArrayList<CardViewManageSlots> mManageSlots;
public static class ManageSlotsViewHolder extends RecyclerView.ViewHolder {
public TextView mSlots, mTiming;
public ManageSlotsViewHolder(@NonNull View itemView) {
super(itemView);
mTiming = itemView.findViewById(R.id.textViewTimingValue);
mSlots = itemView.findViewById(R.id.textViewSlotValue);
}
}
public AdapterManageSlots(ArrayList<CardViewManageSlots> manageSlots) {
mManageSlots = manageSlots;
}
@NonNull
@Override
public ManageSlotsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_manage_slots, viewGroup, false);
ManageSlotsViewHolder evh = new ManageSlotsViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ManageSlotsViewHolder manageSlotsViewHolder, int i) {
CardViewManageSlots currentItem = mManageSlots.get(i);
manageSlotsViewHolder.mSlots.setText(currentItem.getSlot());
manageSlotsViewHolder.mTiming.setText(currentItem.getTiming());
}
@Override
public int getItemCount() {
return mManageSlots.size();
}
}
这是活动的 Java 代码 :
package com.example.android.teleconsultation_doctor;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class ManageSlots extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
// ...
}
这是活动的 XML 代码 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManageSlots">
<!-- ... -->
</RelativeLayout>
英文:
Basically on clicking a button a dialog box opens from which I am capturing information. Once I click on "Done" option present in the Dialog box, I want a new card to be created comprising of that information. I have implemented recycler view for achieving the above but for some reason it is not working. Could someone tell me what's wrong?
Here's the code of my Adapter
package com.example.android.teleconsultation_doctor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterManageSlots extends RecyclerView.Adapter<AdapterManageSlots.ManageSlotsViewHolder> {
private ArrayList<CardViewManageSlots> mManageSlots;
public static class ManageSlotsViewHolder extends RecyclerView.ViewHolder{
public TextView mSlots, mTiming;
public ManageSlotsViewHolder(@NonNull View itemView) {
super(itemView);
mTiming = itemView.findViewById(R.id.textViewTimingValue);
mSlots = itemView.findViewById(R.id.textViewSlotValue);
}
}
public AdapterManageSlots(ArrayList<CardViewManageSlots> manageSlots){
mManageSlots = manageSlots;
}
@NonNull
@Override
public ManageSlotsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_manage_slots, viewGroup, false);
ManageSlotsViewHolder evh = new ManageSlotsViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ManageSlotsViewHolder manageSlotsViewHolder, int i) {
CardViewManageSlots currentItem = mManageSlots.get(i);
manageSlotsViewHolder.mSlots.setText(currentItem.getSlot());
manageSlotsViewHolder.mTiming.setText(currentItem.getTiming());
}
@Override
public int getItemCount() {
return mManageSlots.size();
}
}
Here is the JAVA code of the activity
package com.example.android.teleconsultation_doctor;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class ManageSlots extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
ImageView imageViewCalender;
TextView textViewDateValue;
String date, spinnerSlotValue, spinnerEndTimeValue, spinnerStartTimeValue;
Button buttonAddSlot;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_slots);
ArrayList<CardViewManageSlots> slotDetails = new ArrayList<>();
imageViewCalender = findViewById(R.id.imageViewCalander);
textViewDateValue = findViewById(R.id.textViewDateValue);
buttonAddSlot = findViewById(R.id.buttonAddSlot);
imageViewCalender.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
buttonAddSlot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(ManageSlots.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_manage_slots, null);
Spinner mSpinnerSlots = mView.findViewById(R.id.spinnerSlots);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.slot_names));
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerSlots.setAdapter(adapter);
spinnerSlotValue = mSpinnerSlots.getSelectedItem().toString();
Spinner mSpinnerStartTime = mView.findViewById(R.id.spinnerStartTime);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time));
adapter1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerStartTime.setAdapter(adapter1);
spinnerStartTimeValue = mSpinnerStartTime.getSelectedItem().toString();
Spinner mSpinnerEndTime = mView.findViewById(R.id.spinnerEndTime);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time));
adapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerEndTime.setAdapter(adapter2);
spinnerEndTimeValue = mSpinnerEndTime.getSelectedItem().toString();
mBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Toast.makeText(ManageSlots.this, "Slot Created!" + spinnerSlotValue + spinnerEndTimeValue + spinnerStartTimeValue, Toast.LENGTH_LONG).show();
dialogInterface.dismiss();
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
String finalTime = spinnerStartTimeValue + "-" + spinnerEndTimeValue;
Toast.makeText(this, finalTime, Toast.LENGTH_SHORT).show();
slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
//slotDetails.add(new CardViewManageSlots("Morning","diwwodmw"));
mRecyclerView = findViewById(R.id.recyclerViewSlots);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new AdapterManageSlots(slotDetails);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
private void showDatePickerDialog() {
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
this,
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.show();
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
int i3=i1;
i3=i3+1;
date = i2 + "/" + i3 + "/" + i;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,i);
cal.set(Calendar.MONTH,i1);
cal.set(Calendar.DAY_OF_MONTH,i2);
String selectedDate = DateFormat.getDateInstance().format(cal.getTime());
textViewDateValue.setText(selectedDate);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Here is the XML code of the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManageSlots">
<include
layout="@layout/toolbar_layout"
android:id="@+id/mytoolbar"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date:"
android:id="@+id/textViewSelectDate"
android:layout_below="@id/mytoolbar"
android:layout_margin="10dp"
android:textSize="20dp"
android:textColor="#000"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewCalander"
android:src="@drawable/round_today_black_18dp_2x"
android:layout_toRightOf="@id/textViewSelectDate"
android:layout_below="@id/mytoolbar"
android:layout_marginTop="5dp"
android:clickable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewDateValue"
android:layout_below="@id/imageViewCalander"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:text="Aug 21, 2020"
android:textColor="#000"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textViewDateValue"
android:id="@+id/recyclerViewSlots"
android:padding="4dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonAddSlot"
android:text="Add Slot"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="@color/colorPrimaryDark"
android:textColor="#fff"/>
</RelativeLayout>
答案1
得分: 1
如果我理解正确的话,您想要类似这样的代码:
mBuilder.setPositiveButton("完成", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
spinnerSlotValue = mSpinnerSlots.getSelectedItem().toString();
spinnerStartTimeValue = mSpinnerStartTime.getSelectedItem().toString();
spinnerEndTimeValue = mSpinnerEndTime.getSelectedItem().toString();
String finalTime = spinnerStartTimeValue + "-" + spinnerEndTimeValue;
slotDetails.add(new CardViewManageSlots(spinnerSlotValue, finalTime));
mAdapter.notifyDataSetChanged();
dialogInterface.dismiss();
}
});
英文:
If I understand it correctly, you want something like this:
mBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
spinnerSlotValue = mSpinnerSlots.getSelectedItem().toString();
spinnerStartTimeValue = mSpinnerStartTime.getSelectedItem().toString();
spinnerEndTimeValue = mSpinnerEndTime.getSelectedItem().toString();
String finalTime = spinnerStartTimeValue + "-" + spinnerEndTimeValue;
slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
mAdapter.notifyDataSetChanged();
dialogInterface.dismiss();
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论