XML : ViewPager repeats same data for All pages and adding only 50 pages out of 500 pages

ViewPager repeats same data for All pages and adding only 50 pages out of 500 pages

i m fetching All data from sqlite db (500 data items) but viewpager shows only one item and when flip the page it repeat the same one item for all pages what i m doing wrong? second problem

i have 500 items in data base it means viewpager must add 500 pages for items but it adds only 50 pages

code is below

class

      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();            }      }  }    

test 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>    

No comments:

Post a Comment