列表视图显示所有项目减去最后一个
Posted
技术标签:
【中文标题】列表视图显示所有项目减去最后一个【英文标题】:list view show all items minus the last 【发布时间】:2020-04-21 05:33:44 【问题描述】:完成所有这些后,我对类进行了所有必要的更改以显示所有项目,但现在显示所有项目 -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_
android:layout_
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/name"
android:layout_
android:layout_
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="20dp" />
<Spinner
android:id="@+id/spinner"
android:layout_
android:layout_
android:padding="2dp"
app:layout_constraintTop_toBottomOf="@+id/name"
tools:layout_editor_absoluteX="20dp" />
<Spinner
android:id="@+id/notas"
android:layout_
android:layout_
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_
android:layout_>
<ListView
android:id="@+id/lv1"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1"
/>
<TextView
android:id="@+id/tv1"
android:layout_
android:layout_
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>```
【问题讨论】:
也许你没有在底部正是你的工具栏所采用的大小。在 Viewpager 或 RecyclerView 中添加 android:layout_marginBottom="?attr/actionBarSize"。看看你的 xml。 我正在使用的是一个列表视图,我放了一个没有什么不同的地方 粘贴您的adapter_list_view.xml 请粘贴您的 xml 文件 现在完成了,谢谢您的建议 【参考方案1】:试试这样:
adapter_list_view.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_
android:layout_
android:layout_margin="16dp"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textview1"
android:layout_
android:layout_/>
<Spinner
android:id="@+id/spinner1"
android:layout_
android:layout_ />
<Spinner
android:id="@+id/notas1"
android:layout_
android:layout_ />
</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_
android:layout_
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv1"
android:layout_
android:layout_
tools:listitem="adapter_list_view" />
</LinearLayout>
Modal.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;
ModalAdapter.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);
【讨论】:
以上是关于列表视图显示所有项目减去最后一个的主要内容,如果未能解决你的问题,请参考以下文章
在 AVQueuePlayer 中播放完最后一项后如何显示工作表视图?
Sharepoint Online / 365 - 从列表视图中删除多个附加列和显示条目的“查看条目”