Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何仅从hashmap java打印名称_Java_Android Studio - Fatal编程技术网

如何仅从hashmap java打印名称

如何仅从hashmap java打印名称,java,android-studio,Java,Android Studio,这是主活动,我正在将数据(联系人列表)传递给第二个活动 public class MainActivity extends AppCompatActivity { private String TAG = MainActivity.class.getSimpleName(); private ProgressDialog pDialog; private ListView lv; // URL to get contacts JSON private static String url

这是主活动,我正在将数据(联系人列表)传递给第二个活动

 public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "http://api.androidhive.info/contacts/";

ArrayList<HashMap<String, String>> contactList;

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

    contactList = new ArrayList<>();

    lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();


}


/**
 * Async task class to get json by making HTTP call
 */
public class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }



@Override
public Void doInBackground(Void... arg0) {
    HttpHandler sh = new HttpHandler();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall(url);

    Log.e(TAG, "Response from url: " + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            JSONArray contacts = jsonObj.getJSONArray("contacts");

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                String id = c.getString("id");
                String name = c.getString("name");
                String email = c.getString("email");
                String address = c.getString("address");
                String gender = c.getString("gender");

                // Phone node is JSON Object
                JSONObject phone = c.getJSONObject("phone");
                String mobile = phone.getString("mobile");
                String home = phone.getString("home");
                String office = phone.getString("office");


                // tmp hash map for single contact
                HashMap<String, String> contact = new HashMap<>();

                // adding each child node to HashMap key => value
                contact.put("id", id);
                contact.put("name", name);
                contact.put("email", email);
                contact.put("mobile", mobile);
                contact.put("address", address);

                // adding contact to contact list
                contactList.add(contact);
            }
        } catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Json parsing error: " + e.getMessage(),
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }
    } else {
        Log.e(TAG, "Couldn't get json from server.");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Couldn't get json from server. Check LogCat for possible errors!",
                        Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
    Intent in = new Intent(getApplicationContext(), MapsActivity.class);
    in.putExtra("array", contactList);
    startActivity(in);


    return null;
public类MainActivity扩展了AppCompatActivity{
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
私有ListView lv;
//获取联系人JSON的URL
专用静态字符串url=”http://api.androidhive.info/contacts/";
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList=新的ArrayList();
lv=(ListView)findViewById(R.id.list);
新建GetContacts().execute();
}
/**
*异步任务类通过HTTP调用获取json
*/
公共类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
公共Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“contacts”);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(“id”,id);
联系人。填写(“姓名”,姓名);
联系方式。放置(“电子邮件”,电子邮件);
联系人:put(“mobile”,mobile);
联系方式。填写(“地址”,地址);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
Intent in=新Intent(getApplicationContext(),MapsActivity.class);
in.putExtra(“数组”,联系人列表);
星触觉(in);
返回null;
}

我想只打印这个数组中的地址,但打印地址有问题,它正在打印整个数组列表

Intent intent = getIntent();


if(intent!=null){
            ArrayList tmp = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("array");


        System.out.println(Arrays.asList(tmp));

        System.out.println(tmp);


    }
Intent-Intent=getIntent();
if(intent!=null){
ArrayList tmp=(ArrayList)getIntent().getSerializableExtra(“数组”);
System.out.println(Arrays.asList(tmp));
系统输出打印LN(tmp);
}

可能重复的tim不适用于meCan你可以把你的HashMap放在这里看看它是什么样子吗?@nassertahani我已经上传了主要活动