首页 > 解决方案 > 列表视图显示所有项目减去最后一个

问题描述

在完成所有这些之后,我对类进行了所有必要的更改以显示所有项目,但现在显示所有项目 -1

这是列表视图的自定义适配器,问题是显示所有项目 -1 而不是所有项目,在添加 1 显示所有项目后,添加另一个显示全部加上前面添加的项目

列表视图适配器


import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;



// aquest custom adapter es pel list_view no pel spinner
// aqui pots interectuar amb spinner usant al funcio del getview

public class Custom_listview_adapter extends ArrayAdapter {
    Context context;
    private String []  spinner_array = new String [] {"ha vingut","Ha faltat"};
    private String [] notas_array = new String [] {"0","1","2","3","4","5","6","7","8","9","10"};
    private int textResourceId;
    private int resource;
    private ArrayList<String> data;


    public Custom_listview_adapter(Context context, int resource, int textViewResourceId, ArrayList data){
        super(context,resource,textViewResourceId,data);
        this.context = context;
        this.textResourceId = textViewResourceId;
        this.resource = resource;
        this.data = data;

    }

    @Override
    public int getCount(){
        return this.data.size();
    }

    @Override
    public Object getItem(int position){
        return this.data.get(position);
    }

    @Override
    public long getItemId( int position){
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View view = convertView;
        if (view == null){
            view = ((Activity) context).getLayoutInflater().inflate( R.layout.adapter_list_view, null);
        }

        TextView textView = (TextView) view.findViewById(R.id.name);
        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
        Spinner notas = (Spinner) view.findViewById(R.id.notas);

        textView.setText(data.get(position));
        Custom_spinner_adapter adapter_notas = new Custom_spinner_adapter(context, R.layout.adapter_spinner,R.id.notas, this.notas_array);
        notas.setAdapter(adapter_notas);
        Custom_spinner_adapter adapter = new Custom_spinner_adapter(context, R.layout.adapter_spinner,R.id.spinner, this.spinner_array);
        spinner.setAdapter(adapter);

        return view;

    }


}
<androidx.constraintlayout.widget.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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="20dp" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        app:layout_constraintTop_toBottomOf="@+id/name"
        tools:layout_editor_absoluteX="20dp" />

    <Spinner
        android:id="@+id/notas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:padding="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner" />

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.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:id="@+id/constraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv1"
        />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/llista_alumnes"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>```

标签: javaandroidandroid-listviewcustom-adapter

解决方案


试试这样:

适配器列表视图.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adapter_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/notas1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="adapter_list_view" />

</LinearLayout>

模态.java

public class Modal {
    private String name;
    private String[] spinner;
    private String[] notas;

    Modal(@NonNull String name, @NonNull String[] spinner, @NonNull String[] notas) {
        this.name = name;
        this.spinner = spinner;
        this.notas = notas;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getSpinner() {
        return spinner;
    }

    public void setSpinner(String[] spinner) {
        this.spinner = spinner;
    }

    public String[] getNotas() {
        return notas;
    }

    public void setNotas(String[] notas) {
        this.notas = notas;
    }
}

模态适配器.java

public class ModalAdapter extends ArrayAdapter<Modal> {
    private String name;
    private String[] spinnerArrays;
    private String[] notasArrays;
    private List<Modal> modalList;

    public ModalAdapter(@NonNull Context context, @NonNull List<Modal> modalList) {
        super(context, 0, modalList);
        this.modalList = modalList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Nullable
    @Override
    public Modal getItem(int position) {
        return modalList.get(position);
    }

    @Override
    public int getCount() {
        return modalList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_list_view, parent, false);
        }

        TextView textView = convertView.findViewById(R.id.textview1);
        Spinner spinner = convertView.findViewById(R.id.spinner1);
        Spinner notas = convertView.findViewById(R.id.notas1);

        // Get the data item for this position
        Modal modal = getItem(position);
        if (modal != null) {
            name = modal.getName();
            textView.setText(name);

            spinnerArrays = modal.getSpinner();
            spinner.setAdapter(new ArrayAdapter<>(getContext(), R.layout.support_simple_spinner_dropdown_item, spinnerArrays));

            notasArrays = modal.getNotas();
            notas.setAdapter(new ArrayAdapter<>(getContext(), R.layout.support_simple_spinner_dropdown_item, notasArrays));
        }

        // Return the completed view to render on screen
        return convertView;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    // item 1
    private String[] spinner_array1 = new String[]{"a", "b"};
    private String[] notas_array1 = new String[]{"0", "1"};

    // item 2
    private String[] spinner_array2 = new String[]{"c", "d"};
    private String[] notas_array2 = new String[]{"3", "4"};

    // item 3
    private String[] spinner_array3 = new String[]{"e", "f"};
    private String[] notas_array3 = new String[]{"5", "6"};

    // item 4
    private String[] spinner_array4 = new String[]{"g", "h"};
    private String[] notas_array4 = new String[]{"7", "8"};

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

        // listView
        ListView listView = findViewById(R.id.lv1);

        // adapter
        List<Modal> modalList = new ArrayList<>();
        modalList.add(new Modal("item1", spinner_array1, notas_array1)); // item 1
        modalList.add(new Modal("item2", spinner_array2, notas_array2)); // item 2
        modalList.add(new Modal("item3", spinner_array3, notas_array3)); // item 3
        modalList.add(new Modal("item4", spinner_array4, notas_array4)); // item 4
        ModalAdapter adapter = new ModalAdapter(this, modalList);

        // set listView adapter
        listView.setAdapter(adapter);
    }
}

推荐阅读