当我执行程序时,它显示一个空白屏幕,在logcat中它显示没有连接适配器;跳过布局。当我调试代码时,ArrayList
没有得到值(返回0)。
public class MainActivity extends AppCompatActivity implements Constants, NetworkOperation, URL {
LinearLayoutManager manager;
ArrayList<OfferModal> bestoffers;
RecyclerViewAdapter adapter;
RecyclerView rv;
FetchData fetchData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.recyclerview1);
fetchData = new FetchData(this, this, CLOUD_SECTION);
fetchData.fromServer();
manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void started() {
}
@Override
public void doingBackground() {
}
@Override
public void completed(JSONObject jsonObject) {
bestoffers = new ArrayList<OfferModal>();
try {
JSONArray array_item = new JSONArray();
for (int i = 0; i < array_item.length(); i++) {
JSONObject itemobj = array_item.getJSONObject(i);
String id = itemobj.getString(ID);
String le_id = itemobj.getString(LE_ID);
String title = itemobj.getString(TITLE);
String description = itemobj.getString(DESCRIPTION);
String wid = itemobj.getString(WID);
String hgt = itemobj.getString(HGT);
String tn_path = itemobj.getString(TN_PATH);
String create_time = itemobj.getString(CREATE_TIME);
String update_time = itemobj.getString(UPDATE_TIME);
String view_count = itemobj.getString(VIEW_COUNT);
String slide_count = itemobj.getString(SLIDE_COUNT);
String shared = itemobj.getString(SHARED);
String publish_ver = itemobj.getString(PUBLISH_VER);
String publish_time = itemobj.getString(PUBLISH_TIME);
String user_name = itemobj.getString(USER_NAME);
String avatar_path = itemobj.getString(AVATAR_PATH);
String comment_count = itemobj.getString(COMMENT_COUNT);
String fav = itemobj.getString(FAV);
String fav_count = itemobj.getString(FAV_COUNT);
OfferModal off = new OfferModal(id, le_id, title, description, wid, hgt, tn_path, create_time, update_time, view_count, slide_count, shared, publish_ver, publish_time, user_name, avatar_path, comment_count, fav, fav_count);
off.setId(id);
off.setId(le_id);
off.setTitle(title);
off.setDescription(description);
off.setHgt(hgt);
off.setTn_path(tn_path);
off.setCreate_time(create_time);
off.setUpdate_time(update_time);
off.setView_count(view_count);
off.setSlide_count(slide_count);
off.setShared(shared);
off.setPublish_ver(publish_ver);
off.setPublish_time(publish_time);
off.setUser_name(user_name);
off.setAvatar_path(avatar_path);
off.setComment_count(comment_count);
off.setFav(fav);
off.setFav_count(fav_count);
bestoffers.add(off);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new RecyclerViewAdapter(getApplicationContext(), bestoffers);
rv.setAdapter(adapter);
}
}
这是我的json数据:
[
{
"id": "42057",
"le_id": "568fb0d29a5eb",
"title": "Bag the Big Deal Lulu Hypermarket",
"description": "",
"wid": "0",
"hgt": "0",
"tn_path": "",
"create_time": "1452257490",
"update_time": "1452332748",
"view_count": "00000000669",
"slide_count": "9",
"shared": "F",
"publish_ver": "11",
"publish_time": "1452332761",
"user_name": " Lulu Retail",
"avatar_path": null,
"comment_count": "0",
"fav": "F",
"fav_count": 0
},
{
"id": "42019",
"le_id": "568f9e45625da",
"title": "Bag the Big Deal Lulu Fashion Store",
"description": "",
"wid": "0",
"hgt": "0",
"tn_path": "",
"create_time": "1452252741",
"update_time": "1452584969",
"view_count": "00000000659",
"slide_count": "1",
"shared": "F",
"publish_ver": "5",
"publish_time": "1452584972",
"user_name": " Lulu Retail",
"avatar_path": null,
"comment_count": "0",
"fav": "F",
"fav_count": 0
},
{
"id": "42017",
"le_id": "568f9e30df0da",
"title": "Bag the Big Deal Lulu Connect",
"description": "",
"wid": "0",
"hgt": "0",
"tn_path": "",
"create_time": "1452252720",
"update_time": "1452585162",
"view_count": "00000000726",
"slide_count": "7",
"shared": "F",
"publish_ver": "7",
"publish_time": "1452275301",
"user_name": " Lulu Retail",
"avatar_path": null,
"comment_count": "0",
"fav": "F",
"fav_count": 0
}
]
这是我的适配器类:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
ArrayList<OfferModal> mBestoffers;
Context context;
// List<Offers>card_items;
RequestQueue mRequestQueue;
ImageLoader mImageLoader;
public RecyclerViewAdapter(Context context, ArrayList<OfferModal> mBestoffers) {
this.context = context;
// this.card_items = card_items;
this.mBestoffers = mBestoffers;
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout, null);
RecyclerViewHolder rvh = new RecyclerViewHolder(view);
return rvh;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
// holder.imageView.setImageUrl(mBestoffers.get(position).getAvatar_path(), mImageLoader);
holder.tv1.setText(mBestoffers.get(position).getTitle());
//write code to download image and data from server and set here
}
@Override
public int getItemCount() {
return mBestoffers.size();
}
}
请帮我解决这个问题,任何帮助都会非常贴切。
答案 0 :(得分:1)
请在完成()中粘贴jsonObject.toString()。
数组空的原因在这里。
JSONArray array_item = new JSONArray();
for (int i = 0; i < array_item.length(); i++) {
答案 1 :(得分:0)
请使用此代码,它会对您有所帮助。
公共类MainActivity扩展AppCompatActivity实现常量,NetworkOperation,URL {
LinearLayoutManager manager;
ArrayList<OfferModal> bestoffers;
RecyclerViewAdapter adapter;
RecyclerView rv;
FetchData fetchData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.recyclerview1);
fetchData = new FetchData(this, this, CLOUD_SECTION);
fetchData.fromServer();
manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void started() {
}
@Override
public void doingBackground() {
}
@Override
public void completed(JSONObject jsonObject) {
bestoffers = new ArrayList<OfferModal>();
try {
JSONArray array_item = new JSONArray();
for (int i = 0; i < array_item.length(); i++) {
JSONObject itemobj = array_item.getJSONObject(i);
String id = itemobj.getString(ID);
String le_id = itemobj.getString(LE_ID);
String title = itemobj.getString(TITLE);
String description = itemobj.getString(DESCRIPTION);
String wid = itemobj.getString(WID);
String hgt = itemobj.getString(HGT);
String tn_path = itemobj.getString(TN_PATH);
String create_time = itemobj.getString(CREATE_TIME);
String update_time = itemobj.getString(UPDATE_TIME);
String view_count = itemobj.getString(VIEW_COUNT);
String slide_count = itemobj.getString(SLIDE_COUNT);
String shared = itemobj.getString(SHARED);
String publish_ver = itemobj.getString(PUBLISH_VER);
String publish_time = itemobj.getString(PUBLISH_TIME);
String user_name = itemobj.getString(USER_NAME);
String avatar_path = itemobj.getString(AVATAR_PATH);
String comment_count = itemobj.getString(COMMENT_COUNT);
String fav = itemobj.getString(FAV);
String fav_count = itemobj.getString(FAV_COUNT);
OfferModal off = new OfferModal(id, le_id, title, description, wid, hgt, tn_path, create_time, update_time, view_count, slide_count, shared, publish_ver, publish_time, user_name, avatar_path, comment_count, fav, fav_count);
off.setId(id);
off.setId(le_id);
off.setTitle(title);
off.setDescription(description);
off.setHgt(hgt);
off.setTn_path(tn_path);
off.setCreate_time(create_time);
off.setUpdate_time(update_time);
off.setView_count(view_count);
off.setSlide_count(slide_count);
off.setShared(shared);
off.setPublish_ver(publish_ver);
off.setPublish_time(publish_time);
off.setUser_name(user_name);
off.setAvatar_path(avatar_path);
off.setComment_count(comment_count);
off.setFav(fav);
off.setFav_count(fav_count);
bestoffers.add(off);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new RecyclerViewAdapter(MainActivity.this,R.layout.cardlayout, bestoffers);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged()
}
}
答案 2 :(得分:0)
你的主要问题在于JSONArray,你正在初始化为一个新的JSONArray,它是空的,请检查jsonArray.length()中数组的大小;我确定它是0.这就是为什么你的arraylist也是空的。