简体   繁体   English

从列表视图隐藏/显示ImageView

[英]Hide/show ImageView from a listview

I am trying to hide/show imageview from a custom listview base on the condition. 我试图根据条件从自定义listview隐藏/显示imageview。 However, trying the below code is not hiding/showing the imageview based on the condition. 但是,尝试以下代码不会根据情况隐藏/显示imageview。 As you can see below inside mainactivity.java I have a String variable message now inside the private ArrayList generateData() method I have set message=nopic/message=yespic . 如下面在mainactivity.java中看到的那样,我现在在私有ArrayList generateData()方法中有一个String变量消息,我已经设置了message = nopic / message = yespic I am passing the String value (value of message) to my custom adapter. 我正在将字符串值(消息值)传递给我的自定义适配器。 Then inside my custom adapter if message=="nopic" hide the imageview from the listview else show imageview in listview. 然后在我的自定义适配器中,如果message ==“ nopic”从列表视图中隐藏图像视图,否则在列表视图中显示图像视图。

I have tried the following code but it's not hiding the imageview instead all the imageview are shown. 我已经尝试了以下代码,但是它没有隐藏imageview,而是显示了所有imageview。

Can you help me achieve this please 你能帮我实现这个吗

mainactivity.java mainactivity.java

public class MainActivity extends ActionBarActivity 

{ {

private String[] fruits = 
    {
        "Apple", "Mango", "Banana", "Grapes", "Straberries", "Pineapple", "Berry"
    };

private String message;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<Item> items=generateData();

    MyAdapter adapter=new MyAdapter(getApplicationContext(),items, message);
    final ListView listview = (ListView) findViewById(R.id.listView1);
    listview.setAdapter(adapter);

}
private ArrayList<Item> generateData()
{     
    MyAdapter adapter=new MyAdapter(getApplicationContext(),null, null);

    ArrayList<Item> items = new ArrayList<Item>();  
    for(int i=0; i<fruits.length; i++)
    {

        if(fruits[i].toString().equals("Grapes"))
        {
            message = "nopic";
            items.add(new Item("Grapes sweet", null));
            adapter.notifyDataSetChanged();
        }
        else
        {
            message = "yespic";
            items.add(new Item(fruits[i].toString(), null));
            adapter.notifyDataSetChanged();
        }
    }            
    return items;       
}

MyAdapter.java MyAdapter.java

public class MyAdapter extends ArrayAdapter<Item> {

    private final Context context;
    private final ArrayList<Item> itemsArrayList;
    public String message;

    public MyAdapter(Context context, ArrayList<Item> itemsArrayList, String message) {

        super(context, R.layout.row, itemsArrayList);

        this.context = context;
        this.itemsArrayList = itemsArrayList;
        this.message = message;

    }

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


        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        View rowView = inflater.inflate(R.layout.row, parent, false);


        TextView title = (TextView) rowView.findViewById(R.id.textView1);
        ImageView pic = (ImageView) rowView.findViewById(R.id.imageView1); 

        title.setText(itemsArrayList.get(position).getTitle());
        if(message == "nopic")
        {
            pic.setVisibility(View.GONE);

        }
        if(message == "yespic")
        {
            pic.setVisibility(View.VISIBLE);
            pic.setImageResource(R.drawable.abc_ic_voice_search);
        }

        return rowView;

    }

Instead of 代替

message == "yespic";

Try 尝试

message.equals("yespic"); 

You have to call 你必须打电话

adapter.notifyDataChanged();

every time you change the content of a listView. 每次更改listView的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM