从列表视图隐藏/显示ImageView

时间:2015-05-17 19:15:33

标签: java android listview

我试图根据条件隐藏/显示自定义列表视图中的imageview。但是,尝试以下代码不会根据条件隐藏/显示imageview。正如您在 mainactivity.java 中看到的,我现在在私有ArrayList generateData()方法中有一个String变量消息我设置了消息= nopic /消息= yespic 即可。我将String值(消息的值)传递给我的自定义适配器。然后在我的自定义适配器内,如果消息==“nopic”隐藏listview中的imageview,则在listview中显示imageview。

我尝试了以下代码,但它没有隐藏imageview,而是显示了所有的imageview。

请帮助我实现这个目标

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

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;

    }

2 个答案:

答案 0 :(得分:1)

而不是

message == "yespic";

尝试

message.equals("yespic"); 

答案 1 :(得分:0)

你必须致电

adapter.notifyDataChanged();

每次更改listView的内容。