在 Arraylist 中添加联系人以解决 ListView 错误 [英] Contacts adding in Arraylist for ListView error

查看:18
本文介绍了在 Arraylist 中添加联系人以解决 ListView 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须从电话中获取联系人电话号码和姓名.我可以拨打电话号码和姓名,但是当我将其添加到 ArrayList> 时;我得到了唯一的最后一个值,我不知道为什么.但是当我登录它时,我得到了所有的价值,你们能帮我我哪里出错了

Hi in my project i have to fetch a contacts phone number and the name from the phone.I can get call the phone number and name but when i add it to the ArrayList<HashMap<String, String>>i'm getting the only the last value i don't know why. but when i log it i'm getting all the value can u guys help me where did i go wrong

 public class Contacts  extends Activity {
    Button btnGetContacts;
    int z = 1;
    String namereview = null;
    public String phoneNumber1 = "", phoneNumber, name1, url,urljoingroup;
    ListView listSliding;
    Button slideHandleButton;
    HashMap<String, String> map1;

    ListContactAdaptor allContactAdapter;
    ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> resultp = new HashMap<String, String>();
    String name;
    String message = null;
    String sGetContactUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getcontactlist_main);
        listSliding = (ListView) findViewById(R.id.GetContacts_Listview);
        customer_contact();

    }


    @SuppressLint("NewApi")
    public void customer_contact() {

        int timescount = 1, y = 1;
        map1 = new HashMap<String, String>();
        Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);

        while (phones.moveToNext()) {


                name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.v("Name", name);
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//              phoneNumber = phoneNumber.replaceAll("[^\\d.]", "");
                map1.put("NAME", phoneNumber);
                map1.put("COMMAND", name);

                Log.i("name", "name : " + name);
                Log.i("count", "count :" + z);
                Log.i("pnone", "phone no: " + phoneNumber);

                arraylist.add(map1);
                Log.i("arraylist", arraylist+"");

        }
        phones.close();
        allContactAdapter = new ListContactAdaptor(XmlActivity.this,
                arraylist);
        Log.v("ADAPTER", allContactAdapter + "");
        // Binds the Adapter to the ListView
        listSliding.setAdapter(allContactAdapter);
    }
    class ListContactAdaptor extends BaseAdapter {

        // Declare Variables
        Context context;
        LayoutInflater inflater;
        ArrayList<HashMap<String, String>> data;

        // public static String CategoryHeading;

        public ListContactAdaptor(Context context,
                ArrayList<HashMap<String, String>> arraylist) {
            this.context = context;
            data = arraylist;
            Log.i("data", data + "");

        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // Declare Variables
            TextView name, command;
            Button joinme;

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View itemView = inflater.inflate(R.layout.getcontactlist_view,
                    parent, false);
            // Get the position from the results
            resultp = new HashMap<String, String>();
            resultp = data.get(position);
            name = (TextView) itemView.findViewById(R.id.name);
            command = (TextView) itemView.findViewById(R.id.commamd);

            joinme= (Button) itemView.findViewById(R.id.getImage);
            name.setText(resultp.get("NAME"));
            Log.v("GET NAME", resultp.get("NAME"));

            if (resultp.get("COMMAND").equals("")) {
                command.setText("No Group");
            } else {
                command.setText(resultp.get("COMMAND"));
            }
            Log.v("GET COMMAND", resultp.get("COMMAND"));

            return itemView;
        }
    }

}

推荐答案

将其放入 while 循环

Put this in while loop

map1 = new HashMap<String, String>();

喜欢

while (phones.moveToNext())
{
    map1 = new HashMap<String, String>();
    .
    .
}

这篇关于在 Arraylist 中添加联系人以解决 ListView 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