Android Spinner

时间:2018-05-18 11:44:45

标签: java android json api

我暴露了我的问题。我已经创建了一个REST API,现在我在Android APP中使用它。有问题的申请试图让学生缺少帮助(这是我在课堂上做的一个班级项目)。

在我的应用程序中,当选择主题和注册的学生时,您必须选择错过课程的开始时间。我完成它的方式允许我选择一个预定义的时间,我想采取一个对象属性的起始时间,并将它们放在一个预定义的Android适配器之一的微调器(android.R.layout) .simple_spinner_item或android.R.layout.simple_spinner_dropdown_item)。

我有一个AsynkTask类,它负责从API获取“schedule”对象,并且在这些对象中是我想在Spinner中显示的String类型的“Start”属性。

问题是,有问题的用户 - 教师只需在Spinner中选择一个预定义的开始时间,然后按下缺失按钮添加故障。因此,当在微调器上选择特定时间时,应该存储与所讨论的“开始”相关的“ID”属性,在要在按钮的操作旁边发送的变量内。 (创建错误可以正常工作,但我只尝试使用固定ID)。

这是我的代码

活动

public class FaltasActivity extends AppCompatActivity implements OnFaltasReturn, OnAlumnoReturn, OnHorarioReturn {

private TextView nombre, apellidos, correo, titleFault;

private Toolbar toolbar;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;

private ArrayList<Falta> listaFaltas;
private ArrayList<Horario> listaHorario;
private List<String> list;
private FaltasAdapter adapter;
private ArrayAdapter<String> spinnerAdapter;

private Integer idModulo, idAlumno, idHorario;
private Button falta;
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_faltas);

    toolbar = findViewById(R.id.toolbar_faltas);
    titleFault = findViewById(R.id.toolbar_title_fault);
    titleFault.setText("Faltas");
    setSupportActionBar(toolbar);


    // SPINNER I think the spinner is well implemented ... but I can not add the objects to the lists correctly.
    spinner = findViewById(R.id.spinner);

    listaHorario = new ArrayList<>();
    list = new ArrayList<>();

    HorarioATask horarioATask = new HorarioATask(this, this);
    horarioATask.execute();

    for (int i = 0; i < listaHorario.size(); i++) {
        list.add(listaHorario.get(i).getInicio());
    }

    spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(spinnerAdapter);

    //Finish SPINNER code

    idModulo = getIntent().getExtras().getInt("ID_MODULO");
    idAlumno = getIntent().getExtras().getInt("ID_ALUMNO");
    idHorario = 1;  //This is the variable to be replaced when a start is selected.

    nombre = findViewById(R.id.item_tittle);
    apellidos = findViewById(R.id.second_item);
    correo = findViewById(R.id.third_item);

    mRecyclerView = findViewById(R.id.rvf);

    AlumnoATask alumnoATask = new AlumnoATask(this, this, idAlumno);
    alumnoATask.execute();

    listaHorario = new ArrayList<>();

    falta = findViewById(R.id.action_button_1);
    falta.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    Gson gson = new Gson();
                    final ApiConnector apiConnector = new ApiConnector();
                    final String URI_POST = "faltas";
                    String token;

                    String uri = URI_POST;
                    String method = "POST";
                    String json;

                    Alumno alumno = new Alumno(idAlumno);
                    Modulo modulo = new Modulo(idModulo);
                    Horario horario = new Horario(idHorario);

                    Falta falta = new Falta(alumno, horario, modulo);

                    json = gson.toJson(falta, Falta.class);

                    SharedPreferences prefs = getSharedPreferences("userObject", Context.MODE_PRIVATE);
                    token = prefs.getString("token", "");

                    apiConnector.consult(uri, method, json, token);

                }
            });
        }
    });

    listaFaltas = new ArrayList<>();

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setHasFixedSize(true);
    adapter = new FaltasAdapter(this, listaFaltas);
    mRecyclerView.setAdapter(adapter);
    mRecyclerView.setLayoutManager(mLayoutManager);

    FaltaATask faltaATask = new FaltaATask(this, this, idAlumno, idModulo);
    faltaATask.execute();

}

@Override
public void onFaltasConsultados(ArrayList<Falta> listaFaltas) {
    this.listaFaltas.clear();
    this.listaFaltas.addAll(listaFaltas);
    this.adapter.notifyDataSetChanged();
}

@Override
public void onFaltasError() {
    Toast.makeText(this, "Error en el listado de faltas", Toast.LENGTH_SHORT).show();
}

