Android setListAdapter不显示内容
我试图通过使用Android setListAdapter不显示内容,android,Android,我试图通过使用setListAdapter解析JSON来实现自定义适配器。一切看起来都很好,但手机屏幕上什么也没有显示。 除了台词。请帮我做这个。JSON正在成功解析并存储在ArrayListjsonlist中 public class MainActivity extends ListActivity { final String TAG = "MAINActivity.java"; ListView listView; private static final Str
setListAdapter
解析JSON
来实现自定义适配器。一切看起来都很好,但手机屏幕上什么也没有显示。
除了台词。请帮我做这个。JSON
正在成功解析并存储在ArrayList
jsonlist
中
public class MainActivity extends ListActivity {
final String TAG = "MAINActivity.java";
ListView listView;
private static final String STATION = "stationName";
private static final String DOCKS = "availableDocks";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
private Context context;
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsynctaskParseJson(MainActivity.this).execute();
// listView=(ListView)findViewById();
}
public class AsynctaskParseJson extends AsyncTask<String, String, String> {
private ListActivity activity;
private ProgressDialog dialog;
public AsynctaskParseJson(ListActivity mainActivity) {
// TODO Auto-generated constructor stub
this.activity = mainActivity;
context = activity;
dialog=new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
this.dialog.setMessage("Progress start");
this.dialog.show();
super.onPreExecute();
}
private Context context;
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
/*ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String, String>>(
context, R.layout.list_item, jsonlist);*/
Log.d("JSON LIST", "Headline: " + jsonlist.toString());
Log.d("Elements", "Headline: " + context.toString());
Log.d("Elements", "Station: " + STATION);
ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[] { STATION, DOCKS}, new int[] { R.id.editText1, R.id.editText2 });
setListAdapter(adapter);
lv = getListView();
getListView().setTextFilterEnabled(true);
}
String strURL = "http://www.citibikenyc.com/stations/json";
JSONArray jArray = null;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONParser jParser = new JSONParser();
JSONObject jObject = jParser.getJSONFromURL(strURL);
//Log.e("NITIN", "Headline: " + jObject);
try {
jArray = jObject.getJSONArray("stationBeanList");
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
String headLine = c.getString(STATION);
String agency = c.getString(DOCKS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(headLine, STATION);
map.put(agency, DOCKS);
jsonlist.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
发布适配器的代码,以便在适配器上调用
notifyDataSetChanged()
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class JSONParser {
final String TAG = "JsonParser.java";
static InputStream is = null;
static JSONObject jObj = null;
static String jSon = "";
public JSONObject getJSONFromURL(String strURL) {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(strURL);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
//Log.e("Nitin", "Builder: " + builder.toString());
is.close();
jSon = builder.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(jSon);
} catch (JSONException e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
return jObj;
}
}