ViewPager Add only single data of 500 data
Problem
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 item what i m doing wrong?
class Test
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.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Altaf on 26-Mar-16.
*/
public class Test extends Activity {
private static final String DB_NAME = "dbname";
private static final String TABLE_NAME = "tablename";
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"));
Toast.makeText(getApplicationContext(), name + "content", Toast.LENGTH_LONG).show();
testContactName.add(name);
// testContactName.notifyAll();
mAdapter.notifyDataSetChanged();
Log.i("Name: " + "" + "---", name);
}}
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 collection == ((View) object);
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test1, null);
TextView pagenumber;
pagenumber = (TextView) view.findViewById(R.id.tv_test1);
try {
pagenumber.setText(testContactName.get(position));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
mAdapter.notifyDataSetChanged();
}
}
}
test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
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://ift.tt/nIICcg"
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>
Aucun commentaire:
Enregistrer un commentaire