@Override
public void onAlumnosConsultados(Alumno alumno) {
    this.nombre.setText(alumno.getNombre());
    this.apellidos.setText(alumno.getApellidos());
    this.correo.setText(alumno.getCorreo());
}

@Override
public void onAlumnosError() {
    Toast.makeText(this, "Error en los datos del alumno", Toast.LENGTH_SHORT).show();
}

//In theory this fills the lists (Interface)
@Override
public void onHorarioConsultados(ArrayList<Horario> listaHorario) {
    this.listaHorario.clear();
    this.listaHorario.addAll(listaHorario);
    this.adapter.notifyDataSetChanged();
}

@Override
public void onHorarioError() {
    Toast.makeText(this, "Error con los horarios", Toast.LENGTH_SHORT).show();
}

}

模型对象

public class Horario implements java.io.Serializable {

private Integer idHorario;
private String inicio;
private String fin;
private Date alta;
private Date baja;

public Horario() {
}

public Horario(Integer idHorario) {
    this.idHorario = idHorario;
}

public Horario(String inicio, String fin, Date alta) {
    this.inicio = inicio;
    this.fin = fin;
    this.alta = alta;
}

public Horario(Integer idHorario, String inicio, String fin, Date alta, Date baja) {
    super();
    this.idHorario = idHorario;
    this.inicio = inicio;
    this.fin = fin;
    this.alta = alta;
    this.baja = baja;
}

public Integer getIdHorario() {
    return this.idHorario;
}

public void setIdHorario(Integer idHorario) {
    this.idHorario = idHorario;
}

public String getInicio() {
    return this.inicio;
}

public void setInicio(String inicio) {
    this.inicio = inicio;
}

public String getFin() {
    return this.fin;
}

public void setFin(String fin) {
    this.fin = fin;
}

public Date getAlta() {
    return this.alta;
}

public void setAlta(Date alta) {
    this.alta = alta;
}

public Date getBaja() {
    return this.baja;
}

public void setBaja(Date baja) {
    this.baja = baja;
}

}

HorarioATask(AsynkTask)

public class HorarioATask extends AsyncTask<Void, Void, ArrayList<Horario>> {

private Context context;
private OnHorarioReturn mListener;

public HorarioATask(OnHorarioReturn mListener, Context context){
    this.mListener = mListener;
    this.context = context;
}

@Override
protected ArrayList<Horario> doInBackground(Void... voids) {

    ArrayList<Horario> listaHorarios = new ArrayList<>();

    final String URI_GET = "horarios";

    Gson gson = new Gson();
    String token;

    String uri;
    String method;
    String json = null;

    final ApiConnector apiConnector = new ApiConnector();

    SharedPreferences prefs = context.getSharedPreferences("userObject", Context.MODE_PRIVATE);
    token = prefs.getString("token", "");

    uri = URI_GET;
    method = "GET";

    String response = apiConnector.consult(uri, method, json, token);
    Type tT = new TypeToken<ArrayList<Horario>>() {}.getType();

    if (response.equals("401")){
        context.startActivity(new Intent(context, LoginActivity.class));
    } else {
        listaHorarios = gson.fromJson(response,tT);
    }

    return listaHorarios;
}

@Override
protected void onPostExecute(ArrayList<Horario> listaHorarios) {
    super.onPostExecute(listaHorarios);

    if (listaHorarios == null) {
        mListener.onHorarioError();
    } else {
        mListener.onHorarioConsultados(listaHorarios);
    }
}

}

OnHorarioReturn(接口)

public interface OnHorarioReturn {

void onHorarioConsultados(ArrayList<Horario> listaHorario);

void onHorarioError();

}

我认为它与RecyclerView类似,但是我已经尝试了,但我一直没能,它没有显示任何内容。

View of the activity in question

activity.xml

<android.support.constraint.ConstraintLayout 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"
android:fitsSystemWindows="@bool/fitsSystemWindows"
android:orientation="vertical"
tools:context=".FaltasActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_faltas"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ToolbarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <TextView
        android:id="@+id/toolbar_title_fault"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite" />

</android.support.v7.widget.Toolbar>

<android.support.v7.widget.CardView
    android:id="@+id/fault_card"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/Toolbar"
    android:fitsSystemWindows="@bool/fitsSystemWindows">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/item_tittle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    android:ellipsize="marquee"
                    android:singleLine="true"
                    android:text="Nombre"
                    android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                    android:textColor="@color/colorPrimaryTextDefaultMaterialLight" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/second_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:singleLine="true"
                    android:text="Apellidos"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textColor="@color/colorSecondaryTextDefaultMaterialLight" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/third_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:singleLine="true"
                    android:text="Correo electrónico"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    android:textColor="@color/colorSecondaryTextDefaultMaterialLight" />
            </LinearLayout>

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="8dp" />

            <Button
                android:id="@+id/action_button_1"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:minWidth="0dp"
                android:paddingBottom="8dp"
                android:paddingEnd="16dp"
                android:paddingStart="16dp"
                android:paddingTop="8dp"
                android:text="Falta"
                android:textColor="@color/colorPrimaryTextDefaultMaterialDark"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvf"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:0)

