2012-03-23 174 views
1

我曾試圖通過佈局充氣機將複選框放入列表視圖中,但我得到了成功,但問題是當我選擇多個聯繫人時沒有問題,但是當我取消選擇時&當我滾動下來&再回到那個取消複選框它都會自動選中...複選框在列表視圖中選擇多個聯繫人

public class Contactlist_selfActivity extends ListActivity { 
/** Called when the activity is first created. */ 

private ArrayList<contact> contact_list = null; 
private ProgressDialog mProgressDialog = null; 
private contactAdapter mContactAdapter = null; 
private Runnable mViewcontacts = null; 
private SparseBooleanArray mSelectedContacts = new SparseBooleanArray(); 
private ArrayList<contact> items; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    contact_list = new ArrayList<contact>(); 
    this.mContactAdapter = new contactAdapter(this, R.layout.listview, 
      contact_list); 
    ListView lv = getListView(); 
    setListAdapter(this.mContactAdapter); 
    lv.setItemsCanFocus(false); 
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    // } 
    mViewcontacts = new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      getContacts(); 
     } 
    }; 

    Thread thread = new Thread(null, mViewcontacts, "ContactReadBackground"); 
    thread.start(); 
    mProgressDialog = ProgressDialog.show(Contactlist_selfActivity.this, 
      "Please Wait...", "Retriving Contacts...", true); 
} 

@SuppressWarnings("unused") 
private void getContacts() { 
    // TODO Auto-generated method stub 

    try { 

     String[] projection = new String[] { 
       ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.Contacts.HAS_PHONE_NUMBER, 
       ContactsContract.Contacts._ID }; 

     Cursor mCursor = managedQuery(
       ContactsContract.Contacts.CONTENT_URI, projection, 
       ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", 
       new String[] { "1" }, 
       ContactsContract.Contacts.DISPLAY_NAME); 

     while (mCursor.moveToNext()) { 
      contact contact = new contact(); 

      String contactId = mCursor.getString(mCursor 
        .getColumnIndex(ContactsContract.Contacts._ID)); 

      contact.setContactName(mCursor.getString(mCursor 
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 
      contact_list.add(contact); 
     } 
     mCursor.close(); 

     runOnUiThread(returnRes); 

    } catch (Exception e) { 
     // TODO: handle exception 
     Log.d("getContacts", e.getMessage()); 
    } 
} 

public class contactAdapter extends ArrayAdapter<contact> { 
    private int[] isChecked; 

    public contactAdapter(Context context, int textViewResourceId, 
      ArrayList<contact> items1) { 
     super(context, textViewResourceId, items1); 
     items = items1; 
    } 

    @Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final int position_clicked = 0; 
     // Log.i("asd", "getView :" + getItem(position)); 

     if (convertView == null) { 

      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.listview, parent, false); 
     } 

     contact contacts = items.get(position); 
     isChecked = new int[items.size()]; 

     if (contacts != null) { 
      final CheckBox nameCheckBox = (CheckBox) convertView 
        .findViewById(R.id.checkBox); 
      nameCheckBox.setChecked(mSelectedContacts.get(position)); 

      for (int i = 0; i < isChecked.length; i++) { 

      } 

      if (nameCheckBox != null) { 
       nameCheckBox.setText(contacts.getContactName()); 
      } 

      nameCheckBox.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        isChecked[position_clicked] = position; 
        Log.d("position", String.valueOf(position)); 
       } 
      }); 

     } 

     return convertView; 
    } 
} 

private Runnable returnRes = new Runnable() { 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     if (mProgressDialog.isShowing()) 
      mProgressDialog.dismiss(); 
     mContactAdapter.notifyDataSetChanged(); 
    } 
}; 
} 

回答

11

我找到了答案.... 我剛踏出接觸類一個新的變量...

public class PlanetsActivity extends Activity { 
private ListView mainListView; 
private Contact[] contact_read; 
private Cursor mCursor; 
private ArrayAdapter<Contact> listAdapter; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Find the ListView resource. 
    mainListView = (ListView) findViewById(R.id.mainListView); 

    mainListView 
      .setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View item, 
         int position, long id) { 
        Contact planet = listAdapter.getItem(position); 
        planet.toggleChecked(); 
        ContactViewHolder viewHolder = (ContactViewHolder) item 
          .getTag(); 
        viewHolder.getCheckBox().setChecked(planet.isChecked()); 
       } 
      }); 

    // Throw Query and fetch the contacts. 

    String[] projection = new String[] { Contacts.HAS_PHONE_NUMBER, 
      Contacts._ID, Contacts.DISPLAY_NAME }; 

    mCursor = managedQuery(Contacts.CONTENT_URI, projection, 
      Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" }, 
      Contacts.DISPLAY_NAME); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
     contact_read = new Contact[mCursor.getCount()]; 

     // Add Contacts to the Array 

     int j = 0; 
     do { 

      contact_read[j] = new Contact(mCursor.getString(mCursor 
        .getColumnIndex(Contacts.DISPLAY_NAME))); 
      j++; 
     } while (mCursor.moveToNext()); 

    } else { 
     System.out.println("Cursor is NULL"); 
    } 

    // Add Contact Class to the Arraylist 

    ArrayList<Contact> planetList = new ArrayList<Contact>(); 
    planetList.addAll(Arrays.asList(contact_read)); 

    // Set our custom array adapter as the ListView's adapter. 
    listAdapter = new ContactArrayAdapter(this, planetList); 
    mainListView.setAdapter(listAdapter); 
} 

