gpt4 book ai didi

java - 将 JSON 数据从另一个类传递到我的 ImageAdapter 以提供我的图像路径

转载 作者:行者123 更新时间:2023-12-01 15:47:43 27 4
gpt4 key购买 nike

好吧,在周末取得了很多进展之后,我陷入了 gridView 和 ImageAdapter 的困境。到目前为止,我所做的就是我的主要 Activity 获取用户选择的标签,执行查询并返回 json_ecoded 数据。我通过 bundle 和 Intent 将 json 数据传递给我的 showThumb Activity 。此 Activity 应该将 ImageAdapter 附加到我的 GridView 。但我不知道如何将 json 数据传递到 ImageAdapter,然后使用 json 中的数据提供缩略图的路径。

如果我能得到任何建议来提供帮助,那就太棒了。

JSON 数据:

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}

ShowThumb 类

package com.flash_Images;

import android.os.Bundle;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class showThumb extends Activity{



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridlayout);

Bundle bundle = getIntent().getExtras();
String jsonData = bundle.getString("jsonData");

try {
JSONObject jsonObj = new JSONObject(jsonData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(showThumb.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}

ImageAdapter 类:

package com.flash_Images;

import java.util.ArrayList;

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

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

private JSONObject jsonObj;

public ImageAdapter(Context c) {
mContext = c;
}

public ImageAdapter(showThumb c) {
// TODO Auto-generated constructor stub
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}


JSONArray menuitemArr = popupObject.getJSONArray("thumb");


// references to our images
private Integer[] mThumbIds = {


//I need to loop through my json "thumb:" items below to add the image paths for the grid view

for (int i = 0; i < menuitemArr.length(); i++) {
// printing the values to the logcat
setImageDrawable(Drawable.createFromPath(myFooArray[position].thumb);
}

//This is the example data I need to replace
/** R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7*/
};
}

最佳答案

使用 ArrayAdapter而不是BaseAdapter让事情顺利进行。

创建一个类 menuitemArr这样它们就可以包含从 json 解析器返回的数据。

定义 ArrayAdapter像这样

public class ImageAdapter extends ArrayAdepter<YOUNEWCLASS>

创建一个构造函数,您可以在其中传递已解析数据的列表

看一下 ArrayAdapter 的示例here .

现在像这样使用适配器

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this,listofYourNewCalss));

希望这可以帮助你。

关于java - 将 JSON 数据从另一个类传递到我的 ImageAdapter 以提供我的图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com