Display data sequencely from DB in android
i need to retrieve data from SQLite & display it sequencely.For ex.if im having 6 rows as "Tn","KR","KN","PJ","DL","HD".Firstly i need to display "TN","KR" as list.If i click next button.,"KN","PJ"should be displayed that replaces old 2..Again when i click next button.,"DL","HD"should be displayed that also replace old 2.
My code..
DBAdapter db = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
// list = getListView();
bt=(Button)findViewById(R.id.category);
prev=(Button)findViewById(R.id.prev);
next=(Button)findViewById(R.id.next);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
first();
}
});
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
second();
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
first();
}
});
}
private void first() {
db.open();
//db.insertTitle("Money");
// db.insertTitle("make");
// db.insertTitle("Anythings");
//db.insertTitle("in the world");
//db.insertTitle("Common");
//db.insertTitle("To all");
results.clear();
Cursor c = db.getAllRecords(1,2);
if (c.moveToFirst())
{
do {
String firstName = c.getString(c.getColumnIndex("user"));
results.add( firstName );
} while (c.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
results));
db.close();
开发者_开发知识库 // c.requery();
}
private void second() {
db.open();
Cursor c = db.getAllRecords(2,2);
results.clear();
if (c.moveToFirst())
{
do {
String firstName = c.getString(c.getColumnIndex("user"));
results.add( firstName );
} while (c.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
results));
db.close();
// c.requery();
}
Initially it displays "Tn","KR".When i click next.,it shows "KN","PJ" as list well.But i donno how to display "DL","HD" when i click next again.
Get the values of all the rows from the database using cursor and store that in a string array or array list
Take a count variable and keep incrementing or decrementing based on the next button or previous button to get the required details and display them in the ListView.. I guess you can do it that way
cursor = this.db.query(TABLE_NAME, new String[] {"user"},null,null, null, null, null);
if (cursor.moveToFirst()) {
do {
arrayList.add(cursor.getString(0));
} while (cursor.moveToNext());
cursor.close();
Use a cursor that and retrieve sequentially that way.
精彩评论