/** Holds Contact data. */ 
@SuppressWarnings("unused") 
private static class Contact { 
    private String name = ""; 
    private boolean checked = false; 

    public Contact() { 
    } 

    public Contact(String name) { 
     this.name = name; 
    } 

    public Contact(String name, boolean checked) { 
     this.name = name; 
     this.checked = checked; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public boolean isChecked() { 
     return checked; 
    } 

    public void setChecked(boolean checked) { 
     this.checked = checked; 
    } 

    public String toString() { 
     return name; 
    } 

    public void toggleChecked() { 
     checked = !checked; 
    } 
} 

/** Holds child views for one row. */ 
@SuppressWarnings("unused") 
private static class ContactViewHolder { 
    private CheckBox checkBox; 
    private TextView textView; 

    public ContactViewHolder() { 
    } 

    public ContactViewHolder(TextView textView, CheckBox checkBox) { 
     this.checkBox = checkBox; 
     this.textView = textView; 
    } 

    public CheckBox getCheckBox() { 
     return checkBox; 
    } 

    public void setCheckBox(CheckBox checkBox) { 
     this.checkBox = checkBox; 
    } 

    public TextView getTextView() { 
     return textView; 
    } 

    public void setTextView(TextView textView) { 
     this.textView = textView; 
    } 
} 

/** Custom adapter for displaying an array of Contact objects. */ 
private static class ContactArrayAdapter extends ArrayAdapter<Contact> { 

    private LayoutInflater inflater; 

    public ContactArrayAdapter(Context context, List<Contact> planetList) { 
     super(context, R.layout.simplerow, R.id.rowTextView, planetList); 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Contact to display 
     Contact planet = (Contact) this.getItem(position); 
     System.out.println(String.valueOf(position)); 

     // The child views in each row. 
     CheckBox checkBox; 
     TextView textView; 

     // Create a new row view 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.simplerow, null); 

      // Find the child views. 
      textView = (TextView) convertView 
        .findViewById(R.id.rowTextView); 
      checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01); 

      // Optimization: Tag the row with it's child views, so we don't 
      // have to 
      // call findViewById() later when we reuse the row. 
      convertView.setTag(new ContactViewHolder(textView, checkBox)); 

      // If CheckBox is toggled, update the Contact it is tagged with. 
      checkBox.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        Contact contact = (Contact) cb.getTag(); 
        contact.setChecked(cb.isChecked()); 
       } 
      }); 
     } 
     // Reuse existing row view 
     else { 
      // Because we use a ViewHolder, we avoid having to call 
      // findViewById(). 
      ContactViewHolder viewHolder = (ContactViewHolder) convertView 
        .getTag(); 
      checkBox = viewHolder.getCheckBox(); 
      textView = viewHolder.getTextView(); 
     } 

     // Tag the CheckBox with the Contact it is displaying, so that we 
     // can 
     // access the Contact in onClick() when the CheckBox is toggled. 
     checkBox.setTag(planet); 

     // Display Contact data 
     checkBox.setChecked(planet.isChecked()); 
     textView.setText(planet.getName()); 

     return convertView; 
    } 

} 

public Object onRetainNonConfigurationInstance() { 
    return contact_read; 
} 

}

+0

我也面臨類似的問題,你可以在這裏發佈你的項目在一個zip文件。 – 2012-09-25 10:47:31

+0

@TapanDesai這是全班兄弟.... 你還想要什麼..? – Wolverine 2012-09-26 06:56:04

+0

我試過使用相同的代碼,但我在使用它時在eclipse中出現錯誤..請發佈項目,如果您有檔案 – 2012-09-27 07:18:20

2

這種情況的原因是,當你調用nameCheckBox.setChecked()代碼OnClickListener()醒來並運行它的代碼。在設置複選框之前,我只是在設置OnClickListener(null)時遇到了同樣的問題。

+0

如果我在onClick傳遞null,那麼它如何得到哪個項目被點擊。 &可以詳細說明更多,以確切地做.. ??? &thenx .. – Wolverine 2012-03-23 06:42:06

+0

在調用setChecked()之前,您應該在onClick()上傳遞null,然後您應該通過onClick()傳遞該方法; – redandblack 2012-03-23 08:13:32

+0

在您的代碼中,它可能如下所示:nameCheckBox.setOnClickListener(null); nameCheckBox.setChecked(mSelectedContacts.get(位置)); – redandblack 2012-03-23 08:15:15

2

nameCheckBox.setOnClickListener(新OnClickListener(){

  @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       isChecked[position_clicked] = position; 
       Log.d("position", String.valueOf(position)); 
      } 
     }); 

這個代碼在商店添加更多的一個ARRE列表中的所有位置以及添加或刪除兩個所述 快速加載,第二刪除。 和用於列出在位置的另一種方法

+0

但當我向下滾動或向上滾動,如果我已取消選擇接觸得到選擇.. 我有想法,你說,但因爲它正在選擇如何獲得其狀態作爲取消選擇..? – Wolverine 2012-03-23 06:43:40