Android分页列表视图

时间:2014-11-10 09:40:52

标签: java android eclipse listview android-listview

嗨我在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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/border" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:text="@string/userLabel"
            android:textColor="#FF0000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        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_width="199dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

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

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        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_width="match_parent"
            android:layout_height="184dp"
            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_width="match_parent"
        android:layout_height="match_parent"
        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_width="80dp"
            android:layout_height="wrap_content"
            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_width="80dp"
            android:layout_height="wrap_content"
            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);
}

}

2 个答案:

答案 0 :(得分:0)

虽然我没有得到你正在做的事情,但尝试做这个改变: 删除此行

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

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

itemList.setAdapter(itemAdapter);

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

答案 1 :(得分:0)

您只需使用setAdapter一次。要在下次更改列表项(用于加载下5个项目),您只需使用下一个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();
       }
}