从列表中删除对象时,数组列表大小等于零

时间:2013-03-13 14:35:37

标签: android arraylist

我有数组列表,我在适配器类中使用它。一切都运行良好,但是当我从数组列表中删除特定索引时(在我的情况下为data),数组列表的大小等于零。

这是我的Adapter类

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()中从中获取数据之前检查了数据大小。

我该如何解决这个问题?

0 个答案:

没有答案