Call all records into an array of String - android

I need to get all of records from TABLE_NOTES, Here is my method for calling all of records:
public List<NotesFragmentCreate> getAllNotes() {
List<NotesFragmentCreate> notes = new ArrayList<NotesFragmentCreate>();
String selectQuery = "SELECT * FROM " + TABLE_NOTES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
NotesFragmentCreate n = new NotesFragmentCreate();
n.setId(c.getInt(c.getColumnIndex(KEY_ID_NOTES)));
n.setTitle(c.getString(c.getColumnIndex(KEY_TITLE)));
n.setCreated(c.getString(c.getColumnIndex(KEY_CREATED)));
n.setContent(c.getString(c.getColumnIndex(KEY_CONTENT)));
// adding to note list
notes.add(n);
} while (c.moveToNext());
}
return notes;
}
Then I create arrays in other class:
String[] title,created,content;
db = new DatabaseHelper(getActivity());
List<NotesFragmentCreate> allNotes = db.getAllNotes();
for (NotesFragmentCreate note : allNotes) {
//how can I get each of record to store in this array?
title[?] = note.getTitle();
created[?] = note.getCreated();
content[?] = note.getContent();
}
Because I want to impement those arrays in a listView
adapter = new ListNotesAdapter(getActivity(), title, created);

Its simple do as have variable inside loop shown as below.
Replace
String[] title,created,content;
List<NotesFragmentCreate> allNotes = db.getAllNotes();
for (NotesFragmentCreate note : allNotes) {
//how can I get each of record to store in this array?
title[?] = note.getTitle();
created[?] = note.getCreated();
content[?] = note.getContent();
}
with
List<NotesFragmentCreate> allNotes = db.getAllNotes();
String[] title=new String[allNotes.size()];
String[] created=new String[allNotes.size()];
String[] content=new String[allNotes.size()];
int i=0;
for (NotesFragmentCreate note : allNotes) {
//how can I get each of record to store in this array?
title[i] = note.getTitle();
created[i] = note.getCreated();
content[i] = note.getContent();
i++
}

Related

Passing value to DB Helper

I am passing the value to DB Helper to query, and I have 2 errors.
One in query
DB Helper:
public Cursor opis(final String sid){
SQLiteDatabase db = this.getWritableDatabase();
final String ID = sid;
Cursor rezultat = db.rawQuery("Select opis from tablica where id = ? LIMIT 1" , new String[] {ID});
return rezultat;
}
Error is on question mark:
Second error is in my MainActivity:
private void prikaz() {
final ListView listView = (ListView) findViewById(R.id.idListView);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDb.opis();
if(data.getCount() == 0){
Toast.makeText(this, "No data!",Toast.LENGTH_LONG).show();
}
else
{
while(data.moveToNext()){
String opis = data.getString(0);
}
}
}
Error:
for the first error use
Cursor rezultat = db.rawQuery("Select opis from tablica where id = '"+yourvalue+"' LIMIT 1" , new String[] {ID});
for the secound you should pass String value to your method opis()

Return One value if more than one Exsist

if i have same values in a column and i want to return only one instead of all of them what would be a method.
ArrayList<String> getAllNotes() {
Cursor cursor;
mDbHelper = mSqliteHelper.getWritableDatabase();
String query = "SELECT * FROM " + SqliteHelpers.TABLE_NAME;
cursor = mDbHelper.rawQuery(query, null);
ArrayList<String> arrayList = new ArrayList<>();
while (cursor.moveToNext()) {
String itemname = cursor.getString(cursor.getColumnIndex(SqliteHelpers.NOTES_COLUMN));
if (itemname != null) {
arrayList.add(itemname);
}
}
return arrayList;
}
As you can see in image there are four items all are same but i want one tobe return other will be returned normal. except more than one.image
Just return the column you want:
ArrayList<String> getAllNotes() {
Cursor cursor;
mDbHelper = mSqliteHelper.getWritableDatabase();
String query = "SELECT Distinct NOTES_COLUMN FROM " + SqliteHelpers.TABLE_NAME;
cursor = mDbHelper.rawQuery(query, null);
ArrayList<String> arrayList = new ArrayList<>();
while (cursor.moveToNext()) {
String itemname = cursor.getString(cursor.getColumnIndex(SqliteHelpers.NOTES_COLUMN));
if (itemname != null) {
arrayList.add(itemname);
}
}
return arrayList;
}

Return String[] From database

