2013-11-28 56 views
0

這裏我使用的是內容提供商獲得的所有聯繫人,並把它與多選名單,現在我想選中的名稱保存到共享偏愛。 下次打開活動時,檢查的項目應該存在。保存複選框項目的狀態在一個多選列表視圖

我是新來的android。 在此先感謝.....

這裏是我的代碼...

public class Blacklist extends Activity { 
    SharedPreferences pref,selected; 
    String phoneNo, s; 
    SharedPreferences.Editor edit; 
    SharedPreferences.Editor select; 
    ArrayList<String> temp = new ArrayList<String>(); 
    static ArrayList<String> blocked = new ArrayList<String>(); 

    ContentResolver mcursor; 
    ListView con; 
    ArrayList<String> al; 
    ArrayList<String> ph; 
    static ArrayList<String> checked; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.blacklist); 
     con = (ListView) findViewById(R.id.contactlist); 
     al = new ArrayList<String>(); 

     checked = new ArrayList<String>(); 
     // get contacts 
     Cursor cur = getContacts(); 

     // display contacts 
     if (cur != null) { 
      while (cur.moveToNext()) { 
       String name = cur.getString(cur 
         .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
       al.add(name); 

      } 

      // create an array adapter 

      ArrayAdapter<String> ad = new ArrayAdapter<String>(
        getApplicationContext(), 
        android.R.layout.simple_list_item_multiple_choice, al); 
      con.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
      con.setAdapter(ad); 

      // to find the selected names 
      con.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 
        int kunk = 0; 
        ph = new ArrayList<String>(); 
        SparseBooleanArray sparseBooleanArray = con 
          .getCheckedItemPositions(); 

        for (int i = 0; i < con.getCount(); i++) { 
         selected= getSharedPreferences("SELECTED", MODE_PRIVATE); 
         select=selected.edit(); 
         if (sparseBooleanArray.get(i) == true) { 

          select.putBoolean("bool "+arg1, true); 


          String check = con.getItemAtPosition(i).toString(); 

          if (!temp.contains(check)) { 
           // get the no 
           Cursor cursor; // Cursor object 
           String mime; // MIME type 
           int dataIdx; // Index of DATA1 column 
           int mimeIdx; // Index of MIMETYPE column 
           int nameIdx; // Index of DISPLAY_NAME column 

           // Get the name 
           cursor = getContentResolver() 
             .query(ContactsContract.Contacts.CONTENT_URI, 
               new String[] { ContactsContract.Contacts.DISPLAY_NAME }, 
               null, null, null); 
           if (cursor.moveToFirst()) { 
            nameIdx = cursor 
              .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 

            // Set up the projection 
            String[] projection = { 
              ContactsContract.Data.DISPLAY_NAME, 
              ContactsContract.Contacts.Data.DATA1, 
              ContactsContract.Contacts.Data.MIMETYPE }; 

            // Query ContactsContract.Data 
            cursor = getContentResolver().query(
              ContactsContract.Data.CONTENT_URI, 
              projection, 
              ContactsContract.Data.DISPLAY_NAME 
                + " = ?", 
              new String[] { check }, null); 

            if (cursor.moveToFirst()) { 
             // Get the indexes of the MIME type and 
             // data 
             mimeIdx = cursor 
               .getColumnIndex(ContactsContract.Contacts.Data.MIMETYPE); 
             dataIdx = cursor 
               .getColumnIndex(ContactsContract.Contacts.Data.DATA1); 

             // Match the data to the MIME type, 
             // store in variables 
             do { 
              ArrayList<String> phtemp = new ArrayList<String>(); 
              mime = cursor.getString(mimeIdx); 

              if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE 
                .equalsIgnoreCase(mime)) { 
               phoneNo = cursor 
                 .getString(dataIdx); 
               phoneNo = PhoneNumberUtils 
                 .formatNumber(phoneNo); 
               ph.add(phoneNo); 
               if (!phtemp.contains(ph)) { 

                phtemp.add(phoneNo); 
               } 
              } 
             } while (cursor.moveToNext()); 
            } 
           } 

          } 

         } 

        } 
        if (ph != null) { 
         HashSet<String> hs = new HashSet(); 
         hs.addAll(ph); 
         ph.clear(); 
         ph.addAll(hs); 

         for (String l : hs) { 

          // save list in shared preference 
          pref = getSharedPreferences("MY_LOG", MODE_PRIVATE);                     
          edit = pref.edit(); 
          kunk++; 
          s = Integer.toString(kunk); 

          edit.putString(s, l); 

         } 
         edit.putString("size", s); 
         edit.commit(); 
         Toast.makeText(getApplicationContext(), 
           "saved to shared", Toast.LENGTH_LONG).show(); 

         // retrieve the values of blocked numbers and store it 
         // in an array block list 
         pref = getSharedPreferences("MY_LOG", MODE_PRIVATE); 
         String un = pref.getString("size", "0"); 

         for (int i = 1; i <= Integer.parseInt(un); i++) { 
          String no = pref.getString("" + i, "nill"); 
          String non = returnNumberOnly(no); 
            . 
          blocked.add(non); 
         } 

        } 

       } 
      }); 

     } else { 

     } 

    } 

    String returnNumberOnly(String number) { 
     String ss[] = number.split("\\+91"); 
     String numberArray[] = null; 
     if (ss.length == 2) { 
      numberArray = ss[1].split("-"); 
     } else if (ss.length == 1) { 
      numberArray = ss[0].split(" "); 
     } 
     number = ""; 
     for (int i = 0; i < numberArray.length; i++) { 
      number += numberArray[i]; 
     } 
     System.out.println("------------------ IN returnNumberOnly Fn ------ " 
       + number + " RETURNED ------------------------------------ "); 
     return number; 
    } 

    private Cursor getContacts() { 
     Uri uri = ContactsContract.Contacts.CONTENT_URI; 

     Cursor c; 
     mcursor = getContentResolver(); 
     c = mcursor.query(uri, null, null, null, null); 
     return c; 
    } 

} 
+0

