靜態內容,我用簡單的列表適配器在上面的代碼中的this
的地方就說明this
不能在靜態內容使用。爲什麼?不能在我的Android應用程序</p> <pre><code>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); </code></pre> <p>使用「本」在機器人
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;
}
}
}
}
大概你是從靜態方法做到這一點的。你期望什麼*在這種情況下'this'的價值?目前還不清楚你對「靜態」的含義有多瞭解。 –
下一次請向我們展示方法簽名,因此給其他人回答您的問題 – fGo