我从URL获取JSON数据,然后在MainActivity的ListView中显示它。 我已经与JSON文件URL建立了正确的连接,并且我能够将URL中的数据提取到字符串中,但现在我尝试使用ListAdapter在ListView中显示它,但它没有显示任何数据。
我的JSON数据如下:
{
"result": 1,
"data": [{
"id": 1,
"name": "Aconitum",
"url": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/7\/7e\/Aconitum_degenii.jpg"
},
{
"id": 2,
"name": "African lily",
"url": "http:\/\/i1.wp.com\/cottagegardenflowershop.co.uk\/wp-content\/uploads\/2014\/09\/1040668-90x90.jpg?ssl=1"
},
{
"id": 3,
"name": "Alpine thistle",
"url": "http:\/\/i1.wp.com\/cottagegardenflowershop.co.uk\/wp-content\/uploads\/2014\/09\/1040668-90x90.jpg?ssl=1"
},
我将数据以hashmap的形式存储在此格式的arrayList中。 的ArrayList> contactList;
我的主要活动.java是:
package com.example.hsports.flowers;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
public static String url="http://development.easystartup.org/NO/Backend/flower.php";
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
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.listview);
new GetContacts().execute();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url=contactList.get(position).get("url");
System.out.println("hello");
}
});
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected 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("data");
// looping through All Contacts
for (int i = 0; i < 28; i++) {
JSONObject c = contacts.getJSONObject(i);
if(c==null)
{
continue;
}
String id = c.getString("id");
String name = c.getString("name");
String urlInjson = c.getString("url");
//String address = c.getString("address");
//String gender = c.getString("gender");
// 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("url", urlInjson);
// 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();
}
});
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.support_simple_spinner_dropdown_item, new String[]{"name"}, new int[]{R.id.flowerName});
lv.setAdapter(adapter);
}
}
}
我怎样才能填充&#34; name&#34;来自ListView中的Arraylist?
答案 0 :(得分:0)
此声明应该有变化:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.support_simple_spinner_dropdown_item, new String[]{"name"}, new int[]{R.id.flowerName});
应该给出R.layout.list_view,其中这是定义ListView布局的视图。