我的目标
所以我想在我的Android应用程序中解析图片库的图片。我们说我有 5(或更多)图片库,每个图片库中都有可变数量的图片
我的GalleryOverview给了我ListView,让我们说
单击ListView中的一个项目,可以在PictureDetail Activity中为我提供整个图库。单击Pic本身,它会打开SinglePicture Activity以全分辨率为我提供Pic
当前状态
因此,活动树不是问题所在,我能够解析并加载1个图库,其中包含静态数量的图片。 我失败的地方:JSON-Parsing(我自己选择哪个结构)并插入本地SQLTable
OVERVIEW.CLASS
public class Overview extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_recyclerview_allimages);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
Overview.ImageGalleryAdapter adapter = new Overview.ImageGalleryAdapter(getApplicationContext(),SinglePicture.getSpacePhotos());
recyclerView.setAdapter(adapter);
}
private class ImageGalleryAdapter extends RecyclerView.Adapter<ImageGalleryAdapter.MyViewHolder> {
@Override
public ImageGalleryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the layout
View photoView = inflater.inflate(R.layout.gallery_costume_item_layout, parent, false);
ImageGalleryAdapter.MyViewHolder viewHolder = new ImageGalleryAdapter.MyViewHolder(photoView);
return viewHolder;
}
@Override
public void onBindViewHolder(ImageGalleryAdapter.MyViewHolder holder, int position) {
SinglePicture spacePhoto = mSpacePhotos[position];
ImageView imageView = holder.mPhotoImageView;
Glide.with(mContext)
.load(spacePhoto.getUrl())
.into(imageView);
}
@Override
public int getItemCount() {
return (mSpacePhotos.length);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView mPhotoImageView;
public MyViewHolder(View itemView) {
super(itemView);
mPhotoImageView = (ImageView) itemView.findViewById(R.id.iv_photo);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION) {
SinglePicture spacePhoto = mSpacePhotos[position];
Intent intent = new Intent(mContext, PictureDetail.class);
intent.putExtra(PictureDetail.EXTRA_SPACE_PHOTO, spacePhoto);
startActivity(intent);
}
}
}
private SinglePicture[] mSpacePhotos;
private Context mContext;
public ImageGalleryAdapter(Context context, SinglePicture[] spacePhotos) {
mContext = context;
mSpacePhotos = spacePhotos;
}
}
}
图片细节
public class PictureDetail extends AppCompatActivity {
public static final String EXTRA_SPACE_PHOTO = "PictureDetail.SPACE_PHOTO";
private ImageView mImageView;
@SuppressLint("CheckResult")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_detail);
mImageView = (ImageView) findViewById(R.id.image);
SinglePicture spacePhoto = getIntent().getParcelableExtra(EXTRA_SPACE_PHOTO);
Glide.with(this)
.asBitmap()
.load(spacePhoto.getUrl())
//.listener(new RequestListener<String, Bitmap>()
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
public void onPalette(Palette palette) {
if (null != palette) {
ViewGroup parent = (ViewGroup) mImageView.getParent().getParent();
parent.setBackgroundColor(palette.getDarkVibrantColor(Color.GRAY));
}
}
})
.into(mImageView);
}
}
参与JSON
public class JSON_GallReader extends AppCompatActivity {
SQLiteDatabase sqLiteDatabase;
String HttpJSonURL = config.JSON_GL;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json_reader);
SQLiteDataBaseBuild();
SQLiteTableBuild();
DeletePreviousData();
new StoreJSonDataInToSQLiteClass(JSON_GallReader.this).execute();
}
private class StoreJSonDataInToSQLiteClass extends AsyncTask<Void, Void, Void> {
public Context context;
String FinalJSonResult;
public StoreJSonDataInToSQLiteClass(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JSON_GallReader.this);
progressDialog.setTitle("LOADING");
progressDialog.setMessage("Please Wait");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpServiceClass httpServiceClass = new HttpServiceClass(HttpJSonURL);
try {
httpServiceClass.ExecutePostRequest();
if (httpServiceClass.getResponseCode() == 200) {
FinalJSonResult = httpServiceClass.getResponse();
if (FinalJSonResult != null) {
JSONArray jsonArray = null;
try {
JSONObject jsonObj = new JSONObject(FinalJSonResult);
// Getting JSON Array node
JSONArray gallery = jsonObj.getJSONArray("gallery");
// looping through the Gallery
for (int i = 0; i < gallery.length(); i++) {
JSONObject c = gallery.getJSONObject(i);
final int id = i;
String jr_id = c.getString("id");
String jr_descr = c.getString("cover");
String jr_name = c.getString("title");
String jr_date = c.getString("descr");
**这是问题**
String SQLiteDataBaseQuery = "INSERT INTO overall_table (id,des,name,date,imgurl,fburl,fbid) VALUES('"+id+"', '"+jr_descr+"',** HERE IS THE PROBLEM** );";
sqLiteDatabase.execSQL(SQLiteDataBaseQuery);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
sqLiteDatabase.close();
progressDialog.dismiss();
Toast.makeText(JSON_Reader.this,"Load Done", Toast.LENGTH_LONG).show();
}
}
public void SQLiteDataBaseBuild(){
sqLiteDatabase = openOrCreateDatabase(SQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);
}
public void SQLiteTableBuild(){
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "+SQLiteHelper.TAB_NAME+"("+SQLiteHelper.table_id+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ SQLiteHelper.table_desc+" VARCHAR, " and so on..
}
public void DeletePreviousData(){
sqLiteDatabase.execSQL("DELETE FROM "+ TAB_NAME+"");
}
}
当前的JSON(也可以自由编辑这个)
{
"gallery": [
{
"cover": "https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"title": "Test Gallery 1",
"desc": "Here you go number one",
"pics": [
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png"
]
},
{
"cover": "https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"title": "Here you go number 2",
"desc": "Hier steht die Beschreibung zur Gallery",
"pics": [
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png"
]
},
{
"cover": "https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"title": "Here you go number three",
"desc": "Hier steht die Beschreibung zur Gallery",
"pics": [
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png",
"https://storage.googleapis.com/creator-academy-assets/thumbnails/thumbnails-z1-1.png"
]
}
]
}
问题
正如你所看到的,在我的JSON_GallReader中,当我循环浏览我的JSON长度时,我并没有真正得到如何通过一个数组(图库)进行循环,该数组成为了#34; pics&#34;阵列 - 基本上我能够 JSONArray pics = jsonObj.getJSONArray(&#34; pics&#34;); 和 for(int j = 0; j&lt; pics.length(); j ++){ ...
BUT
有没有办法将我的所有链接放在1个表中,还是我必须为所有链接创建一个单独的表?
我的第二个想法如下:也许我应该得到两个JSON,一个用于解析图像库,另一个只包含IMG链接,当点击ListView项目时,它将动态解析。会更好吗? 我使用GLIDE btw。
答案 0 :(得分:0)
您可以通过以下代码将所有pics数组存储到单个列表中
List<String> pics_list =new ArrayList();
JsonObject response=new JsonObject(s);
JsonArray gallery =response.getJsonArray("gallery");
if(gallery!=null && gallery.length()>0){
for(int i=0;i<gallery.length();i++){
JsonObject json=gallery.getJsonObject(i);
JsonArray pics =json.getJsonArray("pics");
String [] arr=new String[pics.length];
for(int j=0; j<pics.length();j++){
arr[j]=pics.optstring(i);
}
for(int k=0 ;k<arr.length();k++){
pics_list.add(arr[k]);
}
}
}
之后,您可以使用sqlite中的update或create命令将此列表保存到数据库中