Android分页列表视图

Posted

技术标签:

【中文标题】Android分页列表视图【英文标题】:Android Pagination ListView 【发布时间】:2015-01-06 13:46:23 【问题描述】:

您好,我无法在 android 中实现 listview 的分页。我的任务是将值从editText 添加到ListView,我需要在列表中添加分页。但我试过了,我只插入一个值。当我尝试添加下一个值时,我最终会出错。请告诉我我的代码中的错误。我添加了我的布局、活动和日志

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:background="@drawable/agnes2_back"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vivek.projectone.MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_
        android:layout_
        android:background="@drawable/border" >

        <TextView
            android:id="@+id/textView1"
            android:layout_
            android:layout_
            android:layout_marginLeft="65dp"
            android:layout_marginTop="15dp"
            android:gravity="right"
            android:text="@string/welcome"
            android:textColor="#58FA58" />

        <TextView
            android:id="@+id/userView"
            android:layout_
            android:layout_
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:text="@string/userLabel"
            android:textColor="#FF0000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_
        android:layout_
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="14dp"
        android:background="@drawable/border"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/itemName"
            android:layout_
            android:layout_
            android:ems="10"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/addButton1"
            style="?android:attr/buttonStyleSmall"
            android:layout_
            android:layout_
            android:background="@drawable/adds"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_
        android:layout_
        android:layout_alignLeft="@+id/linearlayout2"
        android:layout_below="@+id/linearlayout2"
        android:layout_marginTop="16dp"
        android:background="@drawable/border"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listView1"
            android:layout_
            android:layout_
            android:layout_weight="2.32"
            android:background="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent"
            tools:listitem="@android:layout/simple_list_item_checked" >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_alignLeft="@+id/linearLayout3"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/linearLayout3"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Prev"
            style="?android:attr/buttonStyleSmall"
            android:layout_
            android:layout_
            android:layout_marginLeft="35dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/buttonbackground"
            android:text="@string/btn_prev" />

        <Button
            android:id="@+id/btn_Next"
            style="?android:attr/buttonStyleSmall"
            android:layout_
            android:layout_
            android:layout_marginLeft="50dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/buttonbackground"
            android:text="@string/btn_next" />
    </LinearLayout>

</RelativeLayout>

MainActivity.java

public class MultipleActivity extends Activity implements OnItemClickListener 
Button addToList;
EditText viewListItem1, viewListItem2;
ListView customItemList;
PackageManager packageManager;
ArrayList<String> checkedCustomItem;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multiple);
    addToList = (Button) findViewById(R.id.adToListBtn);
    viewListItem1 = (EditText) findViewById(R.id.viewEditItem1);
    viewListItem2 = (EditText) findViewById(R.id.viewEditItem2);
    packageManager = getPackageManager();
    final List<PackageInfo> packageList = packageManager
