我曾经创建了一个基于 BaseAdapter 类的自定义适配器,而不是 ArrayAdapter 。 但是这是一个问题,当我尝试使用新数据(无论是什么)填充我的适配器时,我的 AdapterView 代表了比它应该拥有的更多的项目。
假设我们有一个Activity,ListFragment和自定义适配器。
适配器的实现:
public class ContactAdapter extends BaseAdapter {
private List<Entry<String,String>> mDataModel;
private LayoutInflater mInflater;
private boolean[] isChecked;
private int mResourceId;
private static class ViewHolder {
private TextView adapterText;
private TextView adapterDate;
}
public ContactAdapter(Context context,int mResourceId) {
this(context, mResourceId,new ArrayList<Entry<String,String>>());
}
public ContactAdapter(Context context, int mResourceId,List<Entry<String,String>> mDataModel) {
this.mInflater = LayoutInflater.from(context);
this.mResourceId = mResourceId;
this.mDataModel = mDataModel;
this.isChecked = new boolean[mDataModel.size()];
}
@Override
public int getCount() {
return mDataModel.size();
}
@Override
public Entry<String,String> getItem(int position) {
return mDataModel.get(position);
}
public void selectItem(int position) {
isChecked[position] = !isChecked[position];
}
public void add(final Entry<String,String> entry) {
mDataModel.add(entry);
if(mDataModel.size()>isChecked.length) {
isChecked = Arrays.copyOf(isChecked, isChecked.length / 2 +
mDataModel.size());
}
notifyDataSetChanged();
}
public void unCheckItems() {
for (int index = 0; index < mDataModel.size(); index++)
isChecked[index] = false;
}
public void removeItem() {
for(int index=0;index<mDataModel.size();index++) {
if (isChecked[index])
mDataModel.remove(index);
}
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
return mDataModel.get(position).hashCode();
}
@Override
public View getView(int position,View convertView, ViewGroup parentGroup) {
ViewHolder holder;
if(convertView==null) {
convertView=mInflater.inflate(mResourceId,parentGroup,false);
holder=new ViewHolder();
holder.adapterDate=(TextView)(convertView.findViewById(R.id.adapterDate));
holder.adapterText=(TextView)(convertView.findViewById(R.id.adapterText));
convertView.setTag(holder);
}
else
holder=(ViewHolder)(convertView.getTag());
holder.adapterDate.setText(mDataModel.get(position).getValue());
holder.adapterText.setText(mDataModel.get(position).getKey());
convertView.setSelected(isChecked[position]);
return convertView;
}
}
我还有一个常用的 ListFragment 的实现,附加方法addTo(...),以便向 ListView 添加行。来自的数据来自活动。
以下是我在活动中调用以将数据发送到 ListFragment 的方法:
public void addToContact(View view) {
String contact = mContact.getText().toString();
if(!contact.isEmpty()) {
if (contactFragment == null) {
contactFragment = (ContactFragment) getSupportFragmentManager().findFragmentByTag(BundleKey.CONTACT_FRAGMENT);
}
Calendar calendar = Calendar.getInstance();
final Entry<String, String> entry = new Entry<>(contact,
calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
contactFragment.addEntry(entry);
}
mContact.getText().clear();
Toast.makeText(this,(contact.isEmpty()?
"Enter contact":"Contact has been added"),Toast.LENGTH_LONG).show();
}
所以,当我添加任何新项目时,我的ListView会显示两个相似的项目,而不是一个。你知道如何修复它吗?