如何在网格视图中设置不同的项目?

时间:2018-01-06 07:44:26

标签: android android-layout gridview layout

我有一个应用程序,其中我有两个视图,即图像视图和文本视图,并有两种类型的数据,如字符串和textdrawable和int.What我想在图像视图中设置一个textdrawable,并在int图像视图和textview将保持不变。我该怎么做

代码: -

LayoutInflater inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView=inflater.inflate(R.layout.dash_board_fragment_one_items, null);

    TextView name=(TextView)convertView.findViewById(R.id.tv_android);
    CircleImageView icon=(CircleImageView)convertView.findViewById(R.id.img_android);
    name.setText(android.get(position).getAndroid_version_name());
    icon.setImageDrawable(android.get(position).getAndroid_image_url());
    return convertView;

这是datamodel的代码

  private String android_version_name;
private TextDrawable android_image_url;
private int img;

public String getAndroid_version_name() {
    return android_version_name;
}

public void setAndroid_version_name(String android_version_name) {
    this.android_version_name = android_version_name;
}

public TextDrawable getAndroid_image_url() {
    return android_image_url;
}

public void setAndroid_image_url(TextDrawable android_image_url) {
    this.android_image_url = android_image_url;
}

public int getImg() {
    return img;
}

public void setImg(int img) {
    this.img = img;
}
}

1 个答案:

答案 0 :(得分:0)

img int变量的默认值设置为-1

private int img=-1;

现在在getView方法

if(android.get(position).getImg()==-1)
{
icon.setImageDrawable(android.get(position).getAndroid_image_url());
}
else
{
icon.setImageResource(android.get(position).getImg());
}

如果您没有资源文件

,请将您的img变量值保持为-1