I'm trying to list the system default bookmarks but when I run the app, it list not only the bookmarks but also show History URLs and Most Visited URLs.
How can I avoid this and show only the bookmarks?
here is the method: (imported everything necessary + permissions valid)
public class CLASSNAME extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {Browser.BookmarkColumns._ID,
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
int[] displayViews = new int[] { android.R.id.text1,
android.R.id.text2 };
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cur,
displayFields, displayViews));
}
}
Solution:
Replace the first "null" to "android.provider.Browser.BookmarkColumns.BOOKMARK"
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI, projection, null->android.provider.Browser.BookmarkColumns.BOOKMARK, null, null);
Hope this helps anyone else :)
Related
I have written this fetchData() method to fill ListView with the values from database.
public void fetchData() {
database = helper.getReadableDatabase();
Cursor c = database.query(db.TABLE, null, null, null, null, null, null);
String[] fromDB={db.FULLNAME,db.YEAR};
int[] toView={R.id.tvName_lv,R.id.tv_birthday_lv};
adapter = new SimpleCursorAdapter(this, R.layout.listview_all, c, fromDB, toView);
lv.setAdapter(adapter);
}
It works fine. But when I try to use it this way, it gives an error.
public void fetchData() {
database = helper.getReadableDatabase();
Cursor c = database.query(db.TABLE, null, null, null, null, null, null);
String birthday=db.YEAR+" "+db.MONTH+" "+db.DAY;
String[] fromDB={db.FULLNAME,birthday};
int[] toView={R.id.tvName_lv,R.id.tv_birthday_lv};
adapter = new SimpleCursorAdapter(this, R.layout.listview_all, c, fromDB, toView);
lv.setAdapter(adapter);
}
I want to get [db.YEAR+" "+db.MONTH+" "+db.DAY] to the view [R.id.tv_birthday_lv]
Thanks in advance.
UPDATE:Error is "Unfortunately, App has stopped". FATAL EXCEPTION on LogCat
How do I bind information from my database to a TextView? The ListView Isnt showing the information from the Cursor. Do I need to use the adapter I have created?
I'm trying to display the results in TextViews withing the List.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
Cursor cursor = getCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this, //Context
R.layout.contactform, //xml definintion of each listView item
cursor, //Cursor
new String[] {"FirstName", "LastName", "Phone", "Email"}, //Columns to select From
new int[] {R.id.contact_firstname, R.id.contact_lastname, R.id.contact_phone, R.id.contact_email} //Object to bind to
);
}
private Cursor getCursor()
{
String TABLE_NAME = "exampleContacts";
String[] FROM = {"_id", ""FirstName", "LastName", "Phone", "Email"} ;
dbManager = new DatabaseManager(this);
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, "ContactID=?", new String[] {PrContactID}, null, null, null);
startManagingCursor(cursor);
return cursor;
}
It looks like you're pulling back a single record. In that case, you don't need to be using the SimpleCursorAdapter, as that is generally used for assigning multiple records to a ListView. Why not just grab the info you need and manually set them to your TextView(s)?
Also, you shouldn't need to use a manage cursor. Just get the values you need and then close the cursor like so:
TextView textView = (TextView) findViewById(R.id.textView01);
Cursor cursor = db.query("exampleContacts", new String[] {"FirstName", "LastName", "Phone", "Email"}, "ContactID=?", new String[] {PrContactID}, null, null, null);
cursor.moveToFirst();
String text = cursor.getString(0) + " " + cursor.getString(1) + " " + cursor.getString(2) + " " + cursor.getString(3);
textView.setText(text);
cursor.close();
Sorry for any typos... this code is just for example and hasn't been tested.
The reason it wasnt being mapped to the ListView is because setListAdapter(); wasnt being used.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
Cursor cursor = getCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this, //Context
R.layout.contactform, //xml definintion of each listView item
cursor, //Cursor
new String[] {"FirstName", "LastName", "Phone", "Email"}, //Columns to select From
new int[] {R.id.contact_firstname, R.id.contact_lastname, R.id.contact_phone, R.id.contact_email} //Object to bind to
);
setListAdapter(adapter);
}
in my app i am using a listview to display a menu of dishes with their Dish Names
This is the code where i bind the data from the query to the listview using the adapter:
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
cursor.moveToFirst();
Log.w("ppp","kk - " + cursor.moveToFirst());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
My Query code looks like this:
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
Really appreciate any help/comments as i have been stuck on this for almost a week now.
Can you explain explicitely what's your problem?
My suggest change
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
to
public Cursor getAllDishes()
{
Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
if(dishes != null)
dishes.moveToFirst();
return dishes;
}
Then
in getDishes() method write like this
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(YourActivity.this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
I'm creating an app that needs to display the phone contacts with its photo, name and number. I've created a ListView and can show contacts and numbers with a generic photo, but I can't show contat's picture. This is my code:
Cursor cursor = getContacts();
String[] fields = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText, R.id.contactEntryNumber});
mContactList.setAdapter(adapter);
private Cursor getContacts()
// Run query
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER,
Contacts.PHOTO_ID
};
//String selection = Contacts.HAS_PHONE_NUMBER + "='1'";
String selection = null;
String[] selectionArgs = null;
String sortOrder = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
How can I attach the contact's picture in the SimpleCursorAdapter? Is there another?
Thank you very much.
I figured it out. If someone had the same problem, this is how i did it:
In an activity that extends ListActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor names = getNamesAndPictures();
names.moveToFirst();
ListAdapter adapter = new MySimpleCursorAdapter(this, R.layout.contacts,
names, new String[] {Phones.NAME, Phones.NUMBER}, new int[] {
R.id.Nombre, R.id.Numero});
setListAdapter(adapter);
startManagingCursor(names);
Log.i(DEBUG_TAG, "Mi numero: " + miNumeroTelefono());
}
public Cursor getNamesAndPictures(){
String[] projection = new String[] {
Phones.NUMBER,
Phones.PERSON_ID,
People.NAME,
People._ID
};
String selection = Phones.NAME + "!='null'";
String sortOrder = Phones.NAME + " COLLATE LOCALIZED ASC";
Cursor cursor = managedQuery(Phones.CONTENT_URI, projection, selection, null, sortOrder);
return cursor;
}
In a custom adapter:
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
...
#Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView imageView = (ImageView) view.findViewById(R.id.Foto);
int id = cursor.getColumnIndex(Phones.PERSON_ID);
Uri uri = ContentUris.withAppendedId(Phones.CONTENT_URI, cursor.getLong(id));
String uriPhoto = uri.toString();
String uriPeople = uriPhoto.replace("phones", "people");
Uri uriFinal = Uri.parse(uriPeople);
Bitmap bitmap = People.loadContactPhoto(context, uriFinal, R.drawable.avatar02, null);
imageView.setImageBitmap(bitmap);
super.bindView(view, context, cursor);
}
}
I know it is a very old question but so is the answer as few things in this solution have now been deprecated. As the question showed up in searches while I was looking for similar solution, I though I will add my two cents here...
I have created a simple contacts list with their names and photos from ContactsContract.
Please check my answer at... https://stackoverflow.com/a/37710199/1209544
i have searched for a awnser for this for a while today. It all looks so easy but i never get it to work. I want to fill a spinner with my cursor. I have been trying to use SimpleCursorAdapter for this as a lot of sites say i shall but i never get it to work. Show me just how easy it is :)
Thanks for your time!
My cursor
Cursor cursor = db.query(DATABASE_TABLE_Clients, new String[] {"_id", "C_Name"}, null, null, null, null, "C_Name");
My spinner
(Spinner) findViewById(R.id.spnClients);
My Code
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { R.id.txt_Address };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, cursor_Names, columns, to);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);
The following code solved my problem. I was missing .setDropDownViewResource. After that i used simple_spinner_dropdown_item so i don't have to make my own layout.
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);
I don't see a View for your dropdown in your code. Something like:
mAdapter.setDropDownViewResource(R.layout.spinner_view_dropdown);
Of course you need to have a spinner_view_dropdown.xml file in your res/layout directory.
I have done it
empresasSpinner = (Spinner) findViewById(R.id.empresasSpinner);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, empresasAll.toArray(new EntidadObject[0]));
empresasSpinner.setAdapter(spinnerArrayAdapter);
A simple DTO
public class EntidadObject {
private int id;
private String nombre;
//GETTES and SETTERS
}
The part DAO
public class EntidadDao {
//...
public List<EntidadObject> getEmpresas() {
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM empresas", null);
List<EntidadObject> entidadObjects = new ArrayList<EntidadObject>();
cursor.moveToFirst();
do {
EntidadObject entidadObject = new EntidadObject();
entidadObject.setId(cursor.getInt(0));
entidadObject.setNombre(cursor.getString(1));
entidadObjects.add(entidadObject);
} while (cursor.moveToNext());
return entidadObjects;
}
}
So then I can catch the ID of the select item with
EntidadObject eo = (EntidadObject)empresasSpinner.getSelectedItem();
eo.getId();