onkar
onkar

Reputation: 4547

Listview not showing consistent data android

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;
        }

Upvotes: 0

Views: 185

Answers (1)

Flávio Faria
Flávio Faria

Reputation: 6605

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);
}

Upvotes: 1

Related Questions