Jack
Jack

Reputation: 161

Hide/show ImageView from a listview

I am trying to hide/show imageview from a custom listview base on the condition. However, trying the below code is not hiding/showing the imageview based on the condition. 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. 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.

I have tried the following code but it's not hiding the imageview instead all the imageview are shown.

Can you help me achieve this please

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;

    }

Upvotes: 0

Views: 417

Answers (2)

xenteros
xenteros

Reputation: 15842

You have to call

adapter.notifyDataChanged();

every time you change the content of a listView.

Upvotes: 0

Andy Joyce
Andy Joyce

Reputation: 2832

Instead of

message == "yespic";

Try

message.equals("yespic"); 

Upvotes: 1

Related Questions