Here I want to store data in ArrayList visible Sections please help me...
Here's my code.
public DataSingleton() {
try {
db = openOrCreateDatabase("/sdcard/Download/Nanhikali_db.db", Context.MODE_PRIVATE, null);
Cursor c = db.rawQuery("select label from nanhikali where level = 1", null);
if (c.moveToFirst()) {
while (c.moveToNext()) {
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
visibleSections = new ArrayList<>();
}
}
}
catch(Exception e)
{
}
Your column string maybe wrong:
Try like this.
if(c.moveToFirst()){
while(c.moveToNext()){
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
}
}
Not "lebel".
You are re assigning the array list every time your loop iterates.
remove visibleSections = new ArrayList<>(); from your loop.
and use While Loop as
while(!cursor.isAfterLast())
{
//get All data here then
cursor.moveToNext();
}
Related
I have table subject, when I try the query in adb shell the output is OK
SELECT * FROM subject;
The output is 10 rows...
When I do the same in Java/Android:
public ArrayList<Subject> getSubjectList() {
SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
ArrayList<Subject> subjects = new ArrayList<Subject>();
String selectQuery = "SELECT * FROM subject";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
Subject subject = new Subject();
subject.setId_subject(Integer.parseInt(cursor.getString(0)));
subject.setName(cursor.getString(1));
subjects.add(subject);
}
}
cursor.close();
DatabaseManager.getInstance().closeDatabase();
return subjects;
}
The output is 9 rows... the first row with id 1 will not display, why?
Because you move to the next (moveToNext()) row immediately after moving on the first one (moveToFirst())...
What i normally do is something like this:
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
// ...
}
while (cursor.moveToNext());
}
}
Because on entering the if statement, you moveToFirst(), then on the first entry to the while loop, you .moveToNext(). Meaning you skip the first record.
So use a do... while() construct instead:
if (cursor.moveToFirst()) {
do {
Subject subject = new Subject();
subject.setId_subject(Integer.parseInt(cursor.getString(0)));
subject.setName(cursor.getString(1));
subjects.add(subject);
}while (cursor.moveToNext())
}
Reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
I retrieve all the rows from my SQLite-DB Table
Cursor cursor = db.rawQuery("select * from table",null);
Then I want to add each row to an ArrayList
Please help!
IF you're using custom object array, then use like following :
public void fillDataToArray() {
ArrayList<BeanClass> arrayList = new ArrayList<BeanClass>();
DatabaseHandler db = new DatabaseHandler(getActivity());
db.open();
try
{
Cursor c = db.getImageNameForList();
if (c != null && c.getCount() > 0) {
c.moveToFirst();
for (int count = 0; count < c.getCount(); count++) {
Beanclass detail = new Beanclass ();
detail.setName(c.getString(c
.getColumnIndex(DatabaseHandler._ID)));
detail.setPath(c.getString(c
.getColumnIndexOrThrow(DatabaseHandler._PATH)));
arrayList.add(detail);
c.moveToNext();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
c.close();
db.close();
}
If you use this in activity, change getActivity() to local context or whichever way you use context in activities.
DATA RETRIEVAL METHOD IN DB ADAPTER CLASS :
> public Cursor getImageNameForList() {
return db.rawQuery("select " + IMAGE_PATH + " from "+ TABLE_PRODUCT_IMAGES , null);
}
Code to do that would look like this, depending on whats in your Table. You have to create a Class that is able to represent a Row in your table.
For example if there are people in your table, with name and age, you will need an object Human(String name, int age) to put into a list.
SQLiteDatabase db = helper.getReadableDatabase();
ArrayList<Human> results = new ArrayList<Human>();
try {
String rawQuery = "Select name, age from people";
Cursor c = db.rawQuery(rawQuery, null);
try {
if (!c.moveToFirst())
return null;
do {
results.add(new Human(c.getString(0),c.getInt(1)));
} while (c.moveToNext());
} finally {
c.close();
}
} finally {
db.close();
}
public class Human {
private String name;
private int age;
public Human (String name, int age) {
this.name = name;
this.age = age;
}
}
In your Db Adapter class, add this method
public ArrayList<String>getData()
{
ArrayList<String>names=new ArrayList<String>();
Cursor c = getReadableDatabase().rawQuery("Select * from tableName", null);
c.moveToFirst();
do
{
String s1=c.getString(c.getColumnIndex("<Column_Name>"));
names.add(s1);
}while(c.moveToNext());
return names;
}
after that in your activity class add this
ArrayList<String> cid;
DB_Adapter database = new DB_Adapter(getApplicationContext(), "<your schema name>", null, 1);
cid=database.getData();
After that add your arraylist to your array adapter which is set to ListView
The problem is i inserted only two rows but on execution it enters into infinite loop and repeatedily showing the same data..Any kind of help would be appreciated
Here is my code.
This is my databse class (i.e DbAdapter)
public Cursor getAllRows() {
String[]columns = new String[]{ KEY_MODEL,KEY_BRAND, KEY_PRICE};
Cursor c = db.query(TABLE_LAPTOPS, columns, null, null, null, null, null);
if (c != null) {
c.moveToLast();
}
return c;
}
This is my main class (i.e Laptops)
DbAdapter obj = new DbAdapter(this);
obj.open();
obj.insertRow("Dell1", "Dell", 345);
obj.insertRow("Hp1", "HP", 546);
obj.close();
obj.open();
Cursor c1 = obj.getAllRows();
try{
if(c1 != null){
ListView empListView = (ListView)findViewById(R.id.listView1);
ArrayList<String> data1 = new ArrayList<String>();
for(c1.moveToFirst();!c1.isAfterLast();c1.moveToNext())
{
Toast.makeText(getApplicationContext(), c1.getString(0), Toast.LENGTH_LONG).show();
}
//empListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1));
}}catch(SQLException e){
e.printStackTrace();
}
obj.close();
c.moveToLast();
here is the issue
c.moveToFirst();
try this..
Try with Like This
listView = (ListView) rootView.findViewById(R.id.Assignment_VM_List);
AssignmentDatabaseConnector dao = new AssignmentDatabaseConnector(getActivity());
List<AssignmentPojo> assignments = dao.getAllAssignmentList();
AssignmentCustomAdapter assignmentCustomAdapter = new AssignmentCustomAdapter(getActivity(),
assignments, this);
listView.setAdapter(assignmentCustomAdapter);
AssignmentDatabaseConnector :
public List<AssignmentPojo> getAllAssignmentList() {
open();
List<AssignmentPojo> assignmentList = new ArrayList<AssignmentPojo>();
// Select All Query
String selectQuery = "SELECT * FROM Assignment";
Cursor cursor = database.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) { // ##Your Main Issue is Here ##
do {
AssignmentPojo assignment = new AssignmentPojo();
assignment.setAssign_Id(cursor.getString(0));
assignment.setAssign_Course(cursor.getString(1));
// Adding contact to list
assignmentList.add(assignment);
} while (cursor.moveToNext());
}
cursor.close();
database.close();
// return contact list
return assignmentList;
}
This is just a sample code you can utilize this code according to your requirements
I have an application where I want to use the values returned from an SQL query against a database in my application. I want to use attach these values to a uri that links to a script on remote host. I am doing a call to the method to retrieve the values, after which I will now extract them. The issue is that I am facing a challenge in going about this. I know a bit of php and what I want in android is done like this in php:
....(preceding code)...$row['email'];
and I can now assign a variable e.g. $email=$row['email'];
How can I do this in android?
I don't want a cursor returned, just the values of the columns as strings.
Below is my code (I will need help to retrieve the password column)
public String[] selectAll()
{
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex("email"));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
I need help on inserting the "password" column so that it will be returned with the email.
try this way
String[][] str = new String[cursor.getCount()][cursor.getColumnCount()];
while (cursor.moveToNext())
{
for(int j=0;j<cursor.getColumnCount();j++)
str[i][j] = cursor.getString(j); /// may be this column start with the 1 not with the 0
i++;
}
you need to store this in two dimenstion array for row wise and column wise
but if this will give only one result everytime
String[] str = new String[cursor.getColumnCount()]; // here you have pass the total column value
while (cursor.moveToNext())
{
str[0] = cursor.getString(cursor.getColumnIndex("email"));
str[1] = cursor.getString(cursor.getColumnIndex("password"));
}
You should change your design like this
public Cursor selectAll() {
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
return cursor;
}
and use the selectAll function like this
Cursor cursor=selectAll();
String[] mail = new String[cursor.getCount()];
String[] pass = new String[cursor.getCount()];
int i=0;
while (cursor.moveToNext())
{
mail[i] = cursor.getString(cursor.getColumnIndex("email"));
pass[i] = cursor.getString(cursor.getColumnIndex("password"));
i++;
}
hi
I want to store the result of request in a table. I want the result of the method is table howa to do that? My code contains error.
public array getResult_libelle(int id)
{
array tab[] = null;
try
{
Cursor c = null;
c = db.rawQuery("select libelle from favori where _id="+id, null);
c.moveToFirst();
tab = c.getString(c.getColumnIndex("libelle"));
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return tab;
}
public ArrayList <String> getResult_libelle(int id)
{
ArrayList <String> tab = new ArrayList <Stringr>();
try
{
Cursor c = null;
c = db.rawQuery("select libelle from favori where _id="+id, null);
c.moveToFirst();
for(int i=0;i<c.getCount();i++){
tab.add(c.getString(c.getColumnIndex("libelle")));
c.moveToNext();
}
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return tab;
}
First of all, your cursor will already be having only the column named libelle so I don't think there should be a need to explicitly filter it out.
I guess, you should directly use getString(i) to get data of each row out, one by one, from the cursor
give that a try.