Json数据未显示在列表视图中。请检查我的代码代码中缺少的内容。我在logcat中获取数据但未在列表视图中显示。请检查谢谢。
com.example.nkrajput01.myapplication;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.util.StringBuilderPrinter;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
ListView listView;
ProgressDialog progressDialog;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
contactList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list);
/* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("downloading content...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String url = "http://api.androidhive.info/contacts/";
try{
URL url_ = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url_.openConnection();
httpURLConnection.setRequestMethod("GET");
InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
int line=0;
while ((line=bufferedReader.read())!= -1)
{
sb.append((char) line);
}
Log.d("Data", sb.toString());
String jsonString = sb.toString();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
for (int i = 0; i <jsonArray.length() ; i++) {
JSONObject c = jsonArray.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");
JSONObject aa = c.getJSONObject("phone");
String mobile = aa.getString("mobile");
String home = aa.getString("home");
String office =aa.getString("office");
HashMap<String, String> contact = new HashMap<>();
contact.put(id, "id");
contact.put(name, "name");
contact.put(email, "email");
contact.put(mobile, "mobile");
contactList.add(contact);
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (progressDialog.isShowing())
progressDialog.dismiss();
Log.d("ContactList", contactList.toString());
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
listView.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您似乎混淆了联系人HashMap
的键和值:
contact.put(id, "id");
contact.put(name, "name");
contact.put(email, "email");
contact.put(mobile, "mobile");
第一个参数是键,第二个是值,所以代码应该是:
contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("mobile", mobile);