联系人添加在ArrayList中的ListView中的错误联系人、错误、ArrayList、ListView

2023-09-04 23:04:49 作者:最爱的人不需要备注

喜在我的项目,我有来从phone.I联系人的电话号码和姓名可以拨打的电话号码和姓名,但是当我把它添加到的ArrayList< HashMap的<字符串,字符串>> 我得到的唯一的最后一个值,我不知道为什么。但是当我登录它我让所有的值可以u人帮我在哪里我做错什么了。

 公共类联系方式延伸活动{
    按钮btnGetContacts;
    INT Z = 1;
    字符串namereview = NULL;
    公共字符串phoneNumber1 =,phoneNumber的,名称1,网址,urljoingroup;
    ListView控件listSliding;
    按钮slideHandleButton;
    HashMap的<字符串,字符串> MAP1;

    ListContactAdaptor allContactAdapter;
    ArrayList的< HashMap的<字符串,字符串>> ArrayList的=新的ArrayList< HashMap的<字符串,字符串>>();
    HashMap的<字符串,字符串> resultp =新的HashMap<字符串,字符串>();
    字符串名称;
    字符串消息= NULL;
    字符串sGetContactUrl;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.getcontactlist_main);
        listSliding =(ListView控件)findViewById(R.id.GetContacts_Listview);
        customer_contact();

    }


    @燮pressLint(NewApi)
    公共无效customer_contact(){

        INT timescount = 1,Y = 1;
        MAP1 =新的HashMap<字符串,字符串>();
        光标手机= getContentResolver()查询(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,
                NULL,NULL);

        而(phones.moveToNext()){


                NAME =手机
                        .getString(电话
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.v(姓名,姓名);
                phoneNumber的=手机
                        .getString(电话
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// phoneNumber的= phoneNumber.replaceAll([^ \\Ð],);
                map1.put(名,phoneNumber的);
                map1.put(命令,名);

                Log.i(姓名,名+名称);
                Log.i(伯爵,伯爵+ Z);
                Log.i(pnone,电话号码+ phoneNumber的);

                arraylist.add(MAP1);
                Log.i(数组列表,数组列表+);

        }
        phones.close();
        allContactAdapter =新ListContactAdaptor(XmlActivity.this,
                数组列表);
        Log.v(适配器,allContactAdapter +);
        //绑定适配器到ListView
        listSliding.setAdapter(allContactAdapter);
    }
    类ListContactAdaptor扩展了BaseAdapter {

        //声明变量
        上下文语境;
        LayoutInflater充气;
        ArrayList的< HashMap的<字符串,字符串>>数据;

        //公共静态字符串CategoryHeading;

        公共ListContactAdaptor(上下文的背景下,
                ArrayList的< HashMap的<字符串,字符串>>数组列表){
            this.context =背景;
            数据=数组列表;
            Log.i(数据,数据+);

        }

        @覆盖
        公众诠释getCount将(){
            返回data.size();
        }

        @覆盖
        公共对象的getItem(INT位置){
            返回null;
        }

        @覆盖
        众长getItemId(INT位置){
            返回0;
        }

        公共查看getView(最终诠释的立场,观点convertView,
                ViewGroup中父){
            //声明变量
            TextView的名称,命令;
            按钮joinme;

            充气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            查看ItemView控件= inflater.inflate(R.layout.getcontactlist_view,
                    父母,假);
            //从结果的位置
            resultp =新的HashMap<字符串,字符串>();
            resultp = data.get(位置);
            NAME =(TextView中)itemView.findViewById(R.id.name);
            命令=(TextView中)itemView.findViewById(R.id.commamd);

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

            如果(resultp.get(命令)。等于()){
                command.setText(无集团);
            } 其他 {
                command.setText(resultp.get(命令));
            }
            Log.v(Get命令,resultp.get(命令));

            返回ItemView控件;
        }
    }

}
 

解决方案

将这个在while循环

  MAP1 =新的HashMap<字符串,字符串>();
 

喜欢

 而(phones.moveToNext())
{
    MAP1 =新的HashMap<字符串,字符串>();
    。
    。
}
 
微信 查看手机联系人时不显示此人却在新的朋友里显示添加并且显示此人的社交资料

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

}

解决方案

Put this in while loop

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

like

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