jeudi 3 décembre 2015

Selecting and display multiple items in ListView doesn't work correctly

I am using this code. this code should normaly displays only items i have selected. But everytime i try, it displays an other item that i havent selected!! and this unselected item is always the same !!! Thank you for your help

public class ChoisirContactActivity extends ListActivity implements View.OnClickListener {

// ArrayList
ArrayList<SelectUser> selectUsers;
SelectUser selectUser;
List<SelectUser> temp;
// Contact List
ListView listView;
// Cursor to load contacts list
Cursor phones, email;

// Pop up
ContentResolver resolver;
SearchView search;
SelectUserAdapter adapter;
private SelectUserManager usermanager;
String arrData[][];


private Button btnSend;
private Poste poste;
private String dest1, dest2, dest3, dest4, dest5, yuyu;
private CheckBox check;
private PosteManager postmanager;
private Intent poste2Intent;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choisir_contact);

    selectUsers = new ArrayList<SelectUser>(); //usermanager.getAllUsers();

    resolver = this.getContentResolver();
    // listView = (ListView) findViewById(R.id.list);

    usermanager = new SelectUserManager(this);


    phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    LoadContact loadContact = new LoadContact();
    loadContact.execute();


    Intent data = getIntent();
    poste = data.getParcelableExtra("yuyu");
    btnSend = (Button) findViewById(R.id.btSend);
    btnSend.setOnClickListener(this);

    postmanager = new PosteManager(this);
}


@Override
public void onClick(View v) {

   // int cntChoice = getListView().getCount();
    SparseBooleanArray sp=getListView().getCheckedItemPositions();

    String str="";
    for(int j = 0;j<sp.size();j++) //sp.size() cntChoice
    {

        if (sp.valueAt(j)) {
            str += arrData[sp.keyAt(j)][1].toString() + "\n";
        }

    }

    postmanager.openForWrite();
    long res = postmanager.createPoste(poste);
    Toast.makeText(getApplicationContext(), ""+str, Toast.LENGTH_SHORT).show();


    poste2Intent = new Intent(ChoisirContactActivity.this, MainActivity.class);

    startActivity(poste2Intent);
}

// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // Get Contact list from Phone

        if (phones != null) {
            Log.e("count", "" + phones.getCount());
            if (phones.getCount() == 0) {
                Toast.makeText(ChoisirContactActivity.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
            }

            while (phones.moveToNext()) {
                // Bitmap bit_thumb = null;
                String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String EmailAddr = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA2));
               /* String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
                try {
                    if (image_thumb != null) {
                        bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                    } else {
                        Log.e("No Image Thumb", "--------------");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } */

                SelectUser selectUser = new SelectUser();
                //selectUser.setThumb(bit_thumb);
                selectUser.setName(name);
                selectUser.setPhone(phoneNumber);
                selectUser.setEmail(EmailAddr);

                usermanager.openForWrite();
                long res = usermanager.createUser(selectUser);
                selectUsers.add(selectUser);


            }
        } else {
            Log.e("Cursor close 1", "----------------");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        final DbHelper myDb = new DbHelper(ChoisirContactActivity.this);

        arrData = myDb.SelectAllDataUser();

        // listView = (ListView)findViewById(R.id.listPost);
        listView = getListView();
        adapter = new SelectUserAdapter(ChoisirContactActivity.this, arrData);
        listView.setAdapter(adapter);

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setItemChecked(5, true);

        listView.setFastScrollEnabled(true);


        // Select item on listclick
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Log.e("search", "here---------------- listener");


                //SelectUser data = (SelectUser) adapterView.getItemAtPosition(i);

                SelectUser data = selectUsers.get(i);
                data = usermanager.getUser(data, selectUsers);
                yuyu = data.getName();
                Toast.makeText(getApplicationContext(), "Uri:" + yuyu, Toast.LENGTH_LONG).show();

                SelectUserAdapter adapter = (SelectUserAdapter) adapterView.getAdapter();
                adapter.markSelected(i);
            }
        });

        listView.setFastScrollEnabled(true);
    }
}

@Override
protected void onStop() {
    super.onStop();
    phones.close();
}

}




Aucun commentaire:

Enregistrer un commentaire