gpt4 book ai didi

android - 动态加载数据到 Gridview

转载 作者:行者123 更新时间:2023-11-29 01:35:47 24 4
gpt4 key购买 nike

当我在 gridview 上工作时,我遇到了以下问题,我们将不胜感激,

  1. 当我将数据加载到我的 gridview 时,它只加载数组的前 3 项,但还有 18 项要加载。为什么它不加载其他 15 个项目。 (Log.i 它显示了我的 LogCat 中的所有 18 个项目)。

  2. 由于只有 18 个项目属于 MainCategoryID=1,它应该只加载 18 个网格,但是这加载了 28 个网格(这是因为 responseJson 中有 28 个项目)。我想将它限制为 18 个网格。 (加载的28个格子里面,加载了3个item,有几处重复,其他格子都是空格子)

PizzaFragment 类

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;
}
}

这是我的屏幕的样子(这不是真实的图像,但这是它的样子)

enter image description here

最佳答案

一次检查循环onTaskCompleted 方法。您正在创建适配器的实例并在循环内设置为 gridview 我认为在观察和检查您的代码后我发现第一个问题是错误的。

其次,您处理用于填充行的适配器 View 膨胀方法的方式也是错误的,我可以说这不是正确的方式...

因此,检查适配器和 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();
}
}

一旦检查数据是否正常传输。或者您是否在两者之间添加任何内容

适配器

    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 BaseAdapter 的类模式。

关于android - 动态加载数据到 Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28166003/

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