-8

我在我的 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;

            }
        }
    }
}
4

8 回答 8

2

而不是使用

ListAdapter adapter = new SimpleAdapter(this, jsonParseList , R.layout.list_view, new String[] {"conv_title","content"}, new int[] { R.id.conv_title, R.id.content });

使用喜欢

ListAdapter adapter = new SimpleAdapter(ClassName.this, jsonParseList , R.layout.list_view, new String[] {"conv_title","content"}, new int[] { R.id.conv_title, R.id.content });
于 2013-05-31T07:36:38.137 回答
2

您不能this在静态方法或字段中使用,如this引用当前实例。

于 2013-05-31T07:40:03.307 回答
2

this关键字是指类的当前对象。所以它与一个对象有关。但是静态方法与类有关。所以你不能this在静态上下文中使用

于 2013-05-31T07:38:19.213 回答
0

“this”指的是调用该方法的类的当前实例。从类本身调用静态方法,因此不能在该上下文中使用“this”。

一种解决方案:

不要将这些方法设为静态并使用该类的实例。

于 2013-05-31T07:49:18.287 回答
0

这是不可能的,因为你没有显示周围的代码。

通常,this指执行方法所属的对象;您只能在实际方法中使用它。

于 2013-05-31T07:36:11.777 回答
0

静态方法不会有任何类的实例。this是当前类的对象。所以 this不能在静态方法中使用。

new SimpleAdapter(getApplication(), jsonParseList , R.layout.list_view, 
                    new String[] {"conv_title","content"}, 
                    new int[] { R.id.conv_title, R.id.content });

尝试使用这种方式。

我希望我解释得清楚一点。

于 2013-05-31T07:40:43.250 回答
0

this 指的是一个instance of an objectwhilestatic methods方法Class,更多细节参见Class Members.

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

于 2013-05-31T07:41:50.597 回答
0

你不能那样使用它。您的解决方案是:

  1. 创建一个静态字段并启动 onCreate。
  2. 稍后在您的静态内容中使用它。

参考这个:

  static Context thisContext;

  onCreate(.....){
      thisContext = this;
  }
于 2013-05-31T07:41:53.107 回答