0

20 個の項目がある ListView があります。10 項目は私の電話の連絡先で、残りの 10 項目は既定のブラウザーのブックマーの 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);

            }
        });

上記のコードは、クリックイベントの処理時にリストビューに連絡先を追加しています。

2 番目の要件では、次のコードを使用して、リストビューを bookmarks で拡張しました。

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

人口が増えていますが、クリックイベントを処理できません。ユーザーがクリックしたときにブラウザでURLを開きたい。

4

1 に答える 1