Example usage for android.widget CursorAdapter CursorAdapter

List of usage examples for android.widget CursorAdapter CursorAdapter

Introduction

In this page you can find the example usage for android.widget CursorAdapter CursorAdapter.

Prototype

public CursorAdapter(Context context, Cursor c, int flags) 

Source Link

Document

Recommended constructor.

Usage

From source file:com.android.development.ExceptionBrowser.java

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    Cursor cursor = getContentResolver().query(Checkin.Crashes.CONTENT_URI,
            new String[] { Checkin.Crashes._ID, Checkin.Crashes.DATA }, null, null, null);

    if (cursor != null) {
        startManagingCursor(cursor);/*w  ww.  j  ava2s  .  c  o m*/

        setListAdapter(new CursorAdapter(this, cursor, true) {
            public View newView(Context context, Cursor c, ViewGroup v) {
                return new CrashListItem(context);
            }

            public void bindView(View view, Context c, Cursor cursor) {
                CrashListItem item = (CrashListItem) view;
                try {
                    String data = cursor.getString(1);
                    CrashData crash = new CrashData(new DataInputStream(
                            new ByteArrayInputStream(Base64.decodeBase64(data.getBytes()))));

                    ThrowableData exc = crash.getThrowableData();
                    item.setText(exc.getType() + ": " + exc.getMessage());
                    item.setCrashData(crash);
                } catch (IOException e) {
                    item.setText("Invalid crash: " + e);
                    Log.e(TAG, "Invalid crash", e);
                }
            }
        });
    } else {
        // No database, no exceptions, empty list.
        setListAdapter(new BaseAdapter() {
            public int getCount() {
                return 0;
            }

            public Object getItem(int position) {
                throw new AssertionError();
            }

            public long getItemId(int position) {
                throw new AssertionError();
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                throw new AssertionError();
            }
        });
    }
}

From source file:com.github.michalbednarski.intentslab.providerlab.proxy.LogViewerFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    setListAdapter(new CursorAdapter(getActivity(), data, false) {
        @Override//ww w .j  ava 2s.c  o  m
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(android.R.layout.simple_list_item_2, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // First row: method and app name
            final String appName = getActivity().getPackageManager().getNameForUid(cursor.getInt(3));
            final String methodName = methodIdToName(cursor.getInt(1));
            ((TextView) view.findViewById(android.R.id.text1)).setText(methodName + "() by " + appName);

            // Second row: uri and exception
            final String uri = cursor.getString(2);
            String exception = cursor.getString(4);
            SpannableStringBuilder text2 = new SpannableStringBuilder(uri);
            if (exception != null) {
                text2.append("\n");
                final int start = text2.length();
                text2.append(exception);
                final int end = text2.length();
                text2.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                // TODO: result / empty result warning
            }
            ((TextView) view.findViewById(android.R.id.text2)).setText(text2);
        }
    });
}

From source file:tw.idv.palatis.danboorugallery.PostListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // enable progress icon
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminate(true);//from  www  .ja  va 2 s  .c  om

    setContentView(R.layout.activity_post_list);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // setup drawer
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLeft = (RelativeLayout) findViewById(R.id.left_drawer);
    mDrawerRight = (RelativeLayout) findViewById(R.id.right_drawer);
    ListView hostsList = (ListView) findViewById(R.id.host_list);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        @Override
        public void onDrawerStateChanged(int newState) {
            super.onDrawerStateChanged(newState);

            if (newState == DrawerLayout.STATE_DRAGGING)
                mUiHider.show();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
        }

        public void onDrawerClosed(View view) {
            if (!mIsDoingSearch && !mDrawerLayout.isDrawerVisible(mDrawerLeft)
                    && !mDrawerLayout.isDrawerVisible(mDrawerRight)) {
                mUiHider.setAutoHideDelay(AUTO_HIDE_DELAY_MILLIS);
                mUiHider.hide();
            }
        }

        public void onDrawerOpened(View drawerView) {
            mUiHider.setAutoHideDelay(UiHider.AUTO_HIDE_DELAY_DISABLED);
            mUiHider.show();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    HostsTable.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            getLoaderManager().restartLoader(R.id.loader_host_ids, null, PostListActivity.this);
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
            mHostsAdapter.swapCursor(null);
        }
    });
    mHostsAdapter = new CursorAdapter(this, null, false) {
        private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked) {
                ViewHolder holder = (ViewHolder) button.getTag(R.id.view_tag_view_holder);
                Host host = SiteSession.getHostById(holder.host_id);
                host.enabled = checked;
                HostsTable.addOrUpdateHost(host);
            }
        };

        class ViewHolder {
            int host_id;
            TextView title;
            TextView summary;
            CheckBox toggle;

            public ViewHolder(View view, int host_id) {
                this.host_id = host_id;
                this.title = (TextView) view.findViewById(android.R.id.title);
                this.summary = (TextView) view.findViewById(android.R.id.summary);
                this.toggle = (CheckBox) view.findViewById(android.R.id.toggle);
                this.toggle.setTag(R.id.view_tag_view_holder, this);
            }
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.listitem_drawer_host_item, parent, false);
            view.setTag(R.id.view_tag_view_holder,
                    new ViewHolder(view, cursor.getInt(HostsTable.INDEX_HOST_DATABASE_ID)));
            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            Host host = SiteSession.getHostById(cursor.getInt(HostsTable.INDEX_HOST_DATABASE_ID));
            if (host == null)
                return;

            ViewHolder holder = (ViewHolder) view.getTag(R.id.view_tag_view_holder);

            holder.host_id = host.id;
            holder.toggle.setChecked(host.enabled);
            holder.title.setText(host.name);
            holder.summary.setText(host.url);
            holder.toggle.setOnCheckedChangeListener(mOnCheckedChangeListener);
        }
    };
    DrawerListAdapter adapter = new DrawerListAdapter(this, mHostsAdapter);
    hostsList.setAdapter(adapter);
    hostsList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
    hostsList.setOnItemClickListener(this);
    hostsList.setOnItemLongClickListener(this);

    getLoaderManager().initLoader(R.id.loader_host_ids, null, this);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mUiHider = new UiHider(AUTO_HIDE_DELAY_MILLIS, new UiHider.OnVisibilityChangeListener() {
        @Override
        public void onVisibilityChange(boolean visible) {
            if (visible)
                getActionBar().show();
            else
                getActionBar().hide();
        }
    });

    if (savedInstanceState == null) {
        mPostListFragment = new PostListFragment();
        getFragmentManager().beginTransaction().replace(R.id.post_list_container, mPostListFragment).commit();
    }
}

From source file:org.catnut.plugin.zhihu.ZhihuItemsFragment.java

private void initSearchLoader() {
    mSearchAdapter = new CursorAdapter(getActivity(), null, 0) {
        @Override/*from   w  w w . jav a  2 s  .  com*/
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null);
        }

        @Override
        public CharSequence convertToString(Cursor cursor) {
            return cursor.getString(1);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView tv = (TextView) view;
            tv.setText(cursor.getString(1));
        }
    };
    mSearchLoader = new LoaderManager.LoaderCallbacks<Cursor>() {
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            String keywords = args.getString(Constants.KEYWORDS);
            return new CursorLoader(getActivity(), CatnutProvider.parse(Zhihu.MULTIPLE), PROJECTION,
                    new StringBuilder(Zhihu.TITLE).append(" like ").append(CatnutUtils.like(keywords))
                            .append(" or ").append(Zhihu.DESCRIPTION).append(" like ")
                            .append(CatnutUtils.like(keywords)).toString(),
                    null, Zhihu.LAST_ALTER_DATE + " desc"

            );
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            mSearchAdapter.swapCursor(data);
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            mSearchAdapter.swapCursor(null);
        }
    };
    mSearchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor c = (Cursor) mSearchAdapter.getItem(position);
            viewItem(c, id);
        }
    });
    mSearchView.setAdapter(mSearchAdapter);
}