Android How to use Simple Cursor Adapter / Custom Cursor Adapter to load data from database and put in a row with text and image

StackOverflow https://stackoverflow.com/questions/21320331

Question

I am trying to customize a list in Android with rows with textviews and image view.

There are: Three textviews (TextViewPurpose, TextViewStart, TextViewInfo) I can directly get from the database (purpose, start, info)

Two more textviews (TextViewCO2, TextViewCalory) I need to calculate based on the values in the database (I want to calculate CO2 and Calory based on the distance, which is also in the database)

And an image view (ImageTripPurpose) based on two values (If it is not uploaded, I use an image. If it is uploaded, the image will be based on the purpose).

The existing code is only putting three textviews to the row.

Cursor allTrips = mDb.fetchAllTrips();
SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(),
                R.layout.saved_trips_list_item, allTrips, new String[] {
                        "purp", "start", "info" }, new int[] {
                        R.id.TextViewPurpose, R.id.TextViewStart, R.id.TextInfo });
lv.setAdapter(sca);

When I simply want to put an image on each row, I used this but it does not work.

sca.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
               /** Binds the Cursor column defined by the specified index to the specified view */
               public boolean setViewValue(View view, Cursor cursor, int columnIndex){
                   if(view.getId() == R.id.icon){
                       ((ImageView)view).setImageResource(R.drawable.commute);
                       return true; //true because the data was bound to the view
                   }
                   return false;
               }
            });

Also, it is said that SimpleCursor Adapter is not encouraged because of delay in loading data. LoaderManager and CursorLoader are suggested. What should I do to solve these problems?

Was it helpful?

Solution

Here is an exemple how it can be done. Sorry some mistypes, it was typed from ipad.. Just to give you an idea.

          class MyFragment extends ListFragment {

            private CursorAdapter mAdapter;

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                String[] from = new String[] { "purp", "start", "info" };
                int[] to = new int[] {R.id.TextViewPurpose, R.id.TextViewStart, R.id.TextInfo };
                mAdapter = MyCursorAdapter(getActivity(),
                        R.layout.saved_trips_list_item, allTrips, 
                        null,
                        from , 
                        to, 
                        CursorAdapter.FLAG_REGISTER_CONTENT_OBSERV);
            }

            public void onViewCreated(View view, Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                new AsyncTask<Void, Void, Cursor>() {
                    protected Long doInBackground(Void... voids) {
                        return mDb.fetchAllTrips();
                    }

                    protected void onPostExecute(Cursor result) {
                        mAdapter.changeCursor(result);
                    }
                }.execute();

            }

            public static class MyCursorAdapter extends SimpleCursorAdapter {
                MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
                    super(context, layout, c, from, to, flags)
                }

                public bindView(View view, Context context, Cursor cursor) {
                    ImageView imageView = (ImageView) view.findById(R.id.imageView);
                    imageView.setImageResource(R.drawable.commute);
                }
            }

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top