【问题标题】:Android How to use Simple Cursor Adapter / Custom Cursor Adapter to load data from database and put in a row with text and imageAndroid 如何使用简单游标适配器/自定义游标适配器从数据库中加载数据并将文本和图像放在一行中
【发布时间】:2014-01-23 22:06:56
【问题描述】:

我正在尝试在 Android 中自定义一个列表,其中包含带有文本视图和图像视图的行。

有: 三个文本视图(TextViewPurpose、TextViewStart、TextViewInfo)我可以直接从数据库中获取(目的、开始、信息)

另外两个textview(TextViewCO2,TextViewCalory)我需要根据数据库中的值来计算(我想根据距离计算CO2和卡路里,也在数据库中)

以及一个基于两个值的图像视图(ImageTripPurpose)(如果不上传,我使用图像。如果上传,图像将基于用途)。

现有代码仅将三个文本视图放入该行。

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);

当我只想在每一行上放一张图片时,我使用了这个,但它不起作用。

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;
               }
            });

另外,据说 SimpleCursor Adapter 不被鼓励,因为加载数据有延迟。建议使用 LoaderManager 和 CursorLoader。我应该怎么做才能解决这些问题?

【问题讨论】:

    标签: android android-listview simplecursoradapter android-cursoradapter


    【解决方案1】:

    这是一个如何做到的例子。抱歉打错了,是从 ipad 打出来的。。只是给你个思路。

              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);
                    }
                }
    
            }
    

    【讨论】:

    • 对于函数bindView,正确的格式是什么?谢谢你的帮助!当你到达你的电脑时,我可以等你。我使用 getView 并在那里获得了图像,但由于它是数据库,我认为我不应该这样做。对吗?
    • 好吧,我会照着做。在 getView 中,您根据条件返回布局,在 bindView 中,您根据数据填充布局。我会尝试更新示例。
    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多