a179583637
a179583637
采纳率35.1%
2016-06-17 11:53

怎么定义一个动态存储数据的数组

我从json中获取5条数据,想要存储到数组中以供使用,可是怎么不好用啊,是我哪里错了,大神帮忙看看
package textview.exam;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.apistore.sdk.ApiCallBack;
import com.baidu.apistore.sdk.ApiStoreSDK;
import com.baidu.apistore.sdk.network.Parameters;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FragmentMainchatnews extends Fragment {
private String[] title={};//标题
private String[] abs;//新闻简介
private String[] url;//新闻详情地址
private String[] datatime;//新闻发布时间
private String[] img_url;//新闻缩略图

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view=inflater.inflate(R.layout.main_chat_news, null);
    apiTest(view);
    return view;
}

private void apiTest(final View view) {

   Parameters para = new Parameters();

    //para.put("keyword", "娱乐");
    ApiStoreSDK.execute("http://apis.baidu.com/songshuxiansheng/news/news",
            ApiStoreSDK.GET,
            para,
            new ApiCallBack() {

                @Override
                public void onSuccess(int status, String responseString) {
                    Log.i("sdkdemo", "onSuccess");


                    try {
                        TextView textView=(TextView)view.findViewById(R.id.nianfdia);
                        JSONObject  dataJson=new JSONObject(responseString);
                        JSONArray data=dataJson.getJSONArray("retData");
                        //textView.setText(data.toString());
                        for (int i = 0; i < 5; i++) {
                            JSONObject info = data.getJSONObject(i);
                            title = new String[]{info.getString("title")};
                            //img_url = new String[]{info.getString("image_url")};
                            //abs = new String[]{info.getString("abstract")};
                        }
                        Log.i("nnnnnn+",title[0]);
                        //Log.i("nnnnnn+",title[1]);
                        //Log.i("nnnnnn+",title[2]);
                        //Log.i("nnnnnn+",title[3]);
                        //Log.i("nnnnnn+",title[4]);

/* // 创建一个List集合,List集合的元素是Map
List> listItems =new ArrayList>();
for (int i = 0; i < 5; i++) {
Map listItem = new HashMap();
listItem.put("title", title[i]);
listItem.put("image", img_url[i]);
listItem.put("abs", abs[i]);
listItems.add(listItem);
}*/
/* // 创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), listItems,R.layout.news_simple_item,
new String[]{"image", "title", "abs"},
new int[]{R.id.image, R.id.title, R.id.abs});
ListView list = (ListView) view.findViewById(R.id.chat_news);
// 为ListView设置Adapter
list.setAdapter(simpleAdapter);*/
} catch (JSONException e) {
Log.i("获取到数据:",responseString);
}

                }

                @Override
                public void onComplete() {
                    Log.i("sdkdemo", "onComplete");
                }

                @Override
                public void onError(int status, String responseString, Exception e) {
                    Log.i("sdkdemo", "onError, status: " + status);
                    Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
                }

            });

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 5年前

    调试下
    listItem.put("title", title[i]);
    这里title得到值了么

    点赞 评论 复制链接分享
  • lzj0327 lzj0327 5年前
     或者将之后的map和list移到前面来 listItem.put("title", info.getString("title"));
    
    点赞 评论 复制链接分享
  • lzj0327 lzj0327 5年前
     title[i] = info.getString("title")   这样试试
    
    点赞 评论 复制链接分享