在gridview中显示项目

时间:2016-04-05 14:22:08

标签: android

我构建了一个imageadapter类来帮助我在gridview上显示图像。但是,网格确实显示,但网格中的项目不显示。我甚至无法弄清楚头痛的来源。我在项目活动课中有这个:

    gridView = (GridView) findViewById(R.id.gridView1);
    gridView.setAdapter(new ImageAdapter(getApplicationContext(), ITEMS));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(getApplicationContext(), ((TextView) v.findViewById(R.id.grid_item_label))
                            .getText(), Toast.LENGTH_SHORT).show();
        }
    });

关于适配器,以下是其内容:

private Context context;
private final String[] gridValues;

public ImageAdapter(Context context, String[] gridValues) {
    this.context = context;
    this.gridValues = gridValues;
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView;

    if (convertView == null) {
        gridView = new View(context);
        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);
        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(gridValues[position]);
        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);
        String mobile = gridValues[position];
        if (mobile.equals("Windows")) {
            imageView.setImageResource(R.drawable.windows_logo);
        } else if (mobile.equals("iOS")) {
            imageView.setImageResource(R.drawable.ios_logo);
        } else if (mobile.equals("Blackberry")) {
            imageView.setImageResource(R.drawable.blackberry_logo);
        } else {
            imageView.setImageResource(R.drawable.android_logo);
        }
    } else {
        gridView = (View) convertView;
    }
    return gridView;
}

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

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

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

拜托,我到底做错了什么?

0 个答案:

没有答案