Domanda

I have a fragment with gridview:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    context = container.getContext();

    View window = inflater
            .inflate(R.layout.shop_fragment, container, false);

    gridView = (GridView) window.findViewById(R.id.shop_root_view);
    gridView.setAdapter(new ShopAdapter(context, GRID_DATA));

    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            Toast.makeText(
                    context,
                    ((TextView) v.findViewById(R.id.grid_item_label))
                            .getText(), Toast.LENGTH_SHORT).show();
        }
    });
    return window;
}

And adapter:

public class ShopAdapter extends BaseAdapter {

    private Context context;
    private final String[] gridValues;

    // Constructor to initialize values
    public ShopAdapter(Context context, String[] gridValues) {

        this.context = context;
        this.gridValues = gridValues;
    }

    @Override
    public int getCount() {
        return gridValues.length;
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    // Number of times getView method call depends upon gridValues.length
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // LayoutInflator to call external grid_item.xml file

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from grid_item.xml ( Defined Below )

            gridView = inflater.inflate(R.layout.item_grid_shop, null);

            // set value into textview

            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);

            Log.d("Shop", "pozycja: " + position);
            textView.setText(gridValues[position]);

            // set image based on selected text

            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            imageView.setImageResource(R.drawable.ic_launcher);

        } else {

            gridView = (View) convertView;
        }

        return gridView;
    }
}

Every time I open that fragment, item from GRID_DATA appears on first and last position. Last item from GRID_VIEW does not appear. How to solve this? This is just example code, which I found on internet.

È stato utile?

Soluzione

The convertView usage is wrong here.

You should use it differently: - If is null, inflate the view - If not, do nothing (and will use it) Then you close that bracket and set your data.

Try this:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_grid_shop, null);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.grid_item_label);
    Log.d("Shop", "pozycja: " + position);
    textView.setText(gridValues[position]);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_item_image);
    imageView.setImageResource(R.drawable.ic_launcher);
    return convertView;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top