带列表视图的片段和复选框

时间:2015-06-28 22:38:04

标签: android android-fragments android-listview fragment

我在Fragment中有一个包含listview和复选框的大问题,当我选中一个复选框并向下滚动时我注意到还有另一个复选框也被检查了,我现在与view和getView()方法有关我已经尝试了所有可能的解决方案,但没有解决问题,方法getView不能覆盖,因为它不是一个活动它是一个片段。

请帮我解决这个问题我真的厌倦了这个问题,请

public class Contacts extends Fragment implements OnCheckedChangeListener{

ArrayList<Boolean> positionArray;
ListView list;
CheckBox chbx;
private int count;
boolean checked[];

public Contacts() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Defines the xml file for the fragment

    View view = inflater.inflate(R.layout.activity_contacts, container, false);

    list = (ListView) view.findViewById(R.id.list);
    chbx = (CheckBox) view.findViewById(R.id.checkBox);


    LoadContactsAyscn lca = new LoadContactsAyscn();
    lca.execute();

    return view;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

}

这是获取手机联系人并将其放入列表视图的LoadContactsAyscn类

class LoadContactsAyscn extends AsyncTask<Void, Void, ArrayList<String>> {
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        pd = ProgressDialog.show(getActivity(), "Loading Contacts",
                "Please Wait");
    }

    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        // TODO Auto-generated method stub
        ArrayList<String> contacts = new ArrayList<String>();

        Cursor c = getActivity().getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);
        while (c.moveToNext()) {

            String contactName = c
                    .getString(c
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phNumber = c
                    .getString(c
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contacts.add(contactName + ":" + phNumber);

        }
        c.close();

        return contacts;

    }

    @Override
    protected void onPostExecute(ArrayList<String> contacts) {
        // TODO Auto-generated method stub
        super.onPostExecute(contacts);

        pd.cancel();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity().getApplicationContext(), R.layout.contact_text,R.id.Name, contacts);

        list.setAdapter(adapter);

    }

}

1 个答案:

答案 0 :(得分:0)

请发布您的onCheckedChanged方法中发生的事情。 我建议错误将在那里。