Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - custom adapter for multiple choice listview
    text
    copied!<p>i need to read the contact list in my android app and show it to the user with a multiple choice option. I've just seen the android example with the ListView.CHOICE_MODE_MULTIPLE but i need something different, i want a custm layout. I need to show bot the name and the number of the contact so i created my own adapter. First of all i made myown class to create a Contact:</p> <pre><code> //Contacts class public class Contatto { private String nome; //The name private String numero;//number public String getNome(){ //Get Name return nome; } public String getNumero(){ //Get number return numero; } public void setNome(String n){ //Set name this.nome = n; } public void setNumero(String n){ //Set number this.numero = n; } } </code></pre> <p>Then i use a cicle to get all the contatcs using a thread to notify the user that the app is working...</p> <pre><code>import java.util.ArrayList; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class RecuperaRubrica extends ListActivity{ private ProgressDialog m_ProgressDialog = null; private ArrayList&lt;Contatto&gt; arrayContatti = null; private ContattoAdapter m_adapter; private Runnable viewContatti; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recupera_rubrica); arrayContatti = new ArrayList&lt;Contatto&gt;(); this.m_adapter = new ContattoAdapter(this, R.layout.riga, arrayContatti); setListAdapter(this.m_adapter); viewContatti = new Runnable(){ @Override public void run() { getContatti(); } }; Thread thread = new Thread(null, viewContatti, "MagentoBackground"); thread.start(); m_ProgressDialog = ProgressDialog.show(RecuperaRubrica.this, "ATTENDI...", "Recupero contatti in corso ...", true); } private Runnable returnRes = new Runnable() { @Override public void run() { if(arrayContatti != null &amp;&amp; arrayContatti.size() &gt; 0){ m_adapter.notifyDataSetChanged(); for(int i=0;i&lt;arrayContatti.size();i++) m_adapter.add(arrayContatti.get(i)); } m_ProgressDialog.dismiss(); m_adapter.notifyDataSetChanged(); } }; private void getContatti(){ try{ arrayContatti = new ArrayList&lt;Contatto&gt;(); Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); //Cursore della rubrica while (cursor.moveToNext()) { //Entro nel ciclo se trovo almeno un contatto try{ //Recupero tutte le info dell'utente corrente String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); //Se è disponibile almeno un numero di telefono lo recupero if (Integer.parseInt(hasPhone) &gt; 0) { Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); while (phones.moveToNext()) { String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); phoneNumber = str_replace("-","",phoneNumber); phoneNumber = str_replace("+39","",phoneNumber); Contatto contatto = new Contatto(); contatto.setNome(name.trim()); contatto.setNumero(phoneNumber.trim()); if(!arrayContatti.contains(contatto)){ arrayContatti.add(contatto); } } phones.close(); //Chiudo la rubrica } }catch(Exception e){} } } catch (Exception e) { Log.e("BACKGROUND_PROC", e.getMessage()); } runOnUiThread(returnRes); } private class ContattoAdapter extends ArrayAdapter&lt;Contatto&gt; { private ArrayList&lt;Contatto&gt; items; public ContattoAdapter(Context context, int textViewResourceId, ArrayList&lt;Contatto&gt; items) { super(context, textViewResourceId, items); this.items = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.riga, null); } Contatto o = items.get(position); if (o != null) { TextView tt = (TextView) v.findViewById(R.id.NomeElenco); TextView bt = (TextView) v.findViewById(R.id.NumeroElenco); if (tt != null) { tt.setText(o.getNome()); } if(bt != null){ bt.setText(o.getNumero()); } } return v; } } public String str_replace (String search, String replace, String subject) { StringBuffer result = new StringBuffer (subject); int pos = 0; while (true) { pos = result.indexOf (search, pos); if (pos != -1) result.replace (pos, pos + search.length (), replace); else break; } return result.toString (); } } </code></pre> <p>this is the simple xml layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt;&lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>this work but when i run the app if i check the first result then scroll the page also the first result in the new page is checked and so on...can you help me to understand why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload