如何将Json字符串传递给其他类-Android

如何将Json字符串传递给其他类-Android,android,json,class,listview,android-asynctask,Android,Json,Class,Listview,Android Asynctask,我有一个asynctask,它使用JSON方法收集注释。我希望将注释发送到扩展baseAdapter的新类,以便将它们放置在listView中,我该如何做?这是我目前的代码 class loadComments extends AsyncTask<JSONObject, String, JSONObject> { private ArrayAdapter<String> mAdapter = null; @Override protected v

我有一个asynctask,它使用JSON方法收集注释。我希望将注释发送到扩展baseAdapter的新类,以便将它们放置在listView中,我该如何做?这是我目前的代码

class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
    private ArrayAdapter<String> mAdapter = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();                   
    } 

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    } 

    protected JSONObject doInBackground(JSONObject... params) {
        JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
        return json2;  
    }

    @Override
    protected void onPostExecute(JSONObject json2) {
         try {  
             if (json2.getString(KEY_SUCCESS) != null) { 
                 registerErrorMsg.setText("");
                 String res2 = json2.getString(KEY_SUCCESS);
                 if(Integer.parseInt(res2) == 1) { 
                     l1=(ListView)findViewById(R.id.list);

                     JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
                     final String comments[] = new String[commentArray.length()];
                     for ( int i=0; i<commentArray.length(); i++ ) {
                          comments[i] = commentArray.getString(i);
                     }
                     JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
                     String numbers[] = new String[numberArray.length()];
                     for ( int i=0; i<numberArray.length(); i++ ) {
                         numbers[i] = numberArray.getString(i);
                     }
                     JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
                     String usernames[] = new String[usernameArray.length()];
                     for ( int i=0; i<usernameArray.length(); i++ ) {
                         usernames[i] = usernameArray.getString(i);
                     }
                 }//end if key is == 1
                 else{
                     // Error in registration
                     registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                 }//end else
             }//end if
         } //end try
         catch (JSONException e) { 
             e.printStackTrace();
         }//end catch   
     }
 }
 new loadComments().execute();
class loadComments扩展了异步任务{
private ArrayAdapter mAdapter=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
} 
@凌驾
受保护的void onProgressUpdate(字符串…值){
super.onProgressUpdate(值);
} 
受保护的JSONObject doInBackground(JSONObject…参数){
JSONObject json2=CollectComments.CollectComments(usernameforcomments,offsetNumber);
返回json2;
}
@凌驾
受保护的void onPostExecute(JSONObject json2){
试试{
if(json2.getString(KEY_SUCCESS)!=null){
registerErrorMsg.setText(“”);
String res2=json2.getString(KEY_SUCCESS);
如果(Integer.parseInt(res2)==1){
l1=(ListView)findViewById(R.id.list);
JSONArray commentArray=json2.getJSONArray(KEY_COMMENT);
最终字符串注释[]=新字符串[commentArray.length()];

对于(int i=0;i您是否尝试将注释[]、数字[]、用户名[]声明为活动类中的全局变量?通过这种方式,您可以从两个类中访问它

class MySimpleArrayAdapter extends ArrayAdapter<String> {

    private final Context context;

    public MySimpleArrayAdapter(Context context, String[] comments) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView commentView = (TextView) rowView.findViewById(R.id.listComment);
        TextView usernameView = (TextView) rowView.findViewById(R.id.listPostedBy);
        TextView numberView = (TextView) rowView.findViewById(R.id.listNumber);
        commentView.setText(comments[position]);

        return rowView;
    }
}