如何在GridView中将文本为空或空时显示图像,反之亦然?

时间:2015-07-30 06:03:04

标签: java android gridview android-listview android-studio

我创建了一个包含图像和文本的网格视图。 现在我想只显示文本为空或空时的图像。 这是当文字为空或&#34时显示图像;"文本存在时隐藏图像。

这是片段中的代码......

public class fragment_detail_both extends Fragment {

GridView mGridView;
ProgressBar progress;
List<SampleTry> rowItems;




public static final String[] web = new String[]{

        "Pinterest",
        "Quora",
        "Twitter",
        "Vimeo msndjkcns nzcjkxns nkjcsns",
        "WordPress",
        "Youtube",
        "Stumbleupon",
        "SoundCloud msnd  hekko sdjkl kdksdon",
        "Reddit",
        "Blogger",
        "Github",
        "Instagram",
        "Blogger ",
        "Facebook",
        "Flickr",
        "Pinterest",
        "Quora",
        "Twitter",
        "Vimeo",
        "WordPress",
        "Youtube",
        "Stumbleupon",
        "SoundCloud",
        "Reddit"
};
public static final Integer[] images = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
        R.drawable.image7,
        R.drawable.image8,
        R.drawable.image9,
        R.drawable.image10,
        R.drawable.image11,
        R.drawable.image12,
        R.drawable.image13,
        R.drawable.image14,
        R.drawable.image15,
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
        R.drawable.image7,
        R.drawable.image8,
        R.drawable.image9,
        R.drawable.image10,
        R.drawable.image11,
        R.drawable.image12,
        R.drawable.image13
};


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_detail_both, container, false);

    rowItems = new ArrayList<>();
    for (int i = 0; i < web.length; i++) {
        SampleTry item = new SampleTry(web[i], images[i]);
        rowItems.add(item);
    }

    mGridView = (GridView) view.findViewById(R.id.grid_view);

    Detail_adaptor adapter = new Detail_adaptor(getActivity(), rowItems);
    mGridView.setAdapter(adapter);

    progress = (ProgressBar) getActivity().findViewById(R.id.detail_progress);
    progress.setVisibility(View.INVISIBLE);


    return view;
}


}

和Detail_adapter是

public class Detail_adaptor extends BaseAdapter {

Context mContext;
List<SampleTry> rowItems;


public Detail_adaptor(Context context, List<SampleTry> items) {

    mContext = context;
    this.rowItems = items;
}


@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

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

private class MyViewHolder {
    TextView textView;
    ImageView imageView;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.single_layout, parent, false);
        holder = new MyViewHolder();

            holder.textView = (TextView) convertView.findViewById(R.id.grid_text);

        holder.imageView = (ImageView) convertView.findViewById(R.id.grid_image);
        convertView.setTag(holder);
    } else {
        holder = (MyViewHolder) convertView.getTag();
    }

    SampleTry sampleTry = (SampleTry) getItem(position);

    holder.textView.setText(sampleTry.getWeb());
    holder.imageView.setImageResource(sampleTry.getImagesId());

    return convertView;
}


 }

和SampleTry类是

public class SampleTry {
String web;
int imagesId;

public SampleTry(String web, int imagesId) {
    this.web = web;
    this.imagesId = imagesId;
}

public String getWeb() {
    return web;
}

public void setWeb(String web) {
    this.web = web;
}

public int getImagesId() {
    return imagesId;
}

public void setImagesId(int imagesId) {
    this.imagesId = imagesId;
}

@Override
public String toString() {
    return web;
}
}

2 个答案:

答案 0 :(得分:2)

你可以尝试这样..

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.single_layout, parent, false);
        holder = new MyViewHolder();

            holder.textView = (TextView) convertView.findViewById(R.id.grid_text);

        holder.imageView = (ImageView) convertView.findViewById(R.id.grid_image);
        convertView.setTag(holder);
    } else {
        holder = (MyViewHolder) convertView.getTag();
    }

    SampleTry sampleTry = (SampleTry) getItem(position);

    if (sampleTry.getWeb() != null && ! sampleTry.getWeb().isEmpty()) {
        holder.textView.setText(sampleTry.getWeb());
        holder.textView.setVisibility(View.VISIBLE);
        holder.imageView.setVisibility(View.GONE);
    } else {
        holder.imageView.setImageResource(sampleTry.getImagesId());
        holder.imageView.setVisibility(View.VISIBLE);
        holder.textView.setVisibility(View.GONE);
    }

    return convertView;
}

希望它对你有用。

答案 1 :(得分:1)

getView()功能中你必须使用类似这样的东西

...
if(sampleTry.getWeb().isEmpty() || sampleTry.getWeb() == null){
  holder.imageView.setImageResource(sampleTry.getImagesId());
  holder.imageView.setVisibility(View.VISIBLE);      
  holder.textView.setVisibility(View.GONE);
}else{
  holder.textView.setText(sampleTry.getWeb());
  holder.imageView.setVisibility(View.GONE);
  holder.textView.setVisibility(View.VISIBLE);
}
...

而不是

holder.textView.setText(sampleTry.getWeb());
holder.imageView.setImageResource(sampleTry.getImagesId());

玩得开心。