How do I bind a GridView to a Custom ContentProvider - android

I'm trying to bind a customer ContentProvider to my activity that holds a GridView
String[] projection = { SAppsDatabase.ID, SAppsDatabase.COL_APP_TITLE};
String[] uiBindFrom = { SAppsDatabase.COL_APP_TITLE };
int[] uiBindTo = { R.id.title };
Cursor apps = managedQuery(
MyProvider.CONTENT_URI, projection, null, null, null);
GridView gridview = (GridView) findViewById(R.id.gridview);
CursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.grid_app_list, apps,
uiBindFrom, uiBindTo);
gridview.setAdapter(adapter);
This is not working.
I manage to bind it to a ListActivity by doing the same as above, and setting:
setListAdapter(adapter);

This will not work for a GridView as the name indicates it is a grid of views
GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.
The grid items are automatically inserted to the layout using a ListAdapter.
but you are providing it a cursor adapter which doesn't make much sense.
Take a look at this sample
If you want to provide a custom adapter then you will have extend adapter and return the view content in the 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

Adding Delete button to a data binding ListView

I've created a ListView and bind it to an adaptor to display data from a table. It works great. I can select on a row and it display the product name that I'm selecting. Now I've added an ImageView in the layout that will act as delete buttons on each row. My problem is I can't figure out how to add the code to make it so that when a user select the ImageView (delete button), it will delete the row. I've searched and found a lot of articles on this topic and tried a lot of them and none really work my code. Do I need to create a getView function? Also I've tried inserting the getTag(), but I couldn't make it work. Can you help me with a sample code that may work with my simple code or point me to the right direction? Here is my code:
private void displayListView() {
prodinputHelper = new DBAdaptorProductInput(this);
prodinputHelper.open();
Cursor cursor = prodinputHelper.fetchAllProductInput();
// The desired columns to be bound
String[] columns = new String[] {
DBAdaptorProductInput.KEY_PRODUCTTYPE,
DBAdaptorProductInput.KEY_PRODUCTNAME,
DBAdaptorProductInput.KEY_MANUFACTURER,
DBAdaptorProductInput.KEY_VISC40,
DBAdaptorProductInput.KEY_VISC100,
DBAdaptorProductInput.KEY_VI,
DBAdaptorProductInput.KEY_DEN15C,
DBAdaptorProductInput.KEY_VISCTEXT,
DBAdaptorProductInput.KEY_BASEOILTYPE,
DBAdaptorProductInput.KEY_BASEOIL,
DBAdaptorProductInput.KEY_ADDITIVES,
DBAdaptorProductInput.KEY_OTHERADDITIVES,
DBAdaptorProductInput.KEY_THICKENER,
DBAdaptorProductInput.KEY_NLGI,
DBAdaptorProductInput.KEY_COMMENT,
DBAdaptorProductInput.KEY_PACKAGES,
DBAdaptorProductInput.KEY_AREA,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.code,
R.id.name,
R.id.manufacturer,
R.id.visc40,
R.id.visc100,
R.id.viscindex,
R.id.den15c,
R.id.visctext,
R.id.baseoiltype,
R.id.baseoil,
R.id.additives,
R.id.otheradditives,
R.id.thickener,
R.id.nlgi,
R.id.comments,
R.id.packages,
R.id.area,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_product_review_info, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//SetOnItemClickListener for the ListView
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the Customer Name from this row in the database.
String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("ProductName"));
Toast.makeText(getApplicationContext(), countryCode, Toast.LENGTH_SHORT).show();
}
});
}
You need to have a custom adapter that extends BaseAdapter or SimpleCursorAdapter.
In the adapter's getView() method set the onClickListener for your ImageView.

How to use an ArrayAdapter to return and show a ListView

I am writing code for an mp3 player I am trying to create. I am only at the start of the project and want to be able to read and show all of the mp3 files that is on my sd card. I don't want to use the direct path method. Now this code I have written gathers all of the mp3 files for me but the only problem is that it does not view them on screen for me. The app shows a blank screen, but doesn't crash.
I got help and advice off of a tutor and I was told to use an ArrayAdapter to view the results but I can't find any help to show it. If anyone could please help that would be great.
Here is my code in the onCreate method;
ListView list;
Cursor cursor;
int columnIndex;
int count;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//list = (ListView) findViewById(R.id.list);
//A array is created that display the 3 fields
String[] displayMusic = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE};
//the cursor displays all of the audio in the sd card, just to limit to .mp3 files now
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, displayMusic, null, null, null);
//this is the loop that gather all of the .mp3 files
int pos = 1;
ArrayList<String> listOfMp3s = new ArrayList<String>();
while (cursor.moveToNext())
{
if(cursor.getString(pos).endsWith("mp3"))
{
listOfMp3s.add(cursor.getString(pos));
}
}
String[] displayFields = new String[] {MediaStore.Audio.Media.DISPLAY_NAME};
int[] displayViews = new int[] {android.R.id.text1};
//setListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews);
ArrayAdapter<String> musicAdapter = new ArrayAdapter<String>(getBaseContext(), R.layout.list_songs, listOfMp3s);
//list.setAdapter(listOfMp3s);
}
Have a look a the simple and straight forward tutorial How to create a ListView using ArrayAdapter in Android.
You need to generate a String array and attach the array to an Array adapter
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, YourStringArray);
and then plug this adapter to your ListView
yourListView.setAdapter(adapter);
Or you could create your own custom adapter by extending ArrayAdapter<YourItem> and attaching YourItem array to it.
I would suggest you to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method to specify exactly what the row data would be.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter

