CursorAdapter adapts to the wrong fields - android

I subclassed SimpleCursorAdapter for displaying a list. In my row layout file, I have 3 textView, but I just want to map the data to 2 of them. I explicitly left out txt2, but it keeps being displayed with the data on it. I can fix this by manually removing txt2 from the layout file, then all good. But just out of curiosity, what was wrong with the adapter in the fist place?
Tks.
String []from = new String[] {
DbField.FIELD1,
//DbField.FIELD2,
DbField.FIELD3
};
// Fields on the UI to which we map
int[] to = new int[] { R.id.txt1, /*R.id.txt2,*/ R.id.txt3 };
adapter = new CustomCursorAdapter(this.getActivity()
, R.layout.row_entry, null, from,
to, 0);
setListAdapter(adapter);

Related

Adding items to the top of a dynamic listview in Android

I am new to android and not a killer in programming. I am trying to implement a dynamic listview in which items must be added to the top, rather than getting added to the bottom by default. Is there a way to achieve this? Below is the code I used to create a dynamic listview. Please help!
private void populateListView() {
Cursor cursor = myDb.getAllRows();
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{DBadapter.KEY_FIELD1,DBadapter.KEY_FIELD2};
int[] toViewIDs = new int[]
{R.id.field1,R.id.field2};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this,
R.layout.inner_list_view,
cursor,
fromFieldNames,
toViewIDs
);
ListView myList = (ListView) findViewById(R.id.sampleList);
myList.setAdapter(myCursorAdapter);
myCursorAdapter.notifyDataSetChanged();
}
ListViews support stacking the items from the bottom. You can either set it in the XML with android:stackFromBottom or you can set it programmatically by invoking setStackFromBottom(boolean).
I don't understand the 2nd part of your question.
you can use the "insert" method. It allows you to define position to add new items
myCursorAdapter.insert(newItem, 0);
Use a loop if you have multiple elements

How to dynamically make TextView's setVisibility(View.GONE)?

I am displaying a List that contains 3 Textview for name, start_date and day.Now I want that if any start_date value is empty, Listview must not display that TextView. I know the functionality of "GONE". But In my case I am using hashmap. I don't know where to apply "GONE".
Here is my code:
ListAdapter adapter = new SimpleAdapter(this, hashList,
R.layout.list_item, new String[] { TAG_NAME,
TAG_START_DATE, TAG_DAYS }, new int[] {
R.id.name, R.id.startdate, R.id.days });
Here R.id.name, R.id.startdate, R.id.days are Textviews.
Here, start_date may and may not be available, so R.id.startdate must be applied Visibility Gone if hashmap don't has value for that.
You have to create your own adapter, extending BaseAdapter for instance. Then, when the getView() callback is called you can check your condition and show the info accordingly

Multiple rows in a single TextView

I have an application that has a Database of vehicles with multiple fields.
I then have a ListView in which I want to display some of the fields.
Currently I have this:
SimpleCursorAdapter vehicleAdapter = new SimpleCursorAdapter(this, R.layout.vehiclerow, vehicleCursor, new String[] {"registration"}, new int[] { R.id.vehicleRow});
And I want to put multiple values on the vehicleRow TextView for instance:
Toyota - Corolla
in which Toyota and Corolla come from separate fields of the database.
I've tried this:
SimpleCursorAdapter vehicleAdapter = new SimpleCursorAdapter(this, R.layout.vehiclerow, vehicleCursor, new String[] {"make", "model"}, new int[] { R.id.vehicleRow});
but it doesn't work.
What could be the problem?
Regards
You can only map one data field to one view with the standard adapters.
You either need to create a custom adapter to let you put two data fields.into one widget (not always trivial) or create a new rowlayout with the necessary textviews and use that with a standard adapter.
I'd go for the second option.
Create another TextView and entered its Id in the cursorAdapter.
try in this way :
String[] from = new String[]{DbManager.Title, DbManager.Date};
int[] to = new int[] { R.id.list_name_main,R.id.list_date_main};{
Adapter = new SimpleCursorAdapter(this, R.layout.row_item_by_list, c1, from, to);
//Adapter.setViewBinder(new ScoreHistoryBinder());
list_item.setAdapter(Adapter);
list_item.setOnItemClickListener(listOnItemClickListener);}

How can I merge two SimpleCursorAdapters in Android?

