0

我有一个自定义 Gridview 来显示操作员列表及其图标和提供者的名称。我的问题是 Gridview 仅显示有限的提供者(仅 12 个)。但是要显示的值超过 16 个。剩余值正在重复在 Gridview 中滚动 GridView。

我的源代码在这里

public class ImageAdapter extends BaseAdapter {

    public int getCount() {
        return providerlist1.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        ImageView imageView;
        if (convertView == null) {
            LayoutInflater layoutInflater = getLayoutInflater();
            v = layoutInflater.inflate(R.layout.icon_with_text, null);
            TextView txt = (TextView) v.findViewById(R.id.icon_text);
            txt.invalidate();
            txt.setTypeface(localTypeface1);
            Collections.sort(providerlist1);
            Log.v("COLLECTIONS", ""+providerlist1);
            txt.setText("" + providerlist1.toArray()[position]);
            String string = (String) txt.getText();
            icon = string.toLowerCase();
            System.out.println(icon);
            int resid = getResources().getIdentifier("" + icon, "drawable",
                    "com.digient.yeldi.app");
            imageView = (ImageView) v.findViewById(R.id.icon_image);
            imageView.setImageResource(resid);

        } else {
            v = convertView;
        }

        return v;

    }

}

providerlist1 是一个数组列表,其中有超过 16 个操作符值。

4

1 回答 1

0

为此使用下面的代码,可能会对您有所帮助。

public class ImageAdapter extends BaseAdapter {

    LayoutInflater layoutInflater;
    Context ctx;
    public ImageAdapter(Context context) {
        // TODO Auto-generated constructor stub
        layoutInflater=LayoutInflater.from(context);
        this.ctx=context;
    }

    public int getCount() {
        return providerlist1.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        ImageView imageView;
        if (convertView == null) {
            v = layoutInflater.inflate(R.layout.icon_with_text, null);
        } else {
            v = convertView;
        }

        TextView txt = (TextView) v.findViewById(R.id.icon_text);
        txt.invalidate();
        txt.setTypeface(localTypeface1);
        Collections.sort(providerlist1);
        Log.v("COLLECTIONS", ""+providerlist1);
        txt.setText("" + providerlist1.toArray()[position]);
        String string = (String) txt.getText();
        icon = string.toLowerCase();
        System.out.println(icon);
        int resid = getResources().getIdentifier("" + icon, "drawable", "com.digient.yeldi.app");
        imageView = (ImageView) v.findViewById(R.id.icon_image);
        imageView.setImageResource(resid);

        return v;

    }

}
于 2012-06-11T07:28:26.060 回答