我有数组列表,我在适配器类中使用它。一切正常,但是当我从数组列表中删除特定索引(在我的情况下data
)时,数组列表的大小为零。
这是我的适配器类
public class MainLocalListAdapter extends BaseAdapter{
public static final int LIMIT_LIST_COUNT = 20;
private Activity activity;
private static ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
private int count = LIMIT_LIST_COUNT;
private int customPosition = 0;
public MainLocalListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
if(count > data.size()) count = data.size();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;//data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.playlist_local_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.waveTitle2); // title
holder.textView.setTypeface(UtiliShare.getTf());
holder.btnCancel = (ImageView) convertView.findViewById(R.id.btndel);
holder.btnCancel.setOnClickListener(mOnClickListener);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if(!data.isEmpty()) {
HashMap<String, String> song = data.get(position);
holder.btnCancel.setTag(position);
holder.textView.setText(song.get(UtiliShare.KEY_TITLE));
}
return convertView;
}
private static class ViewHolder{
TextView textView;
ImageView btnCancel;
}
private OnClickListener mOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainLocalListAdapter.this.activity, ""+v.getTag(), Toast.LENGTH_SHORT).show();
data.remove((Integer) v.getTag());
// Here data.size = 0 ????????????
int nval = data.size();
Log.d("list", ""+nval);
notifyDataSetChanged();
}
};
public int getCustomPosition() {
return customPosition;
}
public void setCustomPosition(int cPosition) {
customPosition = cPosition;
int nval = (data.size() - customPosition);
if(nval < LIMIT_LIST_COUNT) this.count = nval;
else count = LIMIT_LIST_COUNT;
}
}
我没有错误,因为我在从中获取数据之前检查了数据大小getView()
。
我怎样才能解决这个问题??