0

I'm doing an Android application where I have to display in the listview records from my database. However, the photos I have called the drawables and assigned it to array. How can I display the photos on the ImageView using a SimpleCursorAdapter?

Here's what I have tried so far:

 static final int[] imgs = { R.drawable.dinaretreat, // 0 R.drawable.cobterrace, // 1 R.drawable.ventassostreet, // 2 R.drawable.summerhillblvddrouin, // 3 R.drawable.todmanstreetdrouin, // 4 R.drawable.aqueductroad // 5 }; private DBHelper dbHelper; SimpleCursorAdapter dataAdapter; Cursor cursor; Button back, filter; TextView highest, lowest, location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewhouseandland); initControls(); displayRecords(); } private void displayRecords() { // TODO displayRecords // TODO CheckDBConnection checkDatabaseConnection(); // TODO cursor = dbHelper.getAllHouses(); // The desired columns to be bound String[] columns = new String[] { DBHelper.KEY_HOUSE, DBHelper.KEY_PRICE }; // the XML defined views which the data will be bound to int[] to = new int[] { R.id.image, R.id.text1, R.id.text2 }; // create the adapter using the cursor pointing to the desired data //as well as the layout information dataAdapter = new SimpleCursorAdapter( this, R.layout.listrow, cursor, columns, to, 0); lv.setAdapter(dataAdapter); } 

I'm having a hard time how to display the images on my listview. Any ideas? I'd gladly appreciate your help. Thanks.

3 Answers 3

1

What you need is a class (-> CustomAdapter) that extends from BaseAdapter. There you can define your own Layout for each listItem.

So create a custom layout for your row items (like I understand it should be just an ImageView). This Layout should be customized in the getView() method of your BaseAdapter. But please read this article to ensure a "smooth scrolling" of you listView.

Your CustomAdapter should look like this:

public class MyCustomAdapter extends BaseAdapter{ // MEMBER private int[] mImgs; public MyCustomAdapter(Activity context, int[] imgs){ mImgs = imgs; mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return mImgs.length; } @Override public Object getItem(int position) { return mImgs[position]; } @Override public long getItemId(int position) { return position; } // --- detail settings of single views @Override public View getView(int position, View convertView, ViewGroup parent) { // check if your layout is null if(convertView == null){ convertView = createNewView(); } // get the ViewHolder of your view ViewHolder holder = (ViewHolder) convertView.getTag(); // --- set data to listItem holder.imageView.setImageDrawable(mImgs[position]) return convertView; } // new View private View createNewView() { View convertView = null; int layout = 0; ViewHolder holder = new ViewHolder(); layout = R.layout.customlayout; convertView = mInflater.inflate(layout, null); holder.imageView = (ImageView) convertView.findViewById(R.id.customlayout_imageview); convertView.setTag(holder); return convertView; } //ViewHolder public static class ViewHolder{ public ImageView imageView; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, but I'm using cursoradapter? I'm getting the data from my database
It shoul be the same. Create a CustomAdapter that extends from CursorAdapter-> There you also got a method called getView() where you can inflate your customlayout and set the data from your cursor. Also your constructor should be nearly the same -> instead delivering an Integer-Array (like my example) you have to deliver your cursor. Also don't forget to adapt the other methods (getCount(), ...).
0

first of all: parameters "from" and "to" should have the same size, second: use SimpleCursorAdapter. ViewBinder

1 Comment

Can you give me some code snippets to get me started? Thanks.
0

Add a column such as DBHelper.KEY_IMAGE_TYPE, fix the size of "from" as the same as "to",the sample code using ViewBinder is like this:

 // The desired columns to be bound String[] columns = new String[] { DBHelper.KEY_IMAGE_TYPE, DBHelper.KEY_HOUSE, DBHelper.KEY_PRICE }; // the XML defined views which the data will be bound to int[] to = new int[] { R.id.image, R.id.text1, R.id.text2}; SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, columns, to, 0); dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { switch (view.getId()) { case R.id.image: int imageType = cursor.getInt(columnIndex); int imageToBeShown=0; switch (imageType) { case TYPE_ONE: imageToBeShown=imgs[0]; break; case TYPE_TWO: imageToBeShown=imgs[1]; break; case TYPE_THREE: imageToBeShown=imgs[2]; break; case TYPE_FOUR: imageToBeShown=imgs[3]; break; case TYPE_FIVE: imageToBeShown=imgs[4]; break; case TYPE_SIX: imageToBeShown=imgs[5]; break; } ((ImageView)view).setImageResource(imageToBeShown); return true; } return false; } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.