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
Related
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);
My SqliteDB
In the above link am having my db screenshot
How to load that fiGoods column values into Spinner. Since am new to android kindly help me with full code
Write Query to get fiGoods
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select fiGoods from fiGoodsDetail", null);
String[] figoodslist;
res.moveToFirst();
int i=0;
while(!res.isAfterLast())
{
figoodslist[i] = res.getString(res.getColumnIndex("fiGoods"));
i++;
}
Get fiGoods value in String array.Then you have to bind array to spinner like this...
ArrayAdapter<String> adaper = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, YOUR_STRING_ARRAY);
spinner.setAdapter(adaper);
To link sqlite values to an adapter, use the handy CursorAdapter class.
i'am getting my Listview data from an sqlite database like this
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
aa = new ArrayAdapter<String>(getActivity(),
R.layout.row_listview, arrayList);
setListAdapter(aa);
Cursor cursor8 = db.rawQuery("SELECT * FROM test21", null);
if(cursor8.getCount() == 0)
{
textView5.setVisibility(View.VISIBLE);
}cursor8.close();
Cursor cursor = db.rawQuery("SELECT * FROM test21", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())
{
do {
arrayList.add(cursor.getString(3));
}
while (cursor.moveToNext());
}
When i click another button it remove all the data of the sqlitedatabase.
How can i refresh the content of the listview because there is now nothing. I always need to go back in the activity each time :(
Get rid of the notifyDataSetChanged() and call requery() on your Cursor. Here is a sample project demonstrating this.
Hope it will works for you.
How to update items in listview after update the row on database sqlite. I have put notifyDataSetChanged() but not success, there is not error, but not refresh data in row. The data will change if I click menu back and open list again. So I want update the data in row after I update data. Below structur my code, Thanks.
CustomBaseAdapter adapter;
ListView cListView;
in onCreate:
cListView = (ListView) findViewById(R.id.list_category);
List<ItemsArtikel> rowItems = (List<ItemsArtikel>) db.getAllCategory(katname);
adapter = new CustomBaseAdapter(this, rowItems);
cListView.setAdapter(adapter);
Query listcategory:
public List<ItemsArtikel> getAllCategory(String cat) {
SQLiteDatabase db = this.getWritableDatabase();
List<ItemsArtikel> cList = new ArrayList<ItemsArtikel>();
String selectQuery = "SELECT * FROM " + TABLE_CAT + " WHERE "
+ COLOM_KAT + "=\"" + cat + "\"";
Cursor c = db.rawQuery(selectQuery, null);
c.moveToFirst();
if (c.getCount() > 0) {
do {
ItemsArtikel kat = new ItemsArtikel();
kat.setID(c.getLong(c.getColumnIndex(COLOM_ID)));
kat.setName(c.getString(c.getColumnIndex(COLOM_NAME)));
kat.setLink(c.getString(c.getColumnIndex(COLOM_LINK)));
kat.setImage(c.getString(c.getColumnIndex(COLOM_IMAGE)));
kategoriList.add(kat);
} while (c.moveToNext());
}
return cList;
}
After i use for listview is presented for to download images and update each row the database using AsyncTask when download image. Image success downloaded and row also success updated, Just not refresh in listview.
This is code onPostExecute when download image:
#Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
On calling notifyDataSetChanged() it will just notify the register view to update the content to reflect the changes.
In your case you are updating data in Database not in Adapter Data model. To reflect the changes in ListView you need to updated the Adapter's datamodel and then call notifyDataSetChanged();
OR
If your are rendering data directly from Database use CursorAdapter and implement onContentChanged () for updating ListView
Use following line before add adapter.
#Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.Invalidate();
cListView.setAdapter(adapter);
cListView.Invalidate();
adapter.notifyDataSetChanged();
}
I'm new in android. I'm facing a problem creating a listview using data from sqlite database.
I found this code below to create a listview from contact data
public class Test extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a cursor with all people
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
android.R.layout.simple_list_item_1,
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {Contacts.DISPLAY_NAME},
// The "text1" view defined in the XML template
new int[] {android.R.id.text1});
setListAdapter(adapter);
}
private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
}
Now I use this code to pull data from database
SQLiteDatabase myDB= null;
String TableName = "myTable";
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
// Get a cursor with all people
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
Now How can I combine these two codes so I can show data pulled from a database in this list.
Thanks in advance.
If you want to list the data from both sources (contact and DB), I think only option you have is construct an array with return values from both sources.
List finalList = new ArrayList();
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null);
startManagingCursor(c);
c.moveToFirst();
Iterate this cursor and populate the String value to finalList
SQLiteDatabase myDB= null;
String TableName = "myTable";
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
// Get a cursor with all people
Cursor c2 = myDB.rawQuery("SELECT * FROM " + TableName , null);
startManagingCursor(c2);
c2.moveToFirst();
Iterate c2 cursor and add values to finalList.
Convert finalList to array by Iterating finalList
String[] ipArray = new String[finalList.size()];
loop through list and populate the values to ipArray.
I don't have IDE handy, so I have typed the flow.
If suppose you already have data in your cursor, you can follow code in the following tutorial
http://chetanandroidarora.wordpress.com/2011/12/18/customcursoradapter/