Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Custom Cursor Adapter Trouble
    text
    copied!<p>I am wondering if someone may be able to point me in the right direction or lend a pair of eyes and see where I am going wrong with my custom cursor adapter class for a listview.</p> <p>Basically, I had it "working", in the sense that it would populate the listview with the firstname from the database, and not move to the next row.Now however, it will throw up an error and not enter into list, so any help is appreciated.</p> <p>Basically what I am having trouble in achieving is reading a name and image path from database and apply it to a row.xml with my custom adapter.Here is my code:</p> <pre><code>public class CustomAdapter extends SimpleCursorAdapter { private LayoutInflater mInflater; private Context context; private int layout; //private Cursor c; String animal_name; String img_path; public CustomAdapter(Context context,int layout, Cursor c, String[] from, int[] to){ super(context, layout, c, from, to); //this.c = c; this.context = context; this.layout = layout; } @Override public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; ViewWrapper wrapper = null; Cursor c = getCursor(); animal_name = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_ANIMALNAME))); img_path = c.getString((c.getColumnIndexOrThrow(MyDBManager.KEY_IMGPATH))); if(row == null){ LayoutInflater inflater = LayoutInflater.from(context); row = inflater.inflate(R.layout.row, null); // row is passed in as "base" variable in ViewWrapper class. wrapper = new ViewWrapper(row); row.setTag(row); } else{ wrapper=(ViewWrapper)row.getTag(); } wrapper.getLabel().setText(animal_name); wrapper.getIcon().setImageResource(R.id.icon); return (row); } </code></pre> <p>}</p> <pre><code>class ViewWrapper{ View base; TextView label = null; ImageView icon = null; ViewWrapper(View base){ this.base = base; } TextView getLabel(){ if(label== null){ label=(TextView)base.findViewById(R.id.author); } return (label); } ImageView getIcon(){ if(icon == null){ icon=(ImageView)base.findViewById(R.id.icon); } return (icon); } </code></pre> <p>}</p> <p>and have been setting the adapter as follows</p> <pre><code>CustomAdapter mAdapter = new CustomAdapter(this, R.layout.row, myCursor, new String[]{"animal_name"}, new int[]{R.id.animal}); this.setListAdapter(mAdapter); </code></pre> <p>Any help is greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload