画像とその説明を保持できる動的グリッドがあります。これらの詳細は、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();
}
}
これは私のcustomgridアダプターです
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;
}
}