I am using two SimpleCursorAdapters to display text and images in a row in my Listview. The problem is that when calling these Adapters two times they conflict with each other. In the end my Listview only shows the data of the SimpleCursorAdapter that I called last.
What I need to do is merge those two SimpleCursorAdapters although they use different sql databases.
Any ideas to solve this??
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
mDbHelper = new RemindersDbAdapter(this);
mImageHelper = new ImageAdapter(this);
mDbHelper.open();
mImageHelper.open();
fillData();
fillImages();
registerForContextMenu(getListView());
}
//
// Fills the ListView with the data from the SQLite Database.
//
private void fillData() {
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
// Creates an array with the task title.
String[] from = new String[] {RemindersDbAdapter.KEY_TITLE, RemindersDbAdapter.KEY_BODY};
// Creates an array for the text.
int[] to = new int[] {R.id.text1, R.id.text2};
// SimpleCursorAdapter which is displayed.
SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
setListAdapter(reminders);
}
//
// Fills the ListView with the images from the SQLite Database.
//
private void fillImages() {
Cursor imageCursor = mImageHelper.fetchAllImages();
startManagingCursor(imageCursor);
// Creates an array with the image path.
String[] fromImage = new String[] {ImageAdapter.KEY_IMAGE};
// Creates an array for the text.
int[] toImage = new int[] {R.id.icon};
// SimpleCursorAdapter which is displayed.
SimpleCursorAdapter images = new SimpleCursorAdapter(this, R.layout.reminder_row, imageCursor, fromImage, toImage);
setListAdapter(images);
}
You can use the MergeCursor class to expose a number of individual cursors as a single cursor. As your adapters bind different columns->widgets, you may have to write your own subclass of SimpleCursorAdapter (or just CursorAdapter) so that you can do the correct type of binding depending on the row.
how about creating your own adapter (extend BaseAdapter) , which will point to the correct data each time it needs to ?
this way you can have full control over when and where to show each item.
you can even use the 2 adapters and use them for the new adapter.
What I need to do is merge those two SimpleCursorAdapters although they use different sql databases.
If the two adapters are truly different (e.g., different row layouts), use my MergeAdapter to combine your two existing adapters into one, then use the MergeAdapter with your ListView.
However, in your case, it would appear that you simply have two Cursors with different contents, in which case superfell's answer of using MergeCursor is probably a better approach.

Android - Text dropdown/selection of Spinner does not show

I'm using this sample code to populate the Spinner. Data is read from database. The selection displays correctly - in this case, it shows "Green" and "Red".
Spinner spinnerColor = (Spinner) findViewById(R.id.spinnertProfile);
mProfileDbHelper = new ProfileDbAdapter(this);
mProfileDbHelper.open();
Cursor profilesCursor = mProfileDbHelper.fetchAllProfiles();
startManagingCursor(profilesCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
// and an array of the fields we want to bind those fields to
int[] to = new int[] { R.id.textviewColors };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
R.layout.profile_color, profilesCursor, from,
to);
spinnerColor.setAdapter(profilesAdapter);
}
However, when I changed to use a different layout android.R.layout.simple_spinner_dropdown_item. The Spinner Text disappeared.
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
R.layout.profile_color, profilesCursor, from,
to);
profilesAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(profilesAdapter);
See snapshots of without and with simple_spinner_dropdown_item below:
Anything I may miss?
Ok, what's happening is that when you're calling setDropDownViewResource you're replacing the layout you previously specified in the constructor. In your case R.layout.profile_color. SimpleCursorAdapter extends ResourceCursorAdapter and in the constructor sets the two layouts equal to each other.
public ResourceCursorAdapter(Context context, int layout,
Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
The issue arises when you call setDropDownViewResource and change the drop down layout. The SimpleCursorAdapter will continue to use the same resource id bindings that you specified in the constructor.
What you should do is only specify the layout in the SimpleCursorAdapter's constructor. I suggest changing your code to as follows:
String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_dropdown_item, profilesCursor, from, to);
spinnerColor.setAdapter(profilesAdapter);
To get the results you want.
Basically don't use the setDropDownViewResource method. Or, if you do, and you change the resource id bindings, you'll have to call SimpleCursorAdapter#changeCursorAndColumns; however, that is probably overkill for the simple result you're trying to achieve.
VERY IMPORTANT! I almost tore my hair out trying to figure out why my code wouldn't work. For those of you who may be reading this and your code still isn't working, make sure that int[] to = new int[] {android.R.id.text1} if you're using the other android layouts (such as android.R.layout.simple_spinner_dropdown_item). The code won't work if the integer array textview is not contained in the specified layout that you're using. While debugging my code I changed a lot of things around and forgot to make sure that these matched. SO, if you define your own layout, make sure that you use a textview from that layout.
Qberticus,
Yes, you're right about the resource ID binding!!
However, if I started with android.R.layout.simple_spinner_dropdown_item, obviously the dropdown layout will show, but it is not pretty :-)
String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_dropdown_item, profilesCursor, from, to);
spinnerColor.setAdapter(profilesAdapter);
But now If I started with simple_spinner_item first, then setDropDownViewResource to simple_spinner_dropdown item, it now displays exactly what I'm looking for.
String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, profilesCursor, from, to);
profilesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(profilesAdapter);
Thank you so much for your help.

Categories

Resources