适配器在Android中的if条件下重复值

时间:2014-03-02 08:19:25

标签: android android-listview android-adapter

stackoverflow我真的想知道为什么Adapter在我if内放Adapter条件时会重复这些内容,请有人指导我纠正错误,在这里是我的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

提前致谢。

1 个答案:

答案 0 :(得分:2)

Recycle ListView

发生了这个问题

你需要改变:

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

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

请不要使用0将视图的可见性更改为Visible,而不是0使用View.VISIBLE