public class MainActivity extends Activity 
String userLabel;
EditText itemName;
Button addBut;
Button multipleBtn;
ListView itemList;
private ArrayList<String> itemAList;
ArrayAdapter<String> itemAdapter;
private int pageCount;
private Button buttonPrev;
private Button buttonNext;
private int increment = 0;
public int TOTAL_LIST_ITEMS = 1030;
public int NUM_ITEMS_PAGE = 5;
String item;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = (TextView) findViewById(R.id.userView);
    itemName = (EditText) findViewById(R.id.itemName);
    addBut = (Button) findViewById(R.id.addButton1);
    buttonNext = (Button) findViewById(R.id.btn_Next);
    buttonPrev = (Button) findViewById(R.id.btn_Prev);
    itemList = (ListView) findViewById(R.id.listView1);
    buttonPrev.setEnabled(false);
    multipleBtn = (Button) findViewById(R.id.multipleValsBtn);
    int val = TOTAL_LIST_ITEMS % NUM_ITEMS_PAGE;
    val = val == 0 ? 0 : 1;
    pageCount = TOTAL_LIST_ITEMS / NUM_ITEMS_PAGE + val;

    Intent intent = getIntent();
    userLabel = intent.getExtras().getString("emailID");
    textView.setText(userLabel);
    itemAList = new ArrayList<>();
    itemAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, itemAList);
    itemList.setAdapter(itemAdapter);
    itemList.setOnItemClickListener(new OnItemClickListener() 

        @Override
        public void onItemClick(AdapterView parent, View v, int arg2,
                long arg3) 
            // TODO Auto-generated method stub
            item = itemAList.get(arg2);
            Toast.makeText(getApplicationContext(), item, 0).show();
        

    );
    buttonNext.setOnClickListener(new OnClickListener() 

        public void onClick(View v) 

            increment++;
            loadList(increment);
            CheckEnable();
        
    );

    buttonPrev.setOnClickListener(new OnClickListener() 

        public void onClick(View v) 

            increment--;
            loadList(increment);
            CheckEnable();
        
    );

    addBut.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            item = itemName.getText().toString();
            itemAList.add(0, item);
            loadList(0);
            itemName.setText("");
        

    );
    multipleBtn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View arg0) 
            Intent intent = new Intent(MainActivity.this,
                    MultipleActivity.class);
            startActivity(intent);

        
    );


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; 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) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
        return true;
    
    return super.onOptionsItemSelected(item);


private void CheckEnable() 
    if (increment + 1 == pageCount) 
        buttonNext.setEnabled(false);
     else if (increment == 0) 
        buttonPrev.setEnabled(false);
     else 
        buttonPrev.setEnabled(true);
        buttonNext.setEnabled(true);
    


private void loadList(int number) 
    ArrayList<String> sort = new ArrayList<String>();
    int start = number * NUM_ITEMS_PAGE;
    for (int i = start; i < (start) + NUM_ITEMS_PAGE; i++) 
        if (i < itemAList.size()) 
            sort.add(itemAList.get(i));
         else 
            break;
        
    
    itemAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, sort);
    itemList.setAdapter(itemAdapter);

【问题讨论】:

Vivek,你遇到了哪个错误?请在此处发布之前仔细检查您的问题。 是的,正如我所提到的,我无法添加多个字符串 你的问题解决了吗?? 不,我只能在列表视图中添加值,滚动和分页不起作用。您能否建议将列表限制为 5 个项目并使用上述代码实现分页 那你为什么接受这个答案??它误导了用户。您是否使用任何库进行分页? 【参考方案1】:

您只需使用一次setAdapter。下次更改列表项(用于加载接下来的 5 项),您只需要使用 next notifyDataSetChanged

private void loadList(int number) 
        ArrayList<String> sort = new ArrayList<String>();
        int start = number * NUM_ITEMS_PAGE;
        for (int i = start; i < (start) + NUM_ITEMS_PAGE; i++) 
            if (i < itemAList.size()) 
                sort.add(itemAList.get(i));
             else 
                break;
            
        
       if(itemAdapter ==null)
            itemAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, sort);
            itemList.setAdapter(itemAdapter);
       
       else
            itemAdapter.notifyDataSetChanged();
       

【讨论】:

【参考方案2】:

虽然我没有得到你在做什么,但尝试做这个改变: 删除此行

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

或添加在此行下方再添加一行

itemList.setAdapter(itemAdapter);

因为你已经调用了 :itemAdapter.notifyDataSetChanged(); 它应该更新列表。

【讨论】:

是的,我添加了它,但按照你说的删除了,但仍然无法添加值 我可以添加值,但分页不起作用,而是列表视图通过滚动条扩展 我想限制列表并实现分页,如果我像你说的那样删除该行,我该如何实现分页

以上是关于Android分页列表视图的主要内容,如果未能解决你的问题,请参考以下文章

Android分页列表视图

如何从 postgresql 视图表中获取要显示的对象列表

从视图表中获取数据时出现 Laravel 错误

DRF中五大扩展类及视图集的介绍

SQL 如何查询指定架构中所有表(或视图)的名称

无法将查询另存为视图表