尝试:

public class FaltasActivity extends AppCompatActivity implements OnFaltasReturn, OnAlumnoReturn, OnHorarioReturn {

private TextView nombre, apellidos, correo, titleFault;

private Toolbar toolbar;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;

private ArrayList<Falta> listaFaltas;
private ArrayList<Horario> listaHorario;
private List<String> list;
private FaltasAdapter adapter;
private ArrayAdapter<String> spinnerAdapter;

private Integer idModulo, idAlumno, idHorario;
private Button falta;
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_faltas);

    toolbar = findViewById(R.id.toolbar_faltas);
    titleFault = findViewById(R.id.toolbar_title_fault);
    titleFault.setText("Faltas");
    setSupportActionBar(toolbar);


    // SPINNER I think the spinner is well implemented ... but I can not add the objects to the lists correctly.
    spinner = findViewById(R.id.spinner);

    listaHorario = new ArrayList<>();
    list = new ArrayList<>();

    HorarioATask horarioATask = new HorarioATask(this, this);
    horarioATask.execute();


    //Finish SPINNER code

    idModulo = getIntent().getExtras().getInt("ID_MODULO");
    idAlumno = getIntent().getExtras().getInt("ID_ALUMNO");
    idHorario = 1;  //This is the variable to be replaced when a start is selected.

    nombre = findViewById(R.id.item_tittle);
    apellidos = findViewById(R.id.second_item);
    correo = findViewById(R.id.third_item);

    mRecyclerView = findViewById(R.id.rvf);

    AlumnoATask alumnoATask = new AlumnoATask(this, this, idAlumno);
    alumnoATask.execute();

    listaHorario = new ArrayList<>();

    falta = findViewById(R.id.action_button_1);
    falta.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    Gson gson = new Gson();
                    final ApiConnector apiConnector = new ApiConnector();
                    final String URI_POST = "faltas";
                    String token;

                    String uri = URI_POST;
                    String method = "POST";
                    String json;

                    Alumno alumno = new Alumno(idAlumno);
                    Modulo modulo = new Modulo(idModulo);
                    Horario horario = new Horario(idHorario);

                    Falta falta = new Falta(alumno, horario, modulo);

                    json = gson.toJson(falta, Falta.class);

                    SharedPreferences prefs = getSharedPreferences("userObject", Context.MODE_PRIVATE);
                    token = prefs.getString("token", "");

                    apiConnector.consult(uri, method, json, token);

                }
            });
        }
    });

    listaFaltas = new ArrayList<>();

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setHasFixedSize(true);
    adapter = new FaltasAdapter(this, listaFaltas);
    mRecyclerView.setAdapter(adapter);
    mRecyclerView.setLayoutManager(mLayoutManager);

    FaltaATask faltaATask = new FaltaATask(this, this, idAlumno, idModulo);
    faltaATask.execute();

}

@Override
public void onFaltasConsultados(ArrayList<Falta> listaFaltas) {
    this.listaFaltas.clear();
    this.listaFaltas.addAll(listaFaltas);
    this.adapter.notifyDataSetChanged();

     for (int i = 0; i < listaHorario.size(); i++) {
        list.add(listaHorario.get(i).getInicio());
    }

    spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(spinnerAdapter);

}

@Override
public void onFaltasError() {
    Toast.makeText(this, "Error en el listado de faltas", Toast.LENGTH_SHORT).show();
}

@Override
public void onAlumnosConsultados(Alumno alumno) {
    this.nombre.setText(alumno.getNombre());
    this.apellidos.setText(alumno.getApellidos());
    this.correo.setText(alumno.getCorreo());
}

@Override
public void onAlumnosError() {
    Toast.makeText(this, "Error en los datos del alumno", Toast.LENGTH_SHORT).show();
}

//In theory this fills the lists (Interface)
@Override
public void onHorarioConsultados(ArrayList<Horario> listaHorario) {
    this.listaHorario.clear();
    this.listaHorario.addAll(listaHorario);
    this.adapter.notifyDataSetChanged();
}

@Override
public void onHorarioError() {
    Toast.makeText(this, "Error con los horarios", Toast.LENGTH_SHORT).show();
}

}