Android - 多选 ListView 的自定义适配器

标签 android listview android-listview custom-adapter

我需要读取我的 Android 应用程序中的联系人列表,并通过多选选项将其显示给用户。 我刚刚看过带有 ListView.CHOICE_MODE_MULTIPLE 的 android 示例,但我需要一些不同的东西,我想要一个自定义布局。 我需要向机器人显示联系人的姓名和号码,因此我创建了自己的适配器。 首先,我制作了自己的类(class)来创建联系人:

 //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;
    }

}

然后我使用一个 cicle 来获取所有的联系人,使用一个线程来通知用户该应用程序正在运行......

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<Contatto> 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<Contatto>();
    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 && arrayContatti.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<arrayContatti.size();i++)
            m_adapter.add(arrayContatti.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};
private void getContatti(){
      try{
          arrayContatti = new ArrayList<Contatto>();
          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) > 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<Contatto> {

    private ArrayList<Contatto> items;

    public ContattoAdapter(Context context, int textViewResourceId, ArrayList<Contatto> 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 ();
}
}

这是简单的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <ListView 
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      ></ListView>  

</LinearLayout>

这是有效的,但是当我运行应用程序时,如果我检查第一个结果然后滚动页面,也会检查新页面中的第一个结果等等......你能帮我理解为什么吗?

最佳答案

我自己遇到过这样的问题,下面是我发现它的原因。

由于 ListView 使用 View 对象回收来节省内存,我们需要有某种机制来存储每个列表项的选择状态,然后在获取 View 方法中使用它来检查或取消选中(imp)选择正在检索 View 。

希望对你有帮助

关于Android - 多选 ListView 的自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8464850/

相关文章:

android - GridView 的 CollapsingToolbarLayout 问题

android - 将 row_id 从一个 Activity 传递到另一个 Activity ,这样我就可以根据 row_id 从 Activity 中删除行

android - 无法填充 SQLite 数据库

c# - WPF 和 ListView - 在运行时添加列和项目

android - 使用 IntentService 和 ActivityRecognition 更新 UI

listview - 项目仅在 ListView 中显示部分文本

android - 具有 visibility=gone 元素的膨胀布局的性能影响

android - 从 fragment 类主 Activity 调用自定义 ListView 适配器

android - 如何向我的 XMPP 帐户添加和订阅 jabber 条目?

android - "Could not find method packageName()"RC1 错误 - flavor 不编译