Android: ListView with multiple choice and subitems enabled

I'm trying to get my contacts on a list view. Now I know that using simple_list_item_multiple_choice enables me to select multiple contacts, but it views names only without numbers.
On the other side, simple_list_item_2 can be used to show both name and number, but supports selection of only one contact.
Is there any template that combine them both? If not, how could I build my customized list with both features?
EDIT: This is the code I'm using
CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
new int[] { android.R.id.text1},0);
setListAdapter(adapter);
Here, the second parameter of SimpleCursorAdapter is simple_list_item_multiple_choice but it supports only dealing with android.R.id.text1. So I can use items only, not subitems.
But in the following code
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ,ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { android.R.id.text1,android.R.id.text2},0);
I can give it both ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME and NUMBER to be written in android.R.id.text1 and android.R.id.text2, but can't use multiple choice feature.
As Dipu said, you should make your own customized layout.
To show name and contact, you need two text views, and one check box for checking.
You can start coding from this tutorial:
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
Add one more text view to country_info.xml will solve your problem.
ADDED
To use a custom list view layout, you have to implement your own adapter.
This tutorial (2. Custom ArrayAdapter example) will help you figure out how to do that.
http://www.mkyong.com/android/android-listview-example/
The answer provided by Heejin is excellent, but it's not important to implement a custom ArrayAdaptor. What only I needed to do is to write a custom layout and send it to the SimpleCursorAdaptor constructor.
The custom layout represent the layout of each item in the list view. I need each row to contain a CheckedTextView and another small TextView as a subitem. So I've made a layout called row_view.xml with the following content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="#+id/checkedTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="CheckedTextView" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Then I've just used it in the constructor
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { R.id.checkedTextView, R.id.textView},0);
setListAdapter(adapter);
This is the full code
public class MultipleContacts extends ListActivity implements OnItemClickListener {
private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone._ID
,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
,ContactsContract.CommonDataKinds.Phone.NUMBER
};
SimpleCursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiple_contacts);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(this);
// Get a cursor with all people
CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
adapter = new SimpleCursorAdapter(this,
R.layout.row_view, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { R.id.checkedTextView, R.id.textView},0);
setListAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//what to do when an item is clicked
CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextView);
Toast.makeText(this, checkedTextView.getText(), Toast.LENGTH_SHORT).show();
}
}
Please note that I've two layouts, one for the list view itself(called multiple_contacts) and the provided layout here (called row_view) is the layout for each item in the list view. All what I need from multiple_contacts is to write setContentView(R.layout.multiple_contacts);

SimpleCursorAdapter not reloading data&view on insert in ContentProvider

this code is working fine - i'm loading a bunch of rows into the SimpleCursorAdapter and the ListView displays them. nice.
SimpleCursorAdapter adapter;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.main_lv_projects);
lv.setOnItemClickListener(this);
String[] columns = new String[] { CompassDataProvider.ORG_NAME };
int[] names = new int[] { R.id.organisation_row_name};
cursor = this.managedQuery(CompassDataProvider.CONTENT_URI_ORGANISATIONS, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.organisation_row, cursor, columns, names);
lv.setAdapter(adapter);
startManagingCursor(cursor);
}
But when i'm inserting a new row via
long rowID = db.getWritableDatabase().insert(CompassDBHelper.ORGS_TABLE_NAME, "", values);
getContext().getContentResolver().notifyChange(CONTENT_URI_ORGANISATIONS, null);
return Uri.withAppendedPath(CONTENT_URI_ORGANISATIONS, ""+rowID);
the list view is not updateing itself - i thought the SimpleCursorAdapter is notified an can then reload its view - not?
the new row is created - i checked on this
when i'm using
cursor.requery();
adapter.notifyDataSetChanged();
in my UI threat the new data gets loaded correctly... whats the observer/listener pattern here that i did not get? :)
thanks,
Martin
The cursor is being passed into the SimpleCursorAdapter by value and not reference. So, calling cursor.requery has no effect. To get this to work the way you want, you're really going to need to implement a listAdapter and process your cursor in there.

Categories

Resources