标签: android-cursor

查询是否返回空游标

我总是看到人们说它的游标永远不会为空,所以你不需要检查是否为null,但是还有其他人说游标可以为null而你总是要检查它.

所以看看这里的查询方法,它确实显示返回null,所以我不知道我是否只是缺少一些东西,或者是人们说光标永远不会出错?

你或者你不需要在查询Cursor时检查是否为null?

android android-cursor android-sqlite

4
推荐指数
1
解决办法
1305
查看次数

在Android SQLite中,直接使用Cursor比创建模型对象更节省内存?

在大多数Android示例代码中,从SQLite数据库填充ListView有两种方式,

  1. 预取数据,以列表-执行查询,对于每一行创建模型对象,然后将其添加到列表关闭光标,然后填充ListView控件列表.

  2. 没有List和Model对象 -执行查询并按照Cursor使用moveToFirst,moveToLast,move,根据需要填充ListView.

现在我想知道,在Android中,上述哪种方法的内存效率更高?

android android-listview android-cursor android-sqlite android-memory

4
推荐指数
1
解决办法
1051
查看次数

Android:BaseAdapter 如何?

好吧,我一直在仔细搜索,并且在实现 BaseAdapter 时遇到了一些问题。

我已经能够 按照上面的示例实现一个简单的光标适配器http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html 。

这里有一个非常好的 BaseAdapter 示例:List14 google example

我想使用 BaseAdapter 创建自己的列表适配器来显示列表视图,其中包含数据库中的多个项目。我知道这可以使用简单游标适配器来完成,但我希望以不同的方式处理行,因此我希望能够通过重写 getView 来绘制每一行。数据将从游标中提取。

我知道这段代码对于获取光标数据来说很难看,但假设我已经填充了光标。如果第 8 列包含图像资源 id,您对此有何建议。:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    cursor.moveToPosition(position);
    ImageView i = new ImageView(mContext);
    i.setImageResource(cursor.getShort(8));
    i.setAdjustViewBounds(true);
    i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    return i;
}
Run Code Online (Sandbox Code Playgroud)

您有任何使用光标绘制 BaseAdapter 的好示例吗?

android listview adapter android-cursor

3
推荐指数
1
解决办法
4万
查看次数

Android LoaderManager和CursorLoader混淆

我试图将我的Android应用程序转换为使用LoaderManager和CursorLoader.基本上,我有一个包含ADDRESS列和DISTANCE列的SQLite数据库,我想将列值加载到ListView行中.

现在,我已经做了很多研究,所有内容都指向了这个教程:http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/

这是一个很好的教程,但有几件事我还是不明白.主要是,如何构造传递给'new CursorLoader()'的内容URI?我没有使用任何外部数据,例如设备联系人等.

请参阅下面的代码.我对如何为BASE_URI生成值感到困惑:

public class FavoritesFragment extends ListFragment implements
    LoaderManager.LoaderCallbacks<Cursor> {

SimpleCursorAdapter mAdapter;

static final String[] FAVORITES_SUMMARY_PROJECTION = new String[] {
        MyApplication.COLUMN_ID, MyApplication.COLUMN_ADDRESS,
        MyApplication.COLUMN_DISTANCE, };

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

    mAdapter = new SimpleCursorAdapter(getActivity(),
            R.layout.locations_list_row, null, new String[] {
                    MyApplication.COLUMN_ADDRESS,
                    MyApplication.COLUMN_DISTANCE }, new int[] {
                    R.id.address2, R.id.distance }, 0);
    setListAdapter(mAdapter);

    getLoaderManager().initLoader(0, null, this);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Insert desired behavior here.
}

public …
Run Code Online (Sandbox Code Playgroud)

java android android-cursor android-loadermanager

3
推荐指数
1
解决办法
4914
查看次数

使用SimpleCursorAdapter.ViewBinder更改TextView的颜色

我正在为Android开发一个闹钟应用程序,我希望在主屏幕上显示警报列表.其中的每一行都ListView在xml文件中定义.我希望TextViews每周的每一天都能分开.程序将检查sqlite db是否为例如.mondayis = 1的值,然后将其颜色更改TextView为红色.我写了这段代码,但这不起作用.怎么了?

