动态加载数据到Gridview

时间:2015-01-27 08:23:41

标签: android gridview

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

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

  2. 由于只有18个项目属于MainCategoryID = 1,因此它只应加载18个网格,但这会加载28个网格(这是因为responseJson中有28个项目)。我想将它限制为18个网格。 (在加载的28个网格中,有3个项目加载,并且在几个地方重复,其他网格是空网格)

  3. 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();
            }
        }
    

    CustomGrid

    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

2 个答案:

答案 0 :(得分:1)

检查循环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();
        }
    }

检查数据是否正常后,或者您在

之间添加任何内容

ADAPTER

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

请检查答案..希望我正确地问你的问题,然后你的答案...... *建议* 一起检查BaseAdapter的ViewHolder类模式。

答案 1 :(得分:0)

首先,更改适配器以消耗

extends ArrayAdapter<String>

更改构造函数:     public CustomGrid(Context c){            super(c,R.layout.fragment_pizza);         } 现在更改getView()方法:

// Get the item
        String item = getItem(position);

将此项目文本设置为您的视图,如果您需要更多数据集,请创建模型类并进行更改 extends ArrayAdapter<String> extends ArrayAdapter<ModelClass> // Get the item ModelClass item = getItem(position);

{{1}}

现在在您的Activity类中,初始化ModelClass,设置它的变量并使用add或addAll方法添加到适配器。