Question

Hi stackoverflow I'm really wondering why Adapter repeats the stuff when we put if condition inside the Adapter, please someone guide me to correct my mistakes, here is my adapter

public class CustomStoreAdapter extends BaseAdapter  {
/** Variable declaration */
private ArrayList<StoresUtility> storeList = new ArrayList<StoresUtility>();
private Context context;

public CustomStoreAdapter(Context context) {
    this.context = context;
}// End of CustomAdapter constructor

public void setData(ArrayList<StoresUtility> serviceProviderList) {
    this.storeList = serviceProviderList;
    notifyDataSetChanged();
}

public int getCount() {
    return storeList.size();
}// End of getCount method

public Object getItem(int position) {
    return storeList.get(position);
}// End of getItem method

public long getItemId(int position) {
    return position;
}// End of getItemId method

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.stores_adapter, null);
    }// End of if block

    TextView name = (TextView) v.findViewById(R.id.tv_store_full_name);
    TextView firstLetter = (TextView) v.findViewById(R.id.tv_store_first_letter);
    TextView mobileOne = (TextView) v.findViewById(R.id.tv_contact_store_one);
    TextView mobileTwo = (TextView) v.findViewById(R.id.tv_contact_store_two);

    LinearLayout llOne = (LinearLayout) v.findViewById(R.id.ll_mobie_one);
    LinearLayout llTwo = (LinearLayout) v.findViewById(R.id.ll_mobie_two);

    RelativeLayout rlOne = (RelativeLayout) v.findViewById(R.id.rl_contact_store_one);
    RelativeLayout rlTwo = (RelativeLayout) v.findViewById(R.id.rl_contact_store_two);

    try {
        if(storeList.get(position).getName().length() > 0) {
            char initial = storeList.get(position).getName().charAt(0);
            firstLetter.setText(""+initial);
        }
    } catch(Exception e) { }

    name.setText(""+storeList.get(position).getName());

    if(!storeList.get(position).getMobileOne().equalsIgnoreCase("null")) {
        llOne.setVisibility(0);
        mobileOne.setText(""+storeList.get(position).getMobileOne());
    }

    if(!storeList.get(position).getMobileTwo().equalsIgnoreCase("null")) {
        llTwo.setVisibility(0);
        mobileTwo.setText(""+storeList.get(position).getMobileTwo());
    }

    return v;
}// End of getView method
}//End of CustomServiceAdapter class

Thanks in advance.

Was it helpful?

Solution

this problem happened for Recycle ListView

you need change :

if(!storeList.get(position).getMobileOne().equalsIgnoreCase("null")) {
        llOne.setVisibility(0);
        mobileOne.setText(""+storeList.get(position).getMobileOne());
    }

    if(!storeList.get(position).getMobileTwo().equalsIgnoreCase("null")) {
        llTwo.setVisibility(0);
        mobileTwo.setText(""+storeList.get(position).getMobileTwo());
    }

to

if(!storeList.get(position).getMobileOne().equalsIgnoreCase("null")) {
        llOne.setVisibility(View.VISIBLE);
        mobileOne.setText(""+storeList.get(position).getMobileOne());
    }
else
     llOne.setVisibility(View.GONE);

    if(!storeList.get(position).getMobileTwo().equalsIgnoreCase("null")) {
        llTwo.setVisibility(View.VISIBLE);
        mobileTwo.setText(""+storeList.get(position).getMobileTwo());
    }
else
   llTwo.setVisibility(View.GONE);

and please don't use 0 for changing visibility of your view to Visible, instead of 0 use View.VISIBLE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top