将下一个和上一个按钮添加到 ListView

Posted

技术标签:

【中文标题】将下一个和上一个按钮添加到 ListView【英文标题】:Adding next and previous buttons to a ListView 【发布时间】:2021-01-24 02:32:56 【问题描述】:

我试图让我的列表视图显示 5 行,当按下下一个按钮时,它会显示下一个 5,当单击上一个按钮时,它会显示前 5 个。由于某种原因,它无法正常工作它的日志应用程序,所以如果有人能告诉我哪里出错了,那将非常有帮助。谢谢

InventoryStatus 类:

public class InventoryStatus extends AppCompatActivity 
DatabaseHelper myDB;
int position = 0;

@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);
            
            for(int i = position; i < position + 5; i++)
                theList.get(i);
            
        
    
    btNext.setOnClickListener(new View.OnClickListener() 

        public void onClick(View view) 

            position += 5;
        
    );
    btPrevious.setOnClickListener(new View.OnClickListener() 

        public void onClick(View view) 

            position -= 5;
        
    );

 



活动:

<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_
android:layout_
tools:context=".InventoryStatus">

<ListView
    android:id="@+id/listViewStock"
    android:layout_
    android:layout_
    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_
    android:layout_
    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_
    android:layout_
    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_
    android:layout_
    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>

【问题讨论】:

【参考方案1】:

检查我编写的这个示例项目(只需评论您的 main 并添加它,它将与您的布局一起使用)

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class InventoryStatus extends AppCompatActivity

    private ArrayList<String> fullList; // list which keeps all data from DB
    private ArrayList<String> displayedList; // list which is currently displayed in ListView
    private int currentPos = 0; // position in `fullList`
    ArrayAdapter<String> listAdapter;
    ListView listView;

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

        listView = findViewById(R.id.listViewStock);
        Button btNext = findViewById(R.id.buttonNext);
        Button btPrevious = findViewById(R.id.buttonPrevious);

        fullList = new ArrayList<>();
        displayedList = new ArrayList<>();

        // Load data from db to fullList (I am not using db to show example)

        for (int i = 0; i < 23; i++)
        
            fullList.add("Name " + i);
        

        if (fullList.size() == 0)
        
            Toast.makeText(this, "No data found", Toast.LENGTH_SHORT).show();
        
        else // fullList isn't empty show first 5 items in ListView
        
            setDisplayListPosition();
            listView.setAdapter(listAdapter);
        

        btNext.setOnClickListener(new View.OnClickListener()
        
            public void onClick(View view) // change current position and refresh ListView
            
                currentPos += 5;
                if (currentPos > fullList.size()) currentPos -= 5;
                else
                    setDisplayListPosition();
            
        );

        btPrevious.setOnClickListener(new View.OnClickListener() // change current position and refresh ListView
        
            public void onClick(View view)
            
                currentPos -= 5;
                if (currentPos < 0) currentPos = 0;
                else
                    setDisplayListPosition();
            
        );
    

    private void setDisplayListPosition() // helper function which refresh ListView based on `currentPos`
    
        displayedList.clear(); 

        for (int i = 0; i < 5 && i + currentPos < fullList.size(); i++)
        
            displayedList.add(fullList.get(currentPos + i)); // ad 5 items from finalList to displayedList
         

        listAdapter = new ArrayAdapter<String>(this,
                                               android.R.layout.simple_list_item_1,
                                               displayedList
        );

        listView.setAdapter(listAdapter);
    

【讨论】:

谢谢!我如何将我的数据库添加到 fullList?我尝试了完整列表。 add("名称" + i + "数量" + i + "类型" + i);但这不起作用 如果这回答正确,那么接受它作为答案并提出一个新问题。

以上是关于将下一个和上一个按钮添加到 ListView的主要内容,如果未能解决你的问题,请参考以下文章

将下一个和上一个按钮添加到我的图像滑块

将下一个按钮添加到 php 分页脚本。它简短而简单

在模态中的wordpress帖子中添加下一个和上一个按钮

更改“光滑”滑块中的箭头按钮

colorbox:如何禁用下一个和上一个按钮

滚动库中的下一个和上一个按钮中的错误