-1

I'm using SimpleCursorAdapter to retrieve data from a SQLite database and using a custom layout and a listview to display two strings from the database. Now I have two questions:

  1. Do I have to create a custom adapter if I want to display a star in specific rows based on a specific criteria? I have the image already set to invisible in my custom layout and want to set it to visible based on some conditions on the row data itself. I implemented all the tabs as well as the favorites tab and all works properly, I just needed the star icon. I face this problem also with putting the recipe image to a specific rows.

  2. What is the best way to get images dynamically in listviews? I've followed the lazy image tutorial, but I didn't know how to implement it using the CustomCursorAdapter because it was implemented using the baseadapter. What are some links to a lazyloading images with simplecursor tutorials?

 

public class AlternateRowCursorAdapter extends SimpleCursorAdapter{
    int layoutn;
    Cursor mCursor;
    String[] fromn;
    int[] ton;

    LayoutInflater mInflater;

    private int[] colors = new int[] { Color.parseColor("#000000"), Color.parseColor("#303030") };

    public AlternateRowCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, R.layout.listtype, c, from, to);
        this.mCursor = c;
    }

    /**
     * Display rows in alternating colors
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = super.getView(position, convertView, parent);

        ImageView star = (ImageView)view.findViewById(R.id.favoritesicon); // The star I want to show

        if (mCursor.getString(8) == "YES") // shows if the item is in favorites
        {
            star.setVisibility(view.VISIBLE);
        }

        int colorPos = position % colors.length;
        view.setBackgroundColor(colors[colorPos]);

        return view;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohamed Hemdan
  • 303
  • 3
  • 15

1 Answers1

1

Do I have to create a custom adapter?

Yes, you have to create a custom adapter and add a star in your row....

What is the best way to get images dynamically in listviews?

I think you should use universal image loader. See the GitHub project Universal Image Loader for Android.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Thanks Samir for your answer. I will try to have a look into the universal image loader. Do you have any example how can i use the custom adapter to accomplish my task? moreover i did all the code in the activity for the lists with tabs not in the custom adapter. – Mohamed Hemdan Apr 21 '12 at 09:05
  • Mohamed , i haven't any example to accomplish your all task but see custom adapter with image http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – Samir Mangroliya Apr 21 '12 at 09:11
  • I've edited my question with a code I'm trying and get an error. – Mohamed Hemdan Apr 21 '12 at 09:11
  • Thanks Samir. its also using the baseAdapter. – Mohamed Hemdan Apr 21 '12 at 09:15