繁体   English   中英

如何从ArrayList中获取所有元素 <HashMap<String, String> &gt;安卓

[英]How to get all the elements out of ArrayList<HashMap<String, String>> android

我有一个ArrayList HashMap,其中包含联系人-名称和电话号码-。 在将其发送到SQL数据库进行保存之前,我想检查并确保这些项目在ArrayList HashMap中。 下面的所有代码都可以正常工作,但是我不知道如何打印ArrayList中的所有内容。 我尝试过的东西没有用,我也不知道如何使用Log ...我对此很陌生。

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;


public class Contacts extends ActionBarActivity {
private static final int PICK_CONTACT = 1;

private static ArrayList<HashMap<String, String>> getContacts = new ArrayList<HashMap<String, String>>();

private static ArrayList<HashMap<String, String>> data1 = new ArrayList<HashMap<String, String>>();

private static HashMap<String, String> contacts = new HashMap<String,String>();

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

}

public void btnAddContacts_Click(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
}

public void btnDone_Click(View view){
    Intent i = new Intent(Contacts.this, Message.class);
    startActivity(i);
}



@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String id =
                            c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String hasPhone =
                            c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                                null, null);
                        phones.moveToFirst();
                        String phn_no = phones.getString(phones.getColumnIndex("data1"));
                        String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME));
                        contacts.put(name, phn_no);


                        while (c.moveToNext()) {
                            String id1 = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

                            String name1 = contacts.get(id1);
                            String phone = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));

                            HashMap<String, String> h = new HashMap<String, String>();
                            h.put("name", name1);
                            h.put("phone", phone);
                            data1.add(h);
                        }



                        Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();

                    }
                }
            }
    }

}


}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"     tools:context="akh.seniorproj.Contacts">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact"
    android:id="@+id/contact1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="82dp"
    android:clickable="true"
    android:onClick="btnAddContacts_Click" />


<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact"
    android:id="@+id/contact2"
    android:layout_below="@+id/contact1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="40dp"
    android:clickable="true"
    android:onClick="btnAddContacts_Click" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact"
    android:id="@+id/contact3"
    android:layout_below="@+id/contact2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="49dp"
    android:clickable="true"
    android:onClick="btnAddContacts_Click" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact"
    android:id="@+id/contact4"
    android:layout_below="@+id/contact3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="52dp"
    android:clickable="true"
    android:onClick="btnAddContacts_Click" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/Next1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:clickable="true"
    android:onClick="btnDone_Click" />


</RelativeLayout>
for (HashMap<String, String> contact : data1) {
    for (Entry<String, String> item : contact.entrySet()) {
        String key = item.getKey();
        String value = item.getValue();
        ... do whatever you want to do to print things
    }
}

示例程序可以实现您想要的。

public class mainClass {
 public static void main(String[] args){
 HashMap<String , String> map1 = new HashMap();
  map1.put("ONE", "1");
  map1.put("TWO", "2");
  map1.put("THREE", "3");
  map1.put("FOUR", "4");
  map1.put("FIVE", "5");

  HashMap<String , String> map2 = new HashMap();
  map2.put("ONE", "1");
  map2.put("TWO", "2");
  map2.put("THREE", "3");
  map2.put("FOUR", "4");
  map2.put("FIVE", "5");


 ArrayList<HashMap<String, String>> getContacts = new ArrayList<HashMap<String, String>>();
 getContacts.add(map1);
 getContacts.add(map2);

 for(HashMap<String,String> map : getContacts ){
     // get your hashmap keys 
     Set <String>setOfKeys = map.keySet();

     Iterator<String> iterator = setOfKeys.iterator();

     while (iterator.hasNext()) {
         /**
          * next() method returns the next key from Iterator instance.
          * return type of next() method is Object so we need to do DownCasting to String
          */
         String key = (String) iterator.next();

         /**
          * once we know the 'key', we can get the value from the HashMap
          * by calling get() method
          */
          String value = (String)map.get(key);

         System.out.println("Key: "+ key+", Value: "+ value);
         // store the key + value to whatever you want here 
          }
 }


   }
  }

您可以使用data1.toString()打印data1所有值

尝试使用此代码打印列表中的值在日志中检查它

for(int i=0;i< data1.size();i++){


    HashMap<String, String> h= data1.get(i);

       for (TypeKey name: h.keySet()){

            String key =name.toString();
            String value = h.get(name).toString();  
            System.out.println(key + " " + value);  


    } 
}

在将数据传递到数据库之前,请包含此代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM