Android studio 如何向列表视图添加上一个和下一个按钮

Android studio 如何向列表视图添加上一个和下一个按钮,android-studio,for-loop,listview,adapter,Android Studio,For Loop,Listview,Adapter,我目前有一个listview,它可以保存无限量的输入,但我只希望它一次保存5行,然后用户可以选择next按钮,将listview移动到下5行,然后可以选择previous以查看前5行。我目前不确定如何做到这一点,因此任何指导都将非常有用。我想也许我可以使用一个以位置为起点,以位置+10为终点的for循环(也就是说,循环将以for(int I=position;I

我目前有一个listview,它可以保存无限量的输入,但我只希望它一次保存5行,然后用户可以选择next按钮,将listview移动到下5行,然后可以选择previous以查看前5行。我目前不确定如何做到这一点,因此任何指导都将非常有用。我想也许我可以使用一个以位置为起点,以位置+10为终点的for循环(也就是说,循环将以for(int I=position;I InventoryStatus类: 公共类InventoryStatus扩展了AppCompatActivity{ 数据库助手myDB

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


    myDB = new DatabaseHelper(this);
    ListView listView = (ListView) findViewById(R.id.listViewStock);
    Button btNext = findViewById(R.id.buttonNext);
    Button btPrevious = findViewById(R.id.buttonPrevious);

    ArrayList<String> theList = new ArrayList<>();
    Cursor data = myDB.getAllData();

    if(data.getCount() == 0 ){
        Toast.makeText(InventoryStatus.this, "No data found", Toast.LENGTH_SHORT).show();
    }else{
        while (data.moveToNext()){
            theList.add("Name: " + data.getString(0) + " Quantity: " + data.getString(1) + " Type: " + data.getString(2));
            ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
            listView.setAdapter(listAdapter);
        }
    }
    btNext.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

        }
    });
    btPrevious.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

        }
    });

}

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u inventory\u status);
myDB=新数据库助手(此);
ListView ListView=(ListView)findViewById(R.id.listViewStock);
按钮btNext=findViewById(R.id.buttonNext);
按钮btPrevious=findViewById(R.id.buttonPrevious);
ArrayList theList=新的ArrayList();
游标数据=myDB.getAllData();
if(data.getCount()==0){
Toast.makeText(InventoryStatus.this,“未找到数据”,Toast.LENGTH_SHORT.show();
}否则{
while(data.moveToNext()){
添加(“名称:”+data.getString(0)+“数量:”+data.getString(1)+“类型:”+data.getString(2));
ListAdapter ListAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,列表);
setAdapter(listAdapter);
}
}
btNext.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
}
});
btPrevious.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
}
});
}
}
活动:

<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/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InventoryStatus">

<ListView
    android:id="@+id/listViewStock"
    android:layout_width="395dp"
    android:layout_height="359dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textViewInventoryStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/inventory_status"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.549"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.037" />

<Button
    android:id="@+id/buttonNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.95"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.967" />

<Button
    android:id="@+id/buttonPrevious"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/previous"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.049"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.967" />
 </androidx.constraintlayout.widget.ConstraintLayout>


Listview已被弃用,我不鼓励您改用iTunes recyclerview。在这两种情况下,您都可以定义arraylist来保存整个数据,然后使用当前要显示的5项更新适配器。或者代替arraylist,您可以每次直接从本地数据库中选择5项,并在查询中使用偏移量和限制来移动下一步和下一步previous@RamyMalak因此,使用带有“上一步”和“下一步”按钮的列表视图将无法工作?它可以工作,但不能recommended@RamyMalak是的,这更多的是为了测试它,看看我是否能得到它要以某种方式使用列表视图。。。你知道我该怎么做吗?