I am trying to populate my AutoCompleteTextView from a column of values in my database.
The query I am running in my database is:
// GET MEMOS
public ArrayList<String> autoCompleteMemo(String table)
{
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> memoList = new ArrayList<String>();
String SQL_GET_MEMOS = "SELECT memo FROM " + table;
Cursor cursor = db.rawQuery(SQL_GET_MEMOS, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
memoList.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return memoList;
}
And here is how I am attempting to set the values:
memoList = new String[db.autoCompleteMemo(table).size()];
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, memoList);
etMemo.setAdapter(adapter);
For some reason, this does not appear to be working. Am i converting from ArrayList to String[] properly?
Thanks
Also,
If i do something similar to this
String[] memoList = getResources().getStringArray(R.array.memoList);
and populate that in Strings.xml it works fine.
I changed my DB method to the following and it now works
public String[] autoCompleteMemo(String table) {
SQLiteDatabase db = this.getReadableDatabase();
String SQL_GET_MEMOS = "SELECT memo FROM " + table;
Cursor cursor = db.rawQuery(SQL_GET_MEMOS, null);
if (cursor.getCount() > 0) {
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
str[i] = cursor.getString(cursor.getColumnIndex("memo"));
i++;
}
return str;
}
else {
return new String[] {};
}
}

SQLite read all data operation

Please check what problem with this .I am trying to retrive all records in my table (contains 11 records with 3 columns "name","address","city") but my code showing only one i.e last inserted record on my screen. but i need to display all records.
public void readData(View v)
{
DatabaseClass mydb = new DatabaseClass(this);
SQLiteDatabase readdata = mydb.getReadableDatabase();
TextView tv = (TextView)findViewById(R.id.readtext);
Cursor c = readdata.rawQuery("select * from mylistdata", null);
int[] elementId = {R.id.textView1, R.id.textView2, R.id.textView3};
if(c !=null)
{
c.moveToFirst();
while(c.isAfterLast() == false)
{
listData = new ArrayList<GenericListItem>();
String name = c.getString(c.getColumnIndex(DatabaseClass.NAME));
String address = c.getString(c.getColumnIndex(DatabaseClass.ADDRESS));
String city = c.getString(c.getColumnIndex(DatabaseClass.CITY));
listData.add(new GenericListItem(new String[]{name,address,city}));
listAdapter = new GenericListAdapter(getApplicationContext(), R.layout.list_layout, listData, elementId);
result.setAdapter(listAdapter);
c.moveToNext();
}
}
Initialize listData ArrayList outside from While loop as:
c.moveToFirst();
listData = new ArrayList<GenericListItem>();
while(c.isAfterLast() == false)
{
// add value here to listData
c.moveToNext();
}
// set listData datasource to GenericListAdapter here
listAdapter = new GenericListAdapter(getApplicationContext(),
R.layout.list_layout, listData, elementId);
result.setAdapter(listAdapter);
It looks like the problem is that you are re-creating your list each time through the loop. Try this:
listData = new ArrayList<GenericListItem>(); //moved out of loop
while(c.isAfterLast() == false)
{
String name = c.getString(c.getColumnIndex(DatabaseClass.NAME));
String address = c.getString(c.getColumnIndex(DatabaseClass.ADDRESS));
String city = c.getString(c.getColumnIndex(DatabaseClass.CITY));
listData.add(new GenericListItem(new String[]{name,address,city}));
listAdapter = new GenericListAdapter(getApplicationContext(), R.layout.list_layout, listData, elementId);
c.moveToNext();
}
result.setAdapter(listAdapter); // moved out of loop

how to get the database value to a String array in android(sqlite Database)

I have a database name "CUED" (sqlite Android)it have a table HELLO which contain a column NAME I can get the value to String from that column.
Let me show you my code section
myDB =hello.this.openOrCreateDatabase("CUED", MODE_PRIVATE, null);
Cursor crs = myDB.rawQuery("SELECT * FROM HELLO", null);
while(crs.moveToNext())
{
String uname = crs.getString(crs.getColumnIndex("NAME"));
System.out.println(uname);
}
It will print the value one by one. Now what I need is that I want to get the column values from database and so that I can store it in a String Array.
You already did the hard part... the array stuff is pretty simple:
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("NAME"));
array[i] = uname;
i++;
}
Whatever, I always recommend to use collections in cases like this:
List<String> array = new ArrayList<String>();
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("NAME"));
array.add(uname);
}
In order to compare the arrays, you can do things like this:
boolean same = true;
for(int i = 0; i < array.length; i++){
if(!array[i].equals(ha[i])){
same = false;
break;
}
}
// same will be false if the arrays do not have the same elements
String[] str= new String[crs.getCount()];
crs.movetoFirst();
for(int i=0;i<str.length();i++)
{
str[i] = crs.getString(crs.getColumnIndex("NAME"));
System.out.println(uname);
crs.movetoNext();
}
Enjoy it
This is my code that returns arraylist contains afield value:
public ArrayList<String> getAllPlayers() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("SELECT " + serailnumber + " as _id, " + title
+ " from " + table, new String[] {});
ArrayList<String> array = new ArrayList<String>();
while (cur.moveToNext()) {
String uname = cur.getString(cur.getColumnIndex(title));
array.add(uname);
}
return array;
}

Categories

Resources