ViewPager为所有页面重复相同的数据,并在500页中仅添加50页

时间:2016-03-26 11:22:00

标签: android xml android-viewpager

ViewPager为所有页面重复相同的数据,并在500页中仅添加50页

我从sqlite db获取所有数据(500个数据项),但是viewpager只显示一个项目,当翻页时,它为所有页面重复相同的一个项目,我做错了什么? 第二个问题

  

我在数据库中有500个项目,这意味着viewpager必须为项目添加500个页面,但它只添加50个页面

代码低于

    package "";

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by name on 26-Mar-16.
 */
public class Test extends Activity {

    private static final String DB_NAME = "";
    private static final String TABLE_NAME = "";
    private SQLiteDatabase database;

    MyAdapter mAdapter;
    private List<String> testContactName = new ArrayList<>();

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


        ViewPager mViewPager = (ViewPager) findViewById(R.id.testvp);
        mAdapter = new MyAdapter();
        mViewPager.setAdapter(mAdapter);

        getAllContacts();





    }

    public void getAllContacts() {
        DatabaseHelper dbOpenHelper = new DatabaseHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        String query = "select * from " + TABLE_NAME;

        Cursor cursor = database.rawQuery(query, null);

        if (cursor != null) {
            cursor.moveToFirst();
            for (int c = 0; c < cursor.getCount(); c++) {
                String name = cursor.getString(cursor.getColumnIndex("content"));
                String count= String.valueOf(cursor.getCount());
                Toast.makeText(getApplicationContext(), count , Toast.LENGTH_LONG).show();
                testContactName.add(name);
                //  testContactName.notifyAll();

                Log.i("Name: " + "" + "---", name);

            }mAdapter.notifyDataSetChanged();
        }

        cursor.close();

    }



    private class MyAdapter  extends PagerAdapter {

        public MyAdapter() {
            super();
        }

        @Override
        public int getCount() {

            return testContactName.size();

        }
        @Override
        public boolean isViewFromObject(View collection, Object object) {

            return object == collection;
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.test1, null);

            String p= String.valueOf(position)+1;
           TextView pagenumber;
            pagenumber = (TextView) view.findViewById(R.id.tv_test1);

            try {
                pagenumber.setText(testContactName.get(Integer.parseInt(p)));

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ( collection).addView(view);
            return view;

        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            ( collection).removeView((View) view);

            (collection).getChildCount();
            mAdapter.notifyDataSetChanged();

        }
    }
}

测试xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
        android:id="@+id/testvp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

   </RelativeLayout>

test1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/tv_test1"
        android:layout_gravity="top|center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您没有在for循环中前进光标。这就是为什么它不断添加第一项。

cursor.moveToNext();