如何适配TextView?

时间:2014-05-30 07:33:12

标签: android textview adapter drawable

我有一个适配器,它从Drawable中获取项目并将它们放入GridView中。适配器适用于Drawables。但是,我需要从Layout而不是Drawables获取TextViews。

这适用于Drawable:

    private Integer[] mThumbIds = {
        R.drawable.carrot, R.drawable.emerald,
        R.drawable.turquoise, R.drawable.greensea,
    };

但是,我尝试将项目路径更改为TextViews的ID:

    private Integer[] mThumbIds = {
        R.id.text_test,R.id.text_test,
        R.id.text_test,R.id.text_test,      
    };

应用程序运行正常但我没有看到TextViews,而是出现了GridView,但是项目什么都没有填充。适配器似乎能够导入TextViews,因为它们是可点击的,但没有任何内容。

我错过了什么?

我的TextView的XML:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/text_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!"
    android:background="#ff0000"/>

</GridLayout>

编辑:

ImageAdapter:

    public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

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

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

private int mItemHeight = 300;

// set photo item height
public void setItemHeight(int height) {
    if (height == mItemHeight) {
        return;
    }
    mItemHeight = height;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;



    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.carrot, R.drawable.emerald,
        R.drawable.turquoise, R.drawable.greensea,
        R.drawable.nephritis, R.drawable.peterriver,
        R.drawable.carrot, R.drawable.emerald,
        R.drawable.turquoise, R.drawable.greensea,
        R.drawable.nephritis, R.drawable.peterriver,
        R.drawable.carrot, R.drawable.emerald,
        R.drawable.turquoise, R.drawable.greensea,
        R.drawable.nephritis, R.drawable.peterriver,



};
}

我尝试将ImageView更改为TextView,而不是将其更改为此。但是,它现在在GridView的每个视图中返回“false”。我接近了吗?

TextView适配器:

    public View getView(int position, View convertView, ViewGroup parent) {

    TextView textView;


    if (convertView == null) {  // if it's not recycled, initialize some attributes
        textView = new TextView(mContext);
        textView.setLayoutParams(new GridView.LayoutParams(300, 300));
        //textView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        textView.setPadding(0, 0, 0, 0);
    } else {
        textView = (TextView) convertView;
    }

    textView.setText(mThumbIds[position]);
    return textView;
}

// references to our images
private Integer[] mThumbIds = {
        R.id.text_test,R.id.text_test,
        R.id.text_test,R.id.text_test,
        R.id.text_test,R.id.text_test,
        R.id.text_test,R.id.text_test,

};

}

1 个答案:

答案 0 :(得分:0)

AFAK,您要为网格布局中的每个项目设置背景。 所以背景应该是一个图像(可绘制)而不是文本。 setBackgroud或setText ... 可能你应该在适配器上传你的代码