ü沒有任何谷歌? –

+0

檢查[這](http://stackoverflow.com/a/11601129/2591002) –

+0

看到這個example..it將HEP you..http://stackoverflow.com/questions/18715556/removing-muliple-items-從-列表視圖 - 使用 - 複選框功能於Android的 – Giridharan

回答

0

的ListView項目上點擊監聽器: -

final Integer index = Integer.valueOf(position); 

      if(!checkedPositions.contains(index))//This is a global array list which contains the checked item states 
       checkedPositions.add(index); 
      else 
       checkedPositions.remove(index); 

      check.setChecked(checkedPositions.contains(index)); 

內GetView()監聽器: -

holder.checkbox1.setChecked(flag); 
      if(flag){ 
       if(!checkedPositions.contains(position)) 
        checkedPositions.add(position); 
      } 
      else 
      { 
       checkedPositions.clear(); 
      } 
      final Integer index = Integer.valueOf(position); 
     holder.checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { 
        if(isChecked){ 
         if(!checkedPositions.contains(index)) 
          checkedPositions.add(index); 
        } 
        else 
         checkedPositions.remove(index); 


       } 
      }); 
      holder.checkbox1.setChecked(checkedPositions.contains(index)); 
+0

thanx ...它的好.. –

0

存放在共享偏好列表。

下一次您的活動開始時,使用共享首選項檢索數組列表。

getView()中放入if條件,如果數組列表包含位置,則將複選框狀態設置爲選中狀態。

例如: -

if(array_list.get(position) == listview_item) // Just for reference. Not actual code 
    checkbox1.setChecked(true); 
else  
    checkbox1.setChecked(false); 

我希望你得到它。

+0

我使用的是多選列表視圖定製適配器,那麼應​​該怎樣來代替檢查bok1 ....如何檢查特定項目是否被選中 –

+0

多項選擇?在你的列表視圖中,每個項目只有一個checbox? –

+0

是一個複選框每個項目..如何使用arraylist中的項目設置檢查我不能指定checkbox.setChecked(true);對。 –

相關問題