带有自定义适配器的列表视图未显示所有项目

问题描述 投票:0回答:2

我正在做一个项目,其中包括一系列有帮助的学生问题在于,加载列表视图时,并非所有元素都出现在列表视图中,只有2个元素出现在列表视图中。我要显示的是一个列表视图,其中每个文本视图上有2个微调器和1个textview,具有de name of students,其数组为“ noms”

MainActivity

package com.example.llista_alumnes;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.Arrays;



public class MainActivity extends Activity {

    public final static int REQUEST_CODE_B = 1;
    public final static int REQUEST_CODE_C = 2;

    private TextView tv1;
    private ListView lv1;
    private Spinner spinner ;
    private Toolbar toolbar;
    private Custom_listview_adapter adapter;


    private ArrayList noms =new ArrayList <String> (Arrays.asList("Samuel", "Valentina", "Santiago", "Alejandro", "Valeria", "Benjamin",
            "Gerardo", "Carlos", "David", "Sofía"));


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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the main; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.add_estudiants:
                addEstudiant();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void inicialitza() {
        tv1 = findViewById(R.id.tv1);
        lv1 = findViewById(R.id.lv1);
        toolbar = findViewById(R.id.toolbar);
        setActionBar(toolbar);
        adapter = new Custom_listview_adapter(this,R.layout.adapter_list_view, R.id.lv1, noms);
        lv1.setAdapter(adapter);

    }

    void addEstudiant(){
        Intent intent = new Intent(getBaseContext(), second_activity.class );
        startActivityForResult(intent, REQUEST_CODE_B);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case REQUEST_CODE_B:
                noms.add(data.getStringExtra("name"));
                adapter.notifyDataSetChanged();
                break;

        }
    }
}

旋转适配器

package com.example.llista_alumnes;

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

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


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

public class Custom_spinner_adapter extends ArrayAdapter implements SpinnerAdapter {
    private  Context context;
    private int textResourceId;
    private int resource;
    private String [] data;

    public Custom_spinner_adapter(Context context, int resource,  int textViewResourceId, String [] 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.length;
    }

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

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



    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return init_view(position,convertView,parent);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
         return init_view(position,convertView,parent);
    }

    public View init_view(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if (view == null){
            view = ((Activity) context).getLayoutInflater().inflate( R.layout.adapter_spinner, null);
        }

        TextView spinner = view.findViewById(R.id.tv_spinner);
        spinner.setText(data[position]);

        return view;
    }
}

列表视图适配器

package com.example.llista_alumnes;

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.spinner_array.length;
    }

    @Override
    public Object getItem(int position){
        return this.spinner_array[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;

    }




}

https://i.stack.imgur.com/MGU7g.png

java android android-listview android-adapter
2个回答
0
投票

在Custom_listview_adapter更改中

@Override
public int getCount(){
    return this.spinner_array.length;
}

to

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

0
投票

完成了所有必要的更改之后,现在显示所有项目-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;

    }


}
© www.soinside.com 2019 - 2024. All rights reserved.