我在我的 android 应用程序中使用简单的列表适配器
ListAdapter adapter = new SimpleAdapter(this, jsonParseList , R.layout.list_view,
new String[] {"conv_title","content"},
new int[] { R.id.conv_title, R.id.content });
listView.setAdapter(adapter);
在上面的代码中this
它显示的地方this
不能在静态内容中使用。为什么?
public class Json extends Activity {
static HttpURLConnection urlConnection;
static ListView listView;
//ArrayList<String> jsonParseList = new ArrayList<String>();
Jsonparser jsonparser;
enter code here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json_main);
listView = (ListView) findViewById(android.R.id.list);
jsonparser = new Jsonparser();
jsonparser.execute("");
Log.e("List Activity", "" + jsonParseList);
}
public static HttpURLConnection openConnection() throws Exception {
int responseCode = -1;
try {
URL url = new URL(
" place ur url");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-encoded");
urlConnection
.setRequestProperty(
"Content-Length",
""
+ Integer
.toString("place ur url"
.getBytes().length));
urlConnection.setRequestProperty("Content-Language", "en-US");
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setReadTimeout(10 * 1000);
urlConnection.setConnectTimeout(10 * 1000);
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.writeBytes("place ur url");
wr.flush();
wr.close();
responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("Server not responding");
}
} catch (SocketException e) {
throw new Exception("Connection Time Out");
} catch (java.net.UnknownHostException unknownHostException) {
// TODO: handle exception
throw new Exception("unknownHostException");
} catch (Exception e) {
// TODO: handle exception
throw new Exception("Error Occured");
}
return urlConnection;
}
public static ArrayList<String> jsonParseList = new ArrayList<String>();
public static ArrayList<String> jsonParsing() {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
JSONArray array;
try {
try {
urlConnection = openConnection();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line = bufferedReader.readLine();
while (line != null) {
buffer.append(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
if (buffer.toString() != null) {
try {
array = new JSONArray(buffer.toString());
JSONObject jsonObject;
for (int i = 0; i < array.length(); i++) {
jsonObject = array.getJSONObject(i);
String conv_title = jsonObject.getString("conv_title");
String content = jsonObject.getJSONObject("first_post")
.getString("content");
String creator = jsonObject.getJSONObject("first_post")
.getJSONObject("creator").getString("name");
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
//map.put("id", String.valueOf(i));
map.put("conv_title", jsonObject.getString("conv_title"));
map.put("content", jsonObject.getString("content"));
map.put(conv_title, conv_title);
map.put(content, content);
Log.e("List Values", conv_title);
Log.e("List Values", content);
jsonParseList.add("conv_title "+"content");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, jsonParseList , R.layout.list_view,
new String[] {"conv_title","content"},
new int[] { R.id.conv_title, R.id.content });
listView.setAdapter(adapter);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonParseList;
}
@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;
}
private class Jsonparser extends AsyncTask<String, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(String... params) {
// TODO Auto-generated method stub
return jsonUtil.jsonParsing();
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result instanceof ArrayList<?>) {
jsonParseList = result;
}
}
}
}