I'm trying to fill a grid view using data from a db cursor using a custom SimpleCursorAdapter.
My cursor has data (I checked), but nothing is shown in the GridView, and the getView() method is not even called.
Anybody can help? Why is getView() not called?
Thanks
Activity
dbAdapter = new DBAdapter(this);
dbAdapter.open();
Cursor c;
c = dbAdapter.fetchPCList();
startManagingCursor(c);
String[] from = new String[] {};
int[] to = new int[] {};
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new PCIconAdapter(this, R.layout.pc_icon, c, from, to));
c.close();
dbAdapter.close();
Adapter
public class PCIconAdapter extends SimpleCursorAdapter {
private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final int mPCIDIndex;
private final int mClassNameIndex;
private final LayoutInflater mLayoutInflater;
private final class ViewHolder {
public TextView pc_id_view;
public TextView clas_name_view;
}
public PCIconAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mPCIDIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_LM_ID);
this.mClassNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_CLAS_NAME);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.pc_id_view = (TextView) convertView.findViewById(R.id.pc_id);
viewHolder.clas_name_view = (TextView) convertView.findViewById(R.id.clas_name);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String pc_id = mCursor.getString(mPCIDIndex);
String clas_name = mCursor.getString(mClassNameIndex);
viewHolder.pc_id_view.setText(pc_id);
viewHolder.clas_name_view.setText(clas_name);
}
return convertView;
}
}
I had the same problem. I was using an ArrayAdapter and then I changed to a SimpleCursorAdapter, but it doesn't show anything on screen.
I removed
c.close();
dbAdapter.close();
and now its working fine.
because CursorAdapter does not have getView method . it have newView method .
so extend SimpleCursorAdapter and overRide newWiev and other methods like
public class ContactListCursorAdapter extends SimpleCursorAdapter implements Filterable {
private Context context;
private int layout;
public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
return v;
}
#Override
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(People.NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return context.getContentResolver().query(People.CONTENT_URI, null,
buffer == null ? null : buffer.toString(), args, People.NAME + " ASC");
}
}
Your column names array and Views array are empty. So column name to views mapping will never occur. That maybe the reason you are not getting callback on getView
If I remove
c.close();
dbAdapter.close();
it works...
So where am I supposed to close this cursor?...
Related
I am creting music player app and I got music files from device but it is not in shorted order.
It is displaying like this (Not in sorted order)
Code which displays songs in ListView :
public class songlist extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ListView lv_songlist;
public Cursor cursor;
private MediaCursorAdapter mediaAdapter = null;
private String currentFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
lv_songlist = (ListView) findViewById(R.id.songlist);
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
}
}
private class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.displayname, R.id.title, R.id.duration});
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
Duration d = new Duration();
String durationInMin = d.convertDuration(durationInMs);
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);
bindView(v, context, cursor);
return v;
}
}
}
Create string like this :
private String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME+"";
And pass this string to last argument in contentResolver.query method's last parameter :
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, sortOrder);
Here is my SimpleCursorAdapter extension class which I use trying to display information about contacts in a ListView:
private class CustomContactsAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
public CustomContactsAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to, 0);
this.layout = layout;
inflater = LayoutInflater.from(context);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor cur) {
MatrixCursor c = (MatrixCursor) cur;
final String name = c.getString(c.getColumnIndex(COLUMN_NAME));
final String org = c.getString(c.getColumnIndex(COLUMN_ORG));
final byte[] image = c.getBlob(c.getColumnIndex(COLUMN_PHOTO));
ImageView iv = (ImageView) v.findViewById(R.id.contact_photo);
if(image != null && image.length > 3) {
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
}
TextView tname = (TextView) v.findViewById(android.R.id.text1);
tname.setText(name);
TextView torg = (TextView) v.findViewById(android.R.id.text2);
torg.setText(org);
}
}
But when the program reaches the code snippet where I want to get blob data from cursor an UnsupportedOperationException is thrown there with message:
getBlob is not supported
I want to know what am I doing wrong. Also, I pass a MatrixCursor baked by myself as a parameter to the adapter.
That's the implemetaion of getBlob(int) from MatrixCurosr in Android 1.6 and Android 2.3.
public byte[] getBlob(int column) {
throw new UnsupportedOperationException("getBlob is not supported");
}
That's the getBlob(int) implementation for Android ICS
#Override
public byte[] getBlob(int column) {
Object value = get(column);
return (byte[]) value;
}
probably you want to subclass MatrixCursor and implment the getBlob in the ICS way
I am trying to get the selected checkboxes deleted. I have no clue what to do after this code below:
I am using a simple cursor adapter. I also want to know how I could select and deselect all the checkboxes. I had researched everything but can't find any answer. I've been stuck for 3 days. ALso, Im not sure if the code below is even the correct path i need.
public class SecondCheckedListView extends ListActivity {
private Cursor mCursor;
private Long mRowId;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondcheckedlistview);
db.open();
fillData();
}
private void fillData() {
mCursor = db.getAllContacts();
startManagingCursor(mCursor);
String[] from = new String[]{DBAdapter.KEY_FIRSTNAME, DBAdapter.KEY_LASTNAME};
int[] to = new int[]{R.id.textView1, R.id.textView2};
SimpleCursorAdapter d =
new SimpleCursorAdapter(this, R.layout.rowcheckedlistview, mCursor, from, to);
setListAdapter(d);
}
public class MyAdapter extends SimpleCursorAdapter {
private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final int mNameIndex;
private final int mIdIndex;
private final LayoutInflater mLayoutInflater;
private final class ViewHolder {
public TextView firstname;
public TextView lastname;
public CheckBox cBox;
}
public MyAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
this.mIdIndex = mCursor.getColumnIndex(DBAdapter.ROW_ID);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(pos)) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.firstname = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.lastname = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.cBox = (CheckBox) convertView.findViewById(R.id.bcheck);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
String firstname = mCursor.getString(mNameIndex);
String rowId = mCursor.getString(mIdIndex);
}
return convertView;
}
}
}
what do you mean "delete"? You want to remove the checkbox from the listview? You might need
to do something like
viewHolder.RemoveView(cBox);
If you want to select/delsect all checkboxes, you'll probably want that based upon some other botton or checkbox. I do that with another checkbox like so:
private OnClickListener setSelectAllListener = new OnClickListener() {
#Override
public void onClick(View v) {
selectAllTouched = true;
if (!selectAllCheck.isChecked()) {
selectAll = false;
studs.notifyDataSetChanged();
}
else {
selectAll = true;
studs.notifyDataSetChanged();
}
}
};
And then in my custom adapter that does stuff with each checkbox in a listview, I use this:
if (selectAllTouched) {
if(selectAll){
holder.here.setChecked(true);
itemCheckedHere.set(pos, true);
int who= new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 1, attendDate );
}
else{
holder.here.setChecked(false);
itemCheckedHere.set(pos, false);
int who = new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 0, attendDate );
}
}
I used a sparse array, itemCheckedHere, to hold the values of all the check boxes so scrolling doesn't change the values on resued views.
I've an ListView with data from SQLite that works correctly. Now I want to add more things to this list. To each row I want to add EditText and CheckBox. Something like this:
item1 CheckBox
EditText
item2 CheckBox
EditText
This is my List Class:
public class SeleccionarRelacionPregResp extends Activity implements, OnItemClickListener {
private ListView listaTodo;
private ListAdapter uGraduateListAdapter;
private String bundledCodigoPuntoDeControl;
private ArrayList<UndergraduateDetailsPojo> pojoArrayList;
protected DokesimApplication app;
String codigocaja;
Button btnPruebasGuardar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seleccionartodo);
listaTodo = (ListView) findViewById(R.id.ListaTodo);
listaTodo.setOnItemClickListener(this);
pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();
uGraduateListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, populateList());
listaTodo.setAdapter(uGraduateListAdapter);
PuntosDeControl pControlSeleccionado = new PuntosDeControl();
app = (DokesimApplication) getApplicationContext();
pControlSeleccionado = app.getpuntocontrol();
codigocaja = pControlSeleccionado.codigocaja;
}
public List<String> populateList() {
List<String> uGraduateNamesList = new ArrayList<String>();
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query("RelacionPregResp", null,
" relprcodigocaja = '" + codigocaja + "'",
null, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String numeropregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.numeropregunta));
String relprcodigocaja = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.relprcodigocaja));
String codigopregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.codigopregunta));
String pregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.pregunta));
String codigorespuesta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.codigorespuesta));
String respuesta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.respuesta));
String valor = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.valor));
String tipodeguia = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.tipodeguia));
UndergraduateDetailsPojo ugPojoClass = new UndergraduateDetailsPojo();
ugPojoClass.setNumeroPregunta(numeropregunta);
ugPojoClass.setRelPrCodigoCaja(relprcodigocaja);
ugPojoClass.setCodigoPregunta(codigopregunta);
ugPojoClass.setPregunta(pregunta);
ugPojoClass.setCodigoRespuesta(codigorespuesta);
ugPojoClass.setRespuesta(respuesta);
ugPojoClass.setValor(valor);
ugPojoClass.setTipoDeGuia(tipodeguia);
pojoArrayList.add(ugPojoClass);
uGraduateNamesList.add(pregunta);
}
sqliteDatabase.close();
return uGraduateNamesList;
}
#Override
protected void onResume() {
super.onResume();
uGraduateListAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, populateList());
listaTodo.setAdapter(uGraduateListAdapter);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
I need some suggestions or examples of how can I do it.
Thanks for any help.
What you basically need is Custom SimpleCursorAdapter.
I am pasting sample program which has Two TextViews in each list row.
You will have to modify it according to your requirement.
package org.sample;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends SimpleCursorAdapter
{
private Context context;
private Cursor cursor;
private int layout;
public MyAdapter(Context context, int layout, Cursor cursor, String[] from,
int[] to, int flags)
{
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
this.layout = layout;
}
#Override
public int getCount()
{
if (cursor != null)
return cursor.getCount();
else
return 0;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
{
view = inflater.inflate(layout, null);
}
cursor.moveToPosition(position);
String title = cursor.getString(cursor
.getColumnIndex(DBHelper.BOOK_TITLE));
String author = cursor.getString(cursor
.getColumnIndex(DBHelper.BOOK_AUTHOR));
TextView titleTxt = (TextView) view.findViewById(R.id.bTitle);
TextView authorTxt = (TextView) view.findViewById(R.id.bAuthor);
titleTxt.setText(title);
authorTxt.setText(author);
return view;
}
}
Once you are ready with your cursor in MainActivity you can write following code.
myAdapter = new MyAdapter(context, R.layout.row, cursor, new String[]{ DBHelper.BOOK_TITLE, DBHelper.BOOK_AUTHOR }, new int[]{ R.id.bTitle, R.id.bAuthor }, 0);
listView.setAdapter(myAdapter);
You have to create your own class that extends from adapter what you need for example SimpleCursorAdapter, then specific with XML file, which elements will be included to your ListView and override getView() method to controling each item in ListView.
It's recommended to create and use design pattern Holder, which represents arbitrary object that holds child widgets of every row and then you will have full control over items.
So let basic example:
Declaration and init of ListView
this.contactList = new ListView(this);
this.contactList = (ListView) findViewById(R.id.contactList);
this.contactList.setAdapter(new ContactsAdapter());
This ContactsAdapter is your own created adapter.
public ContactsAdapter() {
super(getApplicationContext(),
R.layout.listview, cursor, new String[] {"name", "email", "phone"}, new int[] {R.id.name, R.id.email});
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
rowsWrapper = null;
LayoutInflater inflater = getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview, null, false);
rowsWrapper = new ListWidgetWrapper(convertView);
convertView.setTag(rowsWrapper);
}
else {
rowsWrapper = (ListWidgetWrapper) convertView.getTag();
}
Cursor tempC = cursor;
tempC.moveToPosition(position);
setActivitySettings();
rowsWrapper.getNameColumn().setText(tempC.getString(1));
rowsWrapper.getEmailColumn().setText(tempC.getString(2));
tempC = null;
return convertView;
}
}
rowsWrapper is mentioned design pattern Holder.
public class ListWidgetWrapper {
private View inView;
private TextView nameColumn = null, emailColumn = null, phoneColumn = null;
public ListWidgetWrapper(View inView) {
this.inView = inView;
}
public int getNameId() {
return inView.findViewById(R.id.name).getId();
}
public int getEmailId() {
return inView.findViewById(R.id.email).getId();
}
public TextView getNameColumn() {
if (this.nameColumn == null) {
this.nameColumn = (TextView) inView.findViewById(R.id.name);
}
return this.nameColumn;
}
public TextView getEmailColumn() {
if (this.emailColumn == null) {
this.emailColumn = (TextView) inView.findViewById(R.id.email);
}
return this.emailColumn;
}
}
So i have here TextView elemets, you have here widgets which you want have in ListView.
This require to write much more implementation but it's more faster, less energy consume when you don't use for example design pattern Holder because without it, your rows in ListView would be creating repeatedly when won't be in View and later go back.
More about SimpleCursorAdapter
I've got ListActivity and I am using custom CursorAdapter.
In each item of the list I've got also checkbox...
Now I have in my list screen a perm button, when you press on it,
It should find all the checkboxes which are 'checked' and do some operations on the item which it's checkbox is 'checked'.
How can I retrieve all the checked ones?
I've done focusable:false, so I can use OnClickListener, but I don't know how farther then
this..
Thanks,
some code:
This is in my ListActivity class:
final String columns[] = new String[] { MyUsers.User._ID,
MyUsers.User.MSG, MyUsers.User.LOCATION };
int[] to = new int[] { R.id.toptext, R.id.bottomtext,R.id.ChkBox,
R.id.Location};
Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users");
Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);
startManagingCursor(cursor);
ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this,R.layout.listitem, cursor, columns, to);
this.setListAdapter(myCursorAdapter);
and this is my Custom Cursor adapter class:
public class ListCursorAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public ListCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c)
{
TextView topText = (TextView) v.findViewById(R.id.toptext);
if (topText != null)
{
topText.setText("");
}
int nameCol = c.getColumnIndex(MyUsers.User.MSG);
String name = c.getString(nameCol);
TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext);
if (buttomTxt != null)
{
buttomTxt.setText("Message: "+name);
}
nameCol = c.getColumnIndex(MyUsers.User.LOCATION);
name = c.getString(nameCol);
TextView location = (TextView) v.findViewById(R.id.Location);
if (locationLinkTxt != null)
{
locationLinkTxt.setText(name);
}
}
//Modify to meet your needs
String[] from = new String[]{ YourDbAdapter.KEY_WORDS_ROWID, YourDbAdapter.KEY_WORDS_ROWID};
int[] to = new int[]{ R.id.row_item_checkbox};
mAdapter = new SimpleCursorAdapter(this, R.layout.row_item_with_checkbox, yourDbCursor, from, to);
mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view.getId() == R.id.myCheckbox )
{
CheckBox cb = (CheckBox) view;
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final long id = cursor.getLong(columnIndex); //Your row id
//You can do the necessary work here such as adding to an List or somehting
}
});
return true;
}
return false;
}
});