我试图通过使用#include <iostream>
#include <vector>
#include <chrono>
void f()
{
std::vector<int*> vpi {};
int i {};
try{
for(;;){
int* pi = new int[10000];
vpi.push_back(pi);
// ++i;
// std::cout << "Allocated " << i*80 << " MB so far\n";
}
}
catch(std::bad_alloc){
std::cerr << "Memory exhausted\n";
}
}
int main() {
auto t0 = std::chrono::high_resolution_clock::now();
f();
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t0-t1).count() << " ms\n";
}
解析JSON
来实现自定义适配器。一切看起来都不错,但手机屏幕上没有显示任何内容。
除了线。请帮我解决一下这个。 setListAdapter
正在解析并存储在JSON
ArrayList
中。
jsonlist
activity_main.xml中
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;
}
}
list_item.xml
<?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>
JSON解析器类:
<?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>