private static final String Memo_Name = "MemoName";
.
openDB();
String[] col = new String[] {Memo_Name};
Cursor c = db.query(Memo_Table, col, null, null, null, null, null);
String[] result = new String[c.getCount()-1];
if(c.moveToFirst()){
for (int i = 0; i < c.getCount(); i++){
result[i] = c.getString(c.getColumnIndex(Memo_Name));
c.moveToNext();
}//end of for
}
db.close();
ArrayIndexOutOfBoundsException on this line
result[i] = c.getString(c.getColumnIndex(Memo_Name));
glad I could help.
just so you could close the question -
remove the " -1 "
Related
this is insert code
for(int i = 0 ;pseudo.size() <= i; i++){
ContentValues values = new ContentValues();
String contact = (String) pseudo.get(i);
values.put("pseudo", contact);
values.put("adresse", "test");
values.put("image", "test");
values.put("ID", "5");
dbwrite.insert("Contact", null, values);
}
And this is read code
String[] retour = {
"pseudo",
"xmppadresse",
"image", "ID"
};
Cursor cursor = dbread.query("Contact", retour, null, null, null, null, "pseudo");
Log.d("debug", "populateWithInitialContacts: "+cursor.getCount());
while(cursor.moveToNext()) {
String pseudo = cursor.getString(cursor.getColumnIndexOrThrow("pseudo"));
Log.d("debug", "getcontact: "+pseudo);
listcontact.add(new Contact(pseudo));
}
cursor.close();
My request return nothing(It's my first time using database SQLite in Android)
for(int i = 0 ;pseudo.size() <= i; i++)
This is not how you iterate over lists. You probably want something like
for(int i = 0 ;i < pseudo.size(); i++)
I use a string to populate a textview on an listview
String[] text1 = { "Afghanistan", "Algeria" ,"Fred"};
I want to replace the 3 strings in the string array with data from a database. I have tried the following
String text1[];
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAsset3();
int counter = 0;
while (c.moveToNext()) {
text1[counter]=c.getString(0);
counter++;
}
getAsset3 from DBAdapter
public Cursor getAsset3() throws SQLException
{
Cursor mCursor =
db.query(true, "SURVDAT", new String[] {KEY_SR1,KEY_SR2,KEY_SR3,KEY_SR4,KEY_SR5,KEY_SR6,KEY_SR7}, null, null,null, null, null, null);
if (mCursor != null) {
//mCursor.moveToFirst();
}
return mCursor;
}
When I run the app crashes saying Null PointerException
Any ideas where I'm going wrong?
Any help Appreciated
Mark
You didn't initialize text1 in your second snippet, so when you type text1[counter] = c.getString(0); you are trying to get counter'th index of null
You shall do something like
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAsset3();
String text1[] = new String[c.getCount()];
for(int i = 0; cursor.moveToNext(); ++i)
{
text1[i] = c.getString(0);
}
cursor.close();
I have a problem with getting Shop from database:
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
getShop function in ShopHandler.java
Shop getShop(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SHOPS, new String[] { KEY_ID,
KEY_SHOP }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Shop shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
return shop;
}
and part of procedure in MainClass:
public void AddToList(View view)
{
EditText search = (EditText)findViewById(R.id.editTextsearch);
String SearchProduct = search.getText().toString();
int l = 0;
int p = 0;
ShopHandler sh = new ShopHandler(this);
String[] Shops = new String[sh.getShopCount()];
float[] prices = new float[sh.getShopCount()];
for(int i=0;i<=sh.getShopCount();i++)
{
Shop shop = sh.getShop(i); //hear is second problemplace
ShopName = shop.getShop();
ProductHandler ph = new ProductHandler(this);
for(int j=0;j<ph.getProductsCount();j++)
{
if(ph.getProduct(j).getName()==SearchProduct)
{
Shops[++l]=ShopName;
prices[++p]=ph.getProduct(j).price;
}
}
}
The cursor is empty. Check moveTo...() return value before accessing row data:
Shop shop = null;
if (cursor.moveToFirst()) {
shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
}
cursor.close();
return shop;
This causes the exception. Why the cursor is empty is likely because of the off-by-one here:
for(int i=0;i<=sh.getShopCount();i++)
Either start counting from 1 or change <= to <, depending on your data.
This is most likely the issue:
for(int i=0;i<=sh.getShopCount();i++)
should be for(int i=0;i < sh.getShopCount();i++)
You will be off-by-one if you keep your first loop to stop at sh.getShopCount()
I am using the following code to retrieve the contacts first and last name list from android however i also want to retrieve the email simultaneously:
Cursor1 = getContentResolver().query(
public List<String> getFullContactName()
{
List<String> name = new ArrayList<String>();
String[] projection = new String[] {Data.DATA2, Data.DATA3};
String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
Uri uri = Data.CONTENT_URI;
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Cursor cursor = contentResolver.query(uri,projection,where,null,null);
String firstName, lastName;
while (cursor.moveToNext())
{
firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);
Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();
}
cursor.close();
cursor = null;
return name;
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
cursor.close();
I need to know how can i read first name ,last name email, phone number together using a single Cursor because i need to fetch details in a list and then display them in a listview. I havent got a reference where i can fetch the details using a single cursor.
Use below code for get contact details.
public void getContacts(ContentResolver cr) {
ArrayList<HashMap<String, String>> mContacts = new ArrayList<HashMap<String, String>>();
HashMap<String, String> mTempObj = new HashMap<String, String>();
cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Email", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Number", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Name", cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Address", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
mTempObj.add("City", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
mTempObj.add("State", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
cur.moveToNext();
}
}
mContacts.add(mTempObj);
}
it will solve your problem.
remove
cursor.close();
cursor = null;
and make sure that the contactId that you passed to the Cursor emails is the same as the one that you retrieved from Cursor cursor which seems that you did not declare it in your String[] projection
For the first projection you can as follow:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
this projection will retrive the contact Id and contact name, then to get the contact id and Name
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Then the second projection could be somthing:
String[] projection2= new String[]{
Email.ADDRESS,
Email.TYPE
Then define second cursor to get email within the first cursor something like
Cursor cur2 = cr.query(emailURI,projection2,Email.CONTACT_ID + "=" +id,null,null);
where id is the id that you got from first cursor.
Hope this help
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
for (int i = 0; i < c.getColumnCount(); i++) {
Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
}
I can get all colume names in 4.0.x, but only got _id under 4.0.x. What 's the matter with my code? Thx in advance!
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
for (int i = 0; i < c.getColumnCount(); i++) {
Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
}
while (c.moveToNext()) {
for (int i = 0; i < c.getColumnCount(); i++) {
Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i) + " = " + c.getInt(i) + "/" + c.getString(i));
} ...
code above works well in 4.0.x, I guess there are some differences of database?
#Anuļ¼ this is my complete code, please kindly tell me if you found somthing wrong:
private void retrieveCall()
{
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if (c != null) {
while (c.moveToNext()) {
String number = c.getString(c.getColumnIndex("number"));
String name = c.getString(c.getColumnIndex("name"));
long date = c.getLong(c.getColumnIndex("date"));
if (number.length() > 0) {
LogDetail log = null;
if (_callTable.containsKey(number)) {
log = (LogDetail) _callTable.get(number);
log.name = name;
log.date = date;
log.amount++;
} else {
log = new LogDetail();
log.name = name;
log.date = date;
log.amount = 1;
}
_callTable.put(number, log);
}
}
c.close();
}
}
try this ..... It worked for me...
Cursor c1 = getContentResolver().query(CallLog.Calls.CONTENT_URI,null,null,null,null);
for(int i=0;i<c1.getColumnCount();i++){
Log.i("Column name", ""+c1.getColumnName(i));
}
Don't forget to move the position of the Cursor
Use:
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
while (c.moveToNext()) {
Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
}