0

我有一个 ListView,其中有 20 个项目。10 项是我手机的联系人,其余 10 项是我的默认浏览器的 bookmars url。我想要的是:1)当我点击联系人(前10个)时,它应该带我到联系人应用程序中的那个部分联系人。2)当我单击书签 url(其余 10 个)时,它应该在浏览器中打开该 url。

我使用以下代码实现了我的第一个要求:

 ArrayList<HashMap<String, Object>> listitem = new ArrayList<HashMap<String, Object>>();
        mSharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        Boolean contact_check = mSharedPrefs.getBoolean("contact_check", true);
        Boolean bookmark_check = mSharedPrefs
                .getBoolean("bookmark_check", true);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (contact_check) {
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            HashMap<String, Object> map = new HashMap<String, Object>();
                            map.put("name", name);
                            map.put("phone no", phoneNo);
                            // map.put("Bookmarks", faves.getString(titleIdx));
                            listitem.add(map);

                        }
                        pCur.close();
                    }

                }

            }
        }

SimpleAdapter listitemAdapter = new SimpleAdapter(this, listitem,
                R.layout.list_style, new String[] { "name", "phone no" },
                new int[] { R.id.topTextView, R.id.bottomTextView });
        lv.setAdapter(listitemAdapter);



        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                RelativeLayout lr = (RelativeLayout) arg1;
                TextView mText = (TextView) lr.getChildAt(1);

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
                i.setData(Uri
                        .fromParts("tel", mText.getText().toString(), null));
                startActivity(i);

            }
        });

上面的代码是在我的列表视图中添加联系人来处理点击事件。

对于我的第二个要求,我使用以下代码扩展了我的 listview 书签:

String[] requestedColumns = { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };

@SuppressWarnings("deprecation")
final Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
        Browser.BookmarkColumns.BOOKMARK + "=1", null, null);
Log.d("Bookmarks", "Bookmarks count: " + faves.getCount());
faves.moveToFirst();
int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
final int urlIdx = faves.getColumnIndex(Browser.BookmarkColumns.URL);

if (bookmark_check) {
    while (!faves.isAfterLast()) {
        Log.d("SimpleBookmarks", faves.getString(titleIdx));
        Log.d("SimpleBookmarks url", " " + faves.getString(urlIdx));
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name", faves.getString(titleIdx));
        map.put("phone no", faves.getString(urlIdx));
        listitem.add(map);
        faves.moveToNext();
    }

它被填充但我无法处理它的点击事件。当用户点击它时,我想在浏览器中打开它。

4

1 回答 1

3

对您的方法进行一些调整:

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            if (arg2 < 10)
            {
                RelativeLayout lr = (RelativeLayout) arg1;
                TextView mText = (TextView) lr.getChildAt(1);

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
                i.setData(Uri.fromParts("tel", mText.getText().toString(), null));
                startActivity(i);
            }
            else
            {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getCurrentUri));
                //getCurrentUri is the uri at position arg2-10
                startActivity(browserIntent);
            }

        }
    });
于 2013-08-28T09:49:26.770 回答