动态加载数据到 Gridview
Dynamicaly loading data to Gridview
当我在 gridview 上工作时,我遇到了以下问题,我们将不胜感激,
当我将数据加载到我的 gridview 时,它只加载数组的前 3 项,但还有 18 项要加载。为什么它不加载其他 15 个项目。 (Log.i 它显示了我 LogCat 中的所有 18 个项目)。
由于MainCategoryID=1的item只有18个,应该只加载18个grid,但是这里加载了28个grid(因为responseJson里面有28个item)。我想将它限制为 18 个网格。 (加载的28个格子里面,加载了3个item,有几处重复,其他格子都是空格子)
PizzaFragment class
public class PizzaFragment extends ListFragment implements OnTaskCompleted {
private TranslateAnimation anim;
GridView grid;
@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();
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 class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] Description;
private final String[] ImageURL;
public CustomGrid(Context c, String[] Description, String[] ImageURL) {
mContext = c;
this.ImageURL = ImageURL;
this.Description = Description;
}
@Override
public int getCount() {
return Description.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
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]);
Picasso.with(mContext).load(ImageURL[position]).into(imageView);
} else {
grid = (View) convertView;
}
return grid;
}
}
这是我的屏幕的样子(这不是真实图像,但它是这样的)
首先,更改适配器以扩展
extends ArrayAdapter<String>
更改构造函数:
public自定义网格(上下文c){
超级(c,R.layout.fragment_pizza);
}
现在更改 getView()
方法:
// Get the item
String item = getItem(position);
将此项目文本设置为您的视图,如果您需要更多数据集,请创建模型 class 并更改
extends ArrayAdapter<String>
与 extends ArrayAdapter<ModelClass>
// Get the item
ModelClass item = getItem(position);
现在在您的 Activity class 中,初始化 ModelClass,设置它的变量并使用 add 或 addAll 方法添加到适配器。
一次检查循环onTaskCompleted
方法。您正在创建适配器的实例并在循环内设置为 gridview 我认为在观察和检查您的代码后我发现了第一个问题。
其次,您处理填充行的适配器视图膨胀方法也是错误的,我可以说这不是正确的方法...
因此,请检查适配器和 onTaskCompleted
方法的以下代码。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
String[] Description = new String[responseJson.length()];
String[] ImageURL = new String[responseJson.length()];
JSONArray jArrayCategoryWise = new JSONArray();
for (int i = 0; i < responseJson.length(); i++) {
JSONObject object = responseJson.getJSONObject(i);
if ("1".equalsIgnoreCase(object.getString("MainCategoryID"))) {
// add to another jsonarray if MainCategoryID == 1
jArrayCategoryWise.put(object);
}
}
/******
* You can also create class with getter setter method for the
* Description and ImageUrl.Make the arraylist of the class after
* parsing jsonArray.
*/
// which will required on more iteration of your json array response
CustomGrid adapter = new CustomGrid(getActivity(),
jArrayCategoryWise);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
一旦检查数据是否正常,或者not.or你是否在两者之间添加任何东西
适配器
public class CustomGrid extends BaseAdapter {
private Context context;
private Context mContext;
// private final String[] Description;
// private final String[] ImageURL;
private JSONArray json;
private int size = 0;
public CustomGrid(Context c, JSONArray json) {
this.context = c;
this.json = json;
if (this.json != null)
size = this.json.length();
// You can also create class with getter setter method (getter setter
// for Descriptions and ImageUrl). and pass arraylist of that class
// this.Description = Description;
// this.ImageURL = ImageURL;
}
@Override
public int getCount() {
return size;
}
@Override
public Object getItem(int position) {
return this.json.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.custom_trips_frag_row, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
JSONObject jObjectRowData = this.json.getJSONObject(position);
holder.tvHeader.setText(jObjectRowData.getString("Description"));
Picasso.with(this.context).load(jObjectRowData.getString("ImageURL"))
.into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
请检查答案..希望我能正确回答你的问题并且你能得到你的答案... * 建议 * 一旦检查 ViewHolder class BaseAdapter 的模式。
当我在 gridview 上工作时,我遇到了以下问题,我们将不胜感激,
当我将数据加载到我的 gridview 时,它只加载数组的前 3 项,但还有 18 项要加载。为什么它不加载其他 15 个项目。 (Log.i 它显示了我 LogCat 中的所有 18 个项目)。
由于MainCategoryID=1的item只有18个,应该只加载18个grid,但是这里加载了28个grid(因为responseJson里面有28个item)。我想将它限制为 18 个网格。 (加载的28个格子里面,加载了3个item,有几处重复,其他格子都是空格子)
PizzaFragment class
public class PizzaFragment extends ListFragment implements OnTaskCompleted {
private TranslateAnimation anim;
GridView grid;
@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();
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 class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] Description;
private final String[] ImageURL;
public CustomGrid(Context c, String[] Description, String[] ImageURL) {
mContext = c;
this.ImageURL = ImageURL;
this.Description = Description;
}
@Override
public int getCount() {
return Description.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
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]);
Picasso.with(mContext).load(ImageURL[position]).into(imageView);
} else {
grid = (View) convertView;
}
return grid;
}
}
这是我的屏幕的样子(这不是真实图像,但它是这样的)
首先,更改适配器以扩展
extends ArrayAdapter<String>
更改构造函数:
public自定义网格(上下文c){
超级(c,R.layout.fragment_pizza);
}
现在更改 getView()
方法:
// Get the item
String item = getItem(position);
将此项目文本设置为您的视图,如果您需要更多数据集,请创建模型 class 并更改
extends ArrayAdapter<String>
与 extends ArrayAdapter<ModelClass>
// Get the item
ModelClass item = getItem(position);
现在在您的 Activity class 中,初始化 ModelClass,设置它的变量并使用 add 或 addAll 方法添加到适配器。
一次检查循环onTaskCompleted
方法。您正在创建适配器的实例并在循环内设置为 gridview 我认为在观察和检查您的代码后我发现了第一个问题。
其次,您处理填充行的适配器视图膨胀方法也是错误的,我可以说这不是正确的方法...
因此,请检查适配器和 onTaskCompleted
方法的以下代码。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
String[] Description = new String[responseJson.length()];
String[] ImageURL = new String[responseJson.length()];
JSONArray jArrayCategoryWise = new JSONArray();
for (int i = 0; i < responseJson.length(); i++) {
JSONObject object = responseJson.getJSONObject(i);
if ("1".equalsIgnoreCase(object.getString("MainCategoryID"))) {
// add to another jsonarray if MainCategoryID == 1
jArrayCategoryWise.put(object);
}
}
/******
* You can also create class with getter setter method for the
* Description and ImageUrl.Make the arraylist of the class after
* parsing jsonArray.
*/
// which will required on more iteration of your json array response
CustomGrid adapter = new CustomGrid(getActivity(),
jArrayCategoryWise);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
一旦检查数据是否正常,或者not.or你是否在两者之间添加任何东西
适配器
public class CustomGrid extends BaseAdapter {
private Context context;
private Context mContext;
// private final String[] Description;
// private final String[] ImageURL;
private JSONArray json;
private int size = 0;
public CustomGrid(Context c, JSONArray json) {
this.context = c;
this.json = json;
if (this.json != null)
size = this.json.length();
// You can also create class with getter setter method (getter setter
// for Descriptions and ImageUrl). and pass arraylist of that class
// this.Description = Description;
// this.ImageURL = ImageURL;
}
@Override
public int getCount() {
return size;
}
@Override
public Object getItem(int position) {
return this.json.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.custom_trips_frag_row, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
JSONObject jObjectRowData = this.json.getJSONObject(position);
holder.tvHeader.setText(jObjectRowData.getString("Description"));
Picasso.with(this.context).load(jObjectRowData.getString("ImageURL"))
.into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
请检查答案..希望我能正确回答你的问题并且你能得到你的答案... * 建议 * 一旦检查 ViewHolder class BaseAdapter 的模式。