I find some tutorials, to create databaseHelper class, which help me to fetch data from database, the data was fetch using List, but somehow i dont know how to populate that list into ArrayList for my gridview and list view, when i try, it says List cannot converted into ArrayList. How can i do that? here my code snippet
databaseHelper.java
public List<Seat> getAllSeats() {
List<Seat> seats = new ArrayList<Seat>();
String selectQuery = "SELECT * FROM " + TABLE_SEATS + "WHERE status=0";
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Seat seat = new Seat();
seat.set_id(c.getInt((c.getColumnIndex("id"))));
seat.set_table_no(c.getString(c.getColumnIndex("table_no")));
// adding to list
seats.add(seat);
} while (c.moveToNext());
}
return seats;
}
index.java
/* LIST SEAT */
final ArrayList<Seat> list_seat = db.getAllSeats();
final GridView gridview_seat = (GridView) findViewById(R.id.gridviewSeat);
gridview_seat.setAdapter(new SeatListAdapter(this, list_seat));
Just convert your list data into array list with following code...
List<Seat> list = db.getAllSeats();
ArrayList<Seat> list_seat = new ArrayList<Seat>(list.size());
list_seat .addAll(list);
Related
I'm not able to disply the content of my sqllite table. Instead of displaying the values its displaying address of it....help me with the same...
public List<databaseHandler> getAllContacts() {
List<databaseHandler> contactList = new ArrayList<databaseHandler>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// return cursor;
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
databaseHandler handler = new databaseHandler();
handler.setId(Integer.parseInt(cursor.getString(0)));
handler.setName(cursor.getString(1));
//contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(handler);
} while (cursor.moveToNext());
}
//
// // return contact list
return contactList;
}
Looks like you're returning a list of databaseHandler objects and displaying them somehow. "Address of it" hints at the display mechanism using the default toString() implementation that outputs the object's class name and hashCode().
To fix that, override toString() in your databaseHandler class to return a string representation you want to get displayed.
here i get only single value and save it in spinner but i want to retrieve two column value in one spinner
public List<String> getAllLabels1(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT DISTINCT WeatherPrediction.CropID as CropID, Crop.Name as Name FROM Crop INNER JOIN " +
"WeatherPrediction ON Crop.CropID = WeatherPrediction.CropID order by Name";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
I'm able to populate my SQLite DB and to do all CRUD operations.
Now I would populate a listview starting from:
List<Starred> data = DBAdapter.getAllUserData();
By log I can read my list:
for (Starred st : data)
Log.d("Title: ", "Title: " + st.getName());
I'm pretty sure I'm missing the Adapter but don't well know how start.
This is my getAllUserData() function:
public static List<Starred> getAllUserData() {
List<Starred> starredList = new ArrayList<Starred>();
// Select All Query
String selectQuery = "SELECT * FROM " + USER_TABLE;
final SQLiteDatabase db = open();
Cursor cursor = db.rawQuery ( selectQuery, null );
if (cursor.moveToFirst()) {
do {
Starred data = new Starred();
data.setID(Integer.parseInt(cursor.getString(0)));
data.setName(cursor.getString(1));
data.setLabel(cursor.getString(2));
// Adding contact to list
starredList.add(data);
} while (cursor.moveToNext());
}
return starredList;
I tried with
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<Starred> arrayAdapter = new ArrayAdapter<Starred>(
this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(arrayAdapter);
but I get nullpointer exception.
Could anyone give me suggestion? Any help would be much appreciated.
try to use the cursor adapter pal
https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
I am working on apps in which I need retrieve specific records from one position to another. I have table Track_Detail with 20 records.
How to retrieve records from 1 to 10 in one list and 11 to 20 in another list?
Here my source code:
retrieve record from Track table in list view
public List<CatSetGet> getMainListRecords() {
List<CatSetGet> list = new ArrayList<CatSetGet>();
String selectQuery = "SELECT * FROM " + TRACK;
// like limit what query I needed here
cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
} while (cursor.moveToNext());
}
db.close();
return list;
}
db.openDataBase();
mainlist= db.getMainListRecords();
azAdapter = new MySimpleArrayAdapter(Main.this,
new int[] { R.layout.common_list_item }, mainlist);
lv.setAdapter(azAdapter);
lv.setOnItemClickListener(this);
Use limit query.
Example of query:
SELECT * FROM myTable WHERE mColoumnID >= 1 AND mColoumnID <= 10;
//all judge name
in label's we get different judge-name from judge table in index of get indexes of judge-name means ids....
Code :
public List<String> getAlljudge(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT " + KEY_ID_JUDGE + "," +KEY_NAME_JUDGE + " FROM " + TABLE_CONTACTS_JUDGE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.indexOf(cursor.getString(0));
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
further in another table i want to store this id's and selection of judge name through drop down.. actually another layout where a spinner is used where spinner data comes from this "labels" when selection of any judge i want to save corresponding id in another table...how to use both id and value both in labels???? help me...
Pass labels via Intent
Intent ipass = new Intent(this,Your next Class);
ipass.putStringArrayListExtra("key", labels);
startActivity(ipass);
Then In that layout
List<String> labels = new ArrayList<String>();
Intent iget = getIntent();
labels = iRes.getStringArrayListExtra("key");
Hope it work
For spinner operation use this http://www.mkyong.com/android/android-spinner-drop-down-list-example/