Associating cursor data
Maybe I'm going about this the wrong way, but if so, please correct me. Here is the situation: I have a query which returns URI strings for ringtones stored in a database. I have extended the SimpleCursorAdapter to override the getView() function to query the RingtoneManager for the TITLE of the associated ringer URI, however the getView() function is called more than once per row and slows my application down quite a bit. Is there a way I can run the function once to map uris to titles and store that data somewhere that the getView() can access the data quickly?
Related code snippets:
public String getRingerTitle(String uriString) {
Uri ringtoneUri = Uri.parse(uriString);
Ringtone r = RingtoneManager.getRingtone(this.context, ringtoneUri);
return r.getTitle(context);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(this.layout, null);
}
this.cursor.moveToPosition(position);
String ringer = this.cursor.getString(this.cursor.getColumnIndex(PoolDbAdapter.KEY_RINGER));
TextView rTitle = (TextView) v.findViewById(R.id.ringer_line_item);
rTitle.setText(getRingerTitle(ringer));
return(v);
}
How can the getRingerTitle() function be optimized to cache the data or to only query for each item once? OR - is there a better way I should be approaching this?
Thanks
Asked by: Brooke804 | Posted: 25-01-2022
Answer 1
One way of dealing with this is to keep a Map
around the same lifetime as your Adapter
. The Map
is simply a cache for getRingerTitle()
calls. Be sure to flush the Map
whenever you replace or requery()
your Adapter.
Or, wrap your original Cursor
in a CursorWrapper
and override the appropriate methods to effectively "extend" your original Cursor
by one column. You'd hold the aforementioned Map
in your CursorWrapper
implementation and return the cached value (or call getRingerTitle()
and cache it) for the extra column. This is the same basic approach as the one in the first paragraph, but provides a somewhat cleaner abstraction.
You don't indicate what getRingerTitle()
exactly does. If it is another database query, and your original Cursor
came from a database query, consider using a join to avoid the extra database I/Os.
Also, don't override getView()
on a SimpleCursorAdapter
. Override newView()
and bindView()
instead.
Similar questions
Associating certain file extension to my android application
I'm having trouble associating my custom file extension to my android application that I am developing. In my android manifest file I have the following:
<data android:scheme="file" android:mimeType="*/*" android:pathPattern="*.*\\.myFileExt" />
<data android:scheme="content" android:mimeType="*/*" android:pathPattern="*.*\\.myFileExt" />
It kinda works. Let me explain. I hav...
android - Associating 2 Apps together
I have a project “Abc12†which contains App “TypeYourNameâ€.
Also I have another project “Xyz12†which contains a Service like “MySoftKeyboardâ€, which is NOT a library type project.
I’d like to associate project “Xyz12†to project “Abc12â€, so when user downloads App “TypeYourNameâ€, App “MySoftKeyboard†is also downloaded with it.
Any idea h...
android - Associating ULRs with items in a string array
I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?
Android, associating an xml file to an ImageView (xml file includes the rectangle coords of the Image)
I have an Image that is very big and I want this image to be zoomable and include clickable areas(when clicked changes the layout or something like that). When I try to use invisible buttons for clickable areas, buttons loses their position as image being zoomed.
I found something called imagemapping. this way you can create rectangles with coordinates and you can give id to these rects.
example;
<?...
android - Associating onXXX() calls caused by a single user action
Suppose that I have a RadioGroup in Android. When I tap a RadioButton, a number of listeners are called, including the button's OnClickListener.onClick() method and the group's OnCheckedChangeListener.onCheckedChanged() method.
Is there any way to make an association between those events, to know that they were both triggered by the same tap?
Associating an ID with Android List View Rows
I have an array of User objects. I am only showing First Name and Last Name (through toString() method that I have implemented) in a List View.
public class User
{
public int UserID;
public String FirstName;
public String LastName;
@Override public String toString() {
StringBuilder result = new StringBuilder();
result.append(this.FirstName + " " + this.LastName);
r...
android - Associating App with Epub format
I'm at a loss for getting my application to register with epub files on a mobile device. I have a set of intent filters in my android manifest, but it still will not open with epub files on the sd card. When I go through the File Explorer app, it shows the file, but when I click on it, it says "The system does not support this type of file:". When I download a file from the internet, and then navigate to the d...
java - Android Associating Multiples of two With array Indexes?
I have an Array Of Integers that Contains several Colors; 0xFF000000. I have attempted to create a function that returns the Color from the Array based on the parameter of the function. The parameters will be numbers to the power of two; 2,4,8,16,32. How can I figure calculate which index from the array to get the color from based on the Parameter?
Function:
public static int getNumberColor(int nu...
android - Detecting touch patterns and associating them with info
i want to make an android app that detects touch patterns and associates them with info
EG: 3 touch points with specific distance
Is there any way to achieve this ?
mapping - Android: associating a new file suffix with an existing MIME type?
I asked a similar question in the Android Enthusiasts stackexchange forum, but no answer has been offered. I'm posting this question here, in hopes that possibly more people will see it and perhaps supply an answer.
This is the Android Enthusiasts post:
https://android.stackexchange.com/questio...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android