如何将Json String传递给另一个类--Android

时间:2013-07-11 21:42:07

标签: android json class listview android-asynctask

我有一个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();

这是我需要字符串,注释,用户名和编号的类。 Number是一个字符串而不是整数!

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;
    }
} 

1 个答案:

答案 0 :(得分:0)

您是否尝试将注释[],数字[],用户名[]声明为活动类中的全局变量?这样您就可以从两个类中访问它