将 json 响应数据加载到网格
Load json response data to the grid
我有一个可以保存图像及其描述的动态网格。这些详细信息在 responseJson 中。我想使用 responseJson 中的那些细节来填充网格。如何将 Description、ImageURL 传递给网格?任何帮助将不胜感激。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PizzaMenuAsyncTask(getActivity(), this).execute();
mQuickReturnView = (TextView) view.findViewById(R.id.footer);
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1);
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2);
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
@Override
public void onTaskCompleted(JSONArray responseJson) {
try
{
String[] Description = new String[responseJson.length()];
String[] ImageURL = new String[responseJson.length()];
for (int i = 0; i < responseJson.length(); i++)
{
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1"))
{
Log.i("MainCategoryID ", object.getString("ImageURL"));
ImageURL[i] = object.getString("ImageURL");
Log.i("MainCategoryID ", object.getString("Description"));
Description[i] = object.getString("Description");
CustomGrid adapter = new CustomGrid(getActivity(), Description, ImageURL);
grid.setAdapter(adapter);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
这是我的自定义网格采用者
public CustomGrid(Context c,String[] Description,String[] ImageURL ) {
mContext = c;
this.ImageURL = ImageURL;
this.Description = Description;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Description.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fragment_pizza, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(Description[position]);
imageView.setImageResource(ImageURL[position]); //error - The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)
} else {
grid = (View) convertView;
}
return grid;
}
}
试试这个:
@Override
public void onTaskCompleted(JSONArray responseJson) {
try
{
description = new String[responseJson.length()];
imageUrl = new String[responseJson.length()];
for (int n = 0; n < responseJson.length(); n++)
{
JSONObject object = responseJson.getJSONObject(n);
if ((object.getString("MainCategoryID")).equals("1"))
{
Log.i("MainCategoryID ", object.getString("ImageURL"));
imageUrl[i] = object.getString("ImageURL");
Log.i("MainCategoryID ", object.getString("Description"));
description[i] = object.getString("Description");
}
}
adapter = new CustomGrid(getActivity(), description, imageURL);
grid.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
}
我有一个可以保存图像及其描述的动态网格。这些详细信息在 responseJson 中。我想使用 responseJson 中的那些细节来填充网格。如何将 Description、ImageURL 传递给网格?任何帮助将不胜感激。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PizzaMenuAsyncTask(getActivity(), this).execute();
mQuickReturnView = (TextView) view.findViewById(R.id.footer);
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1);
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2);
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
@Override
public void onTaskCompleted(JSONArray responseJson) {
try
{
String[] Description = new String[responseJson.length()];
String[] ImageURL = new String[responseJson.length()];
for (int i = 0; i < responseJson.length(); i++)
{
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1"))
{
Log.i("MainCategoryID ", object.getString("ImageURL"));
ImageURL[i] = object.getString("ImageURL");
Log.i("MainCategoryID ", object.getString("Description"));
Description[i] = object.getString("Description");
CustomGrid adapter = new CustomGrid(getActivity(), Description, ImageURL);
grid.setAdapter(adapter);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
这是我的自定义网格采用者
public CustomGrid(Context c,String[] Description,String[] ImageURL ) {
mContext = c;
this.ImageURL = ImageURL;
this.Description = Description;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Description.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fragment_pizza, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(Description[position]);
imageView.setImageResource(ImageURL[position]); //error - The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)
} else {
grid = (View) convertView;
}
return grid;
}
}
试试这个:
@Override
public void onTaskCompleted(JSONArray responseJson) {
try
{
description = new String[responseJson.length()];
imageUrl = new String[responseJson.length()];
for (int n = 0; n < responseJson.length(); n++)
{
JSONObject object = responseJson.getJSONObject(n);
if ((object.getString("MainCategoryID")).equals("1"))
{
Log.i("MainCategoryID ", object.getString("ImageURL"));
imageUrl[i] = object.getString("ImageURL");
Log.i("MainCategoryID ", object.getString("Description"));
description[i] = object.getString("Description");
}
}
adapter = new CustomGrid(getActivity(), description, imageURL);
grid.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
}