我有像这样的json
{
"News": [
{
"data": {
"id": "963426",
"gallery": [
{
"image_path": "/images/metbuat/images_d/963426_0.jpg"
},
{
"image_path": "/images/metbuat/images_d/963426_1.jpg"
},
{
"image_path": "/images/metbuat/images_d/963426_2.jpg"
}
]
}
},
{
"data": {
"id": "959846",
"gallery": [
{
"image_path": "/images/metbuat/images_d/959846_0.jpg"
},
{
"image_path": "/images/metbuat/images_d/959846_1.jpg"
}
]
}
}
]
}
解析这个json并从中获取数据时我遇到了问题。它为每个新闻项添加了所有图库图像。但其他价值观,如'观看',' body'正在添加正确的
private ArrayList<FeedNewsItem> feedNewsItemArrayList;
JSONArray jsonArray = response.getJSONArray("News");
for (int i = 0; i < jsonArray.length() - 1; i++)
{
JSONObject newsItem = jsonArray.getJSONObject(i);
JSONObject data = newsItem.getJSONObject("data");
imageUrl = "https://metbuat.az" + data.getString("image_t");
titleText = data.getString("title");
subTitleText = data.getString("sub_title");
watched = data.getString("views_stats");
body = data.getString("details_full");
if (!data.getString("gallery").equals("false"))
{
JSONArray galleryJsonArray = data.getJSONArray("gallery");
// I think problem is here. This block adding all news images to every single news item
for (int j = 0; j < galleryJsonArray.length(); j++)
{
JSONObject imageJsonObject = galleryJsonArray.getJSONObject(j);
imageURL = "https://metbuat.az" + imageJsonObject.getString("image_path");
galleryImagesList.add(imageURL);
}
}
// adding every news details to list one by one
feedNewsItemArrayList.add(new FeedNewsItem(imageUrl, titleText, subTitleText, time,date, watched, body, addDateTimestamp, currentDate, galleryImagesList));
}
...
Intent newsIntent = new Intent(MainActivity.this, NewsActivity.class);
FeedNewsItem clickedItem = feedNewsItemArrayList.get(position);
newsIntent.putStringArrayListExtra(EXTRA_GALLERYLIST, clickedItem.getmGalleryArrayList());
因此,当打开特定新闻时一切正常,但所有新闻的图库图像都出现在单个新闻项目中
这是NewsActivity.java类
中图库图像的代码Intent intent = getIntent();
galleryImagesList = intent.getStringArrayListExtra(EXTRA_GALLERYLIST);
private void getGalleryImages()
{
for (int i = 0; i < galleryImagesList.size(); i++)
{
final ImageView imageView = new ImageView(NewsActivity.this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setLayoutParams(new LinearLayout.LayoutParams(250, 250));
imageView.requestLayout();
Picasso.get().load(galleryImagesList.get(i)).into(imageView);
horizontalScrollLayout.addView(imageView);
}
}
在这张图片中,我只需要将这些图像放在红色框中。相反,我在json中使用gallery属性获取所有新闻的所有图像
答案 0 :(得分:0)
试试这个 将以下依赖项添加到应用程序级别的gradle文件中。
#include <ppl.h>
#include <Windows.h>
#include <array>
#include <stdio.h>
int main()
{
combinable<DWORD64> count[10];
int c;
for (c = 0; c < 10; c++)
{
parallel_for(0, 52, [&](int a)
{
//printf("%d,", a);
for (int b = a + 1; b < 52; b++)
{
count[c].local()++;
}
});
printf("\n%llu\n", count[c].combine(plus<DWORD64>()));
}
getchar();
}
然后在下面制作一些pojo课程。
compile 'com.google.code.gson:gson:2.2.4'
}
public class GalleryItem{
@SerializedName("image_path")
private String imagePath;
public void setImagePath(String imagePath){
this.imagePath = imagePath;
}
public String getImagePath(){
return imagePath;
}
@Override
public String toString(){
return
"GalleryItem{" +
"image_path = '" + imagePath + '\'' +
"}";
}
}
public class NewsItem{
@SerializedName("data")
private Data data;
public void setData(Data data){
this.data = data;
}
public Data getData(){
return data;
}
@Override
public String toString(){
return
"NewsItem{" +
"data = '" + data + '\'' +
"}";
}
然后在活动之后如下调用。
public class Response{
@SerializedName("News")
private List<NewsItem> news;
public void setNews(List<NewsItem> news){
this.news = news;
}
public List<NewsItem> getNews(){
return news;
}
@Override
public String toString(){
return
"Response{" +
"news = '" + news + '\'' +
"}";
}
}