I have a ListView (technically its a ListFragment, but I dont think that changes the question) which displays its results uisng a SimpleCursorAdapter.
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
null,
new String[] {"Name"},
new int[] {R.id.Name}
);
setListAdapter(adapter);
(The Cursor is set/swapped in onStart).
I want to give the user the ability to toggle choice mode (which will than be used to display only the checked items). A bit like an SMS application when you put messages in "batch mode" which allows you to select multiple conversations which can the be deleted, saved etc. Do I set a new adapter or just change the existing ones properties? the ListView will need re-drawing anyway to reflect changes.
You have to do two things.
Use the ListView's setChoiceMode method to enable MULTI_SELECT on the ListView.
Create a 'Selector' to define a 'selected' or 'checked' state and set this as a style element on your R.layout.item.
Like Vikram Bodicherla answer.
setChoiceMode to enable MULTI_SELECT on the ListView.
Set in your custom item layout a background that show the selected state like:
android:background="?android:attr/activatedBackgroundIndicator"
This is a suprisingly simple answer. The problem was I was calling the method in onCreate before onCreateView had inflated the List. Stupdily this was just to test the function. Once I moved it to onStart and later into its intended location in the FragmentActivity the following worked fine:
public void toggleAdapter()
{
ListView listView = getListView();
if (listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE)
{
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.item,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
else
{
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.dashboard_list_item_multiple_choice,
dashboardCursor,
new String[] {"Name"},
new int[] {R.id.Name}
);
}
listView.setAdapter(adapter);
}
Related
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
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
I've finally managed to populate a spinner from an sqlite db after much messing about with this code however it's only using one field and I want First and Last names on the same spinner item?
The code is as follows:
private void fillSpinner() {
Cursor c = myDbHelper.FetchDrivers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"FirstName"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.spr_Driver1);
s.setAdapter(adapter);
}
Now it displays the FirstName and I've tried added "FirstName", "LastName" but it doesn't do anything different, I ideally want it to display the name in full on each spinner item. Is this even possible?
Any help would be great!
Thanks,
Chris
For this, you need to change the android.R.layout.simple_spinner_item and android.R.layout.simple_spinner_dropdown_item, as these layout items are able to display only one item in the dropdown list and on the spinner.
For your purpose, the best way is to create your own layout that has two items.
Here is a link about how to do it.
I have a listview that I want to change the font size of.
Of course, doing this works with some hardcoded data:
setListAdapter(new ArrayAdapter<SimpleCursorAdapter>(this, R.layout.list_view));
But I would like it to take my cursor, so I have
mContactList.setAdapter(adapter);
But in that case, I can't change my layout to be my list_view. Is there any way to go about this oro do I have to take my cursor data and put it into an array first.
This is where I have my adapter:
private void populateContactList() {
Cursor cursor = getContacts();
fields = new String[] { ContactsContract.Data.DISPLAY_NAME };
adapter = new SimpleCursorAdapter(this,
R.layout.entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
}
EDIT: Got it. Turns out, I just needed to edit the contactEntryText TextView.
Maybe this will help you:
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.two_line_list_item,
mCursor,
new String[] {"String1", "String2"},
new int[] {android.R.id.text1, android.R.id.text2});
lv.setAdapter(adapter);
You could use a CursorAdapter.
for custom listviews you need to extend baseadapter link here
and make your own adapter where you can specify the layout xml to be used for each or all elements.
Hey i use a listview for demonstrate entries which are stored in a database. I also have a EditText element and a button which adds the content of the EditText into the Database. To bind the view to the database content i use the SimpleCursorAdapter and following populate function:
private void populate() {
cursor = dbAdapter.getAllItems();
startManagingCursor(cursor);
String[] from = new String[] { DBAdapter.KEY_TASK };
int[] to = new int[] { android.R.id.text1 };
// Now create an array adapter and set it to display using our row
cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
list.setAdapter(cursorAdapter);
}
If i added a new entry, by clicking on the button i want to refresh the listview but this only works with the populate function and not with the common adapter function notifyDataSetChanged();. Do i have a bug or is this the right way to refresh a listview?
Have you seen this, tried the swap cursor method, or tried just simply calling setAdapter() again?
I had a similar issue where I could not get my list to update, and what I did was just create a refreshListView() method. Now you can call this initially from your onCreate(), AND anytime a user adds something to the DB. All it does is re-bind the listview to a cursor. With all the deprecating methods (requery()), and issues with notifyDataSetChanged(), I decided this was the easiest way.
Please refer this link...it works like charm
Update SimpleCursorAdapter while maintaining scroll position in ListView
for dynamic listview on scroll i added new item from database ..
I did mistake here ..
i was assigning new adapter for each time for same simplecursoradapter .
Instead of creating new adapter.
just use
adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);
You need to call swapcursor() before notifyDataSetChanged() on the adapter.