-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:38:19.213 に答える
0

thisはinstance of an objectメソッドを参照します。詳細については、 を参照してください。static methodsClassClass 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 に答える
0

周囲のコードを表示していないため、それは言えません。

通常、this実行中のメソッドが属するオブジェクトを参照します。実際のメソッド内でのみ使用できます。

于 2013-05-31T07:36:11.777 に答える
0

「this」は、メソッドが呼び出されるクラスの現在のインスタンスを指します。静的メソッドはクラス自体から呼び出されるため、そのコンテキストでは「this」を使用できません。

1 つの解決策:

これらのメソッドを静的にして、そのクラスのインスタンスを操作しないでください。

于 2013-05-31T07:49:18.287 に答える