0

I am trying to parse this JSON response onto a ListView in android where I am trying to modify the listview by showing an image depending upon the gender and adding a button for male records.

The logic is working fine, except for the last two entries.John Wayne & Leonardo Dicaprio are having details button, same is expected for Johnny Depp & Ravi Tambda.Please guide me for the same.

Using this code, I am trying to differentiate them according to the genders

 if(rowData.mgender.equalsIgnoreCase("male"))
                {
                    imageViewStatus.setImageResource(R.drawable.male);
                }
    else{
                    imageViewStatus.setImageResource(R.drawable.female);
                button.setVisibility(View.GONE);

                }

My output

EDIT 1

public View getView(final int position, View convertView, ViewGroup parent){
            ViewHolder holder = null;
            TextView title = null;
            TextView detail = null;
            TextView data=null;
            TextView message=null;

            ImageView imageViewStatus=null;
            Button button=null;

            final RowData rowData= getItem(position);
            if(null == convertView)

            {
                convertView = layoutInflater.inflate(R.layout.record, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }

            holder = (ViewHolder) convertView.getTag();

            button=(Button)holder.getProceedButton();


            message=holder.getEmail();
            message.setText(rowData.mEmail);

            title = holder.getName();
            title.setText(rowData.mName);

            detail = holder.getAddress();
            detail.setText(rowData.mAdress);                                                    

            data= holder.getPhoneNumber();
            data.setText(rowData.mMobile+" "+rowData.mOffice);

            imageViewStatus=holder.getImage();

            System.out.println("This is the gender "+rowData.mgender);
            if(rowData.mgender.equalsIgnoreCase("male"))
            {
                imageViewStatus.setImageResource(R.drawable.male);
}

            else{
                imageViewStatus.setImageResource(R.drawable.female);
                System.out.println("Button Visibility"+button.getVisibility());

                button.setVisibility(View.GONE);

            }

            return convertView;
        }
3
  • Before if condition just log (print) the rowData.mgender Commented Feb 7, 2013 at 12:52
  • It is giving me response as male, button button is not appearing. Commented Feb 7, 2013 at 12:53
  • Please include the whole getView() method in your question, you're probably making mistakes while reusing views. Commented Feb 7, 2013 at 12:56

1 Answer 1

1

Keep in mind that ListView children are always reused while you scroll them. When you set a property according to some condition in getView(), you have to revert that property when the condition isn't met. You're hiding the button when the gender is female, but when this view gets reused to populate a male contact, the button is still invisible and you have to set it as visible again.

Check this out:

if(rowData.mgender.equalsIgnoreCase("male"))
{
    imageViewStatus.setImageResource(R.drawable.male);
    button.setVisibility(View.VISIBLE); // You need to add this line in your code
} else {
    imageViewStatus.setImageResource(R.drawable.female);
    button.setVisibility(View.GONE);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. will try this. I have added getView method also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.