2013-08-28 73 views
0

我有一個ListView中有20個項目。 10個項目是我的手機的聯繫人,其餘10個是默認瀏覽器的bookmars網址。我想要的是: 1)當我點擊聯繫人(前10)時,它應該帶我到聯繫應用程序的那部分聯繫人。 2)當我點擊書籤網址(其餘的10)時,它應該在瀏覽器中打開該網址。處理ListView中的Click事件

我實現了我的下面的下面的代碼的第一個要求:

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

      } 
     }); 

上面的代碼被添加的聯繫人在我的列表視圖上處理的點擊事件。

對於我的第二個要求我擴展我的列表視圖使用書籤,用下面的代碼:

字符串[] 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(); 
    } 

它得到填充,但我無法處理其點擊事件。我想在用戶點擊瀏覽器時打開瀏覽器中的網址。

+0

你做你的TextView'機器人:可調焦= FALSE' ... ????? – SilentKiller

+0

是的,我做了我的textView android:focusable = false – jad

回答

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

     } 
    }); 
+0

這樣一個簡單的解決方案,沒有點擊我的心...謝謝:) – jad

+0

歡迎您;) – Dyna

相關問題