如何在从对话框获取输入后创建新卡片。

huangapple 未分类评论47阅读模式
英文:

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&lt;AdapterManageSlots.ManageSlotsViewHolder&gt; {

    private ArrayList&lt;CardViewManageSlots&gt; 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&lt;CardViewManageSlots&gt; 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&lt;CardViewManageSlots&gt; slotDetails =  new ArrayList&lt;&gt;();
        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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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&lt;String&gt; adapter1 = new ArrayAdapter&lt;String&gt;(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&lt;String&gt; adapter2 = new ArrayAdapter&lt;String&gt;(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(&quot;Done&quot;, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Toast.makeText(ManageSlots.this, &quot;Slot Created!&quot; + spinnerSlotValue + spinnerEndTimeValue + spinnerStartTimeValue, Toast.LENGTH_LONG).show();
                        dialogInterface.dismiss();
                    }
                });

                mBuilder.setNegativeButton(&quot;Cancel&quot;, 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 + &quot;-&quot; + spinnerEndTimeValue;
        Toast.makeText(this, finalTime, Toast.LENGTH_SHORT).show();
        slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
        //slotDetails.add(new CardViewManageSlots(&quot;Morning&quot;,&quot;diwwodmw&quot;));
        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 + &quot;/&quot; + i3 + &quot;/&quot; + 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&lt;?&gt; adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {

    }
}

Here is the XML code of the activity

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.ManageSlots&quot;&gt;

    &lt;include
        layout=&quot;@layout/toolbar_layout&quot;
        android:id=&quot;@+id/mytoolbar&quot;
        /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Select Date:&quot;
        android:id=&quot;@+id/textViewSelectDate&quot;
        android:layout_below=&quot;@id/mytoolbar&quot;
        android:layout_margin=&quot;10dp&quot;
        android:textSize=&quot;20dp&quot;
        android:textColor=&quot;#000&quot;/&gt;
    
    &lt;ImageView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/imageViewCalander&quot;
        android:src=&quot;@drawable/round_today_black_18dp_2x&quot;
        android:layout_toRightOf=&quot;@id/textViewSelectDate&quot;
        android:layout_below=&quot;@id/mytoolbar&quot;
        android:layout_marginTop=&quot;5dp&quot;
        android:clickable=&quot;true&quot;/&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/textViewDateValue&quot;
        android:layout_below=&quot;@id/imageViewCalander&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginTop=&quot;10dp&quot;
        android:textSize=&quot;20dp&quot;
        android:text=&quot;Aug 21, 2020&quot;
        android:textColor=&quot;#000&quot;
        /&gt;
    &lt;android.support.v7.widget.RecyclerView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_below=&quot;@+id/textViewDateValue&quot;
        android:id=&quot;@+id/recyclerViewSlots&quot;
        android:padding=&quot;4dp&quot;
        /&gt;
    &lt;Button
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/buttonAddSlot&quot;
        android:text=&quot;Add Slot&quot;
        android:layout_alignParentBottom=&quot;true&quot;
        android:layout_margin=&quot;10dp&quot;
        android:background=&quot;@color/colorPrimaryDark&quot;
        android:textColor=&quot;#fff&quot;/&gt;
&lt;/RelativeLayout&gt;

答案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(&quot;Done&quot;, 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 + &quot;-&quot; + spinnerEndTimeValue;
           slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
           mAdapter.notifyDataSetChanged();
           dialogInterface.dismiss();
     }
});

huangapple
  • 本文由 发表于 2020年5月2日 15:34:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61555906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定