private void fillData() {

    // Get all of the notes from the database and create the item list
    Cursor c = db.fetchAllAlarms();
    startManagingCursor(c);

    String[] from = new String[] { db.KEY_TIME, db.KEY_NAME };
    int[] to = new int[] { R.id.time, R.id.alarmName };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter alarms =
        new SimpleCursorAdapter(this, R.layout.alarm_row, c, from, to);
        alarms.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public …
Run Code Online (Sandbox Code Playgroud)

sqlite android simplecursoradapter android-cursor android-viewbinder

3
推荐指数
1
解决办法
9079
查看次数

将事件添加到android日历失败

我正在尝试以编程方式将事件添加到用户的日历中.我在StackOverflow和其他地方跟踪了无数的指南,但是没有一个会在我的日历中显示那个该死的事件.这是我目前的代码:

public void saveGamesToCalendar() {
    showCalendarPopup();
}

private void showCalendarPopup() {
    final ContentResolver cr;
    final Cursor result;
    final Uri uri;
    List<String> listCals = new ArrayList<String>();
    final String[] projection = new String[] {CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.NAME,};

    uri = CalendarContract.Calendars.CONTENT_URI;

    cr = context.getContentResolver();
    result = cr.query(uri, projection, null, null, null);
    if(result.getCount() > 0 && result.moveToFirst()) {
        do {
                listCals.add(result.getString(result.getColumnIndex(CalendarContract.Calendars.NAME)));
    } while(result.moveToNext());
}
CharSequence[] calendars = listCals.toArray(new CharSequence[listCals.size()]);

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Calendar to use:");
builder.setItems(calendars, new DialogInterface.OnClickListener() {
    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

android calendar android-contentresolver android-cursor

3
推荐指数
1
解决办法
5691
查看次数

无法识别WhatsApp联系人

我正在尝试编写一个Android程序,可以识别给定的联系号码是否与WhatsApp相关联.我设法找出一个特定的联系人姓名是否有WhatsApp帐户.如何找出与该联系人姓名相对应的联系人是否有WhatsApp?目前我正在使用以下代码:

public void identifyWhatsappContact() {
    Cursor c = ctx.getContentResolver().query(
            RawContacts.CONTENT_URI,
            new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
            RawContacts.ACCOUNT_TYPE + "= ? AND " + StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
            new String[] { "com.whatsapp", "John Doe" },
            null);

    ArrayList<String> myWhatsappContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext())
    {
        myWhatsappContacts.add(c.getString(contactNameColumn));
        Log.v("WHATSAPP", c.getString(contactNameColumn)+"");
    }
}
Run Code Online (Sandbox Code Playgroud)

我能够确定"John Doe"是否有WhatsApp,但我无法弄清楚"John Doe"的哪个电话号码有WhatsApp(假设联系人有多个电话号码).有人可以帮帮我吗?我已经在这里尝试过这个解决方案,但它对我不起作用.

android android-contacts android-cursor rawcontactid whatsapp

3
推荐指数
1
解决办法
1433
查看次数

两个日期之间的 Android 通话记录数据和来自同一号码的通话总持续时间。

想要获得今天、昨天、过去 7 天和过去 30 天的通话记录历史记录以及我想显示该特定号码的来电和去电的总持续时间。

假设abc有 3 个呼出和 1 个呼入。我应该得到这些电话的总持续时间。

让我知道我们是否可以通过游标GroupByORDER BY子句获取持续时间并调用日志,而不是循环和添加持续时间。只需给我粗略的结构以获得更好的解决方案,并且可以有效地工作。

         String[] whereValue=null;
        Calendar calendar = Calendar.getInstance();
        String currentDate = String.valueOf(calendar.getTimeInMillis());
        switch (period) {
            case DAY:
                whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            case YESTERDAY:
                whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            case WEEK:
                whereValue  = new String[]{getTimeFrom(period),currentDate};

                break;
            case MONTH:
                    whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            default:
                Log.d(Utils.LOG_TAG, "Error:");
        }
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Uri callUri = Uri.parse("content://call_log/calls");

        Cursor cur = context.getContentResolver().query(callUri, null, android.provider.CallLog.Calls.DATE+" …
Run Code Online (Sandbox Code Playgroud)

android duration calllog android-contentprovider android-cursor

3
推荐指数
1
解决办法
3525
查看次数

代码如何从 Android 画廊加载图像

我有用于从图库加载图像的代码,但我真的不明白它是如何工作的。这是代码。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //Browse Gallery is requested

        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        loadImage(picturePath);         //load picture according the path
        image_View.setImageBitmap(pic); //Show the selected picture
    }
}
Run Code Online (Sandbox Code Playgroud)

Uri selectedImage = data.getData();

从意图中获取所选图像的 uri

String[] filePathColumn …

android image android-cursor

3
推荐指数
1
解决办法
1033
查看次数

cursor.setNotificationUri()和getContentResolver()之间的差异.incomeChange(uri,null)

我是新到Android任何一个可以告诉我是什么样的区别cursor.setNotificationUri(),并getContentResolver().notifyChange(uri,null)同时实现内容提供商.

我已经看到cursor.setNotificationUri()query()方法中使用,并且使用更新或插入时getContentResolver().notifyChange().

我几乎没有理解getContentResolver().notifyChange()通知解析器某些数据已被更改但是那里做了cursor.setNotificationUri()什么?

android android-contentprovider android-cursor

3
推荐指数
1
解决办法
1019
查看次数