android expandlistview删除项目

时间:2015-06-09 08:00:00

标签: android items

问题描述:

当我点击该组时,它会展开并显示childView,然后点击childView中的“删除”按钮,弹出一个AlertDialog,点击“确认”按钮后,当前项目将从列表中删除,但是我目前的代码无法达到这个效果。有人有什么想法吗?

这是我的代码:

public class ManageFragment extends FragmentBase{

private ExpandableListView mExpandableListView;
private ExpandListAdapter mAdapter;
private List<String> mGroups;
private HashMap<String, List<String>> mChilds;
private Button openBtn;
private TextView download_history;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_manage, container, false);
    prepareData();
    init(view);
    return view;
}
private void init(View view) {
    mAdapter = new ExpandListAdapter(getActivity(), mGroups, mChilds);
    mExpandableListView = (ExpandableListView) view.findViewById(R.id.expand_listview);
    download_history = (TextView) view.findViewById(R.id.download_history);

    int number = mGroups.size();
    download_history.setText(String.valueOf(number));


    mExpandableListView.setAdapter(mAdapter);


    mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            for(int i = 0; i < mAdapter.getGroupCount(); i++){
                if(groupPosition != i){
                    mExpandableListView.collapseGroup(i);
                }
            }

        }
    });



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

private void prepareData() {
    mGroups = new ArrayList<String>();
    mChilds = new HashMap<String, List<String>>();

    mGroups.add("Group 1");
    mGroups.add("Group 2");
    mGroups.add("Group 2");
    mGroups.add("Group 2");
    mGroups.add("Group 2");
    mGroups.add("Group 2");
    mGroups.add("Group 2");

    List<String> menu = new ArrayList<String>();
    menu.add("menu 1"); 

    mChilds.put(mGroups.get(0), menu);
    mChilds.put(mGroups.get(1), menu);
}

}
 public class ExpandListAdapter extends BaseExpandableListAdapter implements OnClickListener{

private Context mContext;
private List<String> mGroups;
private HashMap<String, List<String>> mChilds;
private boolean flag = true;    
private TextView delete_local,delete_confirm,delete_cancel;
AlertDialog dialog;

public ExpandListAdapter(Context context, List<String> groups,
        HashMap<String, List<String>> childs) {
    mContext = context;
    mGroups = groups;
    mChilds = childs;
}

/**
 * get size list group
 */
@Override
public int getGroupCount() {
    return mGroups.size();
}

/**
 * get size current list child
 */
@Override
public int getChildrenCount(int groupPosition) {
    return mChilds.get(mGroups.get(groupPosition)).size();
}

/**
 * get object group
 */
@Override
public Object getGroup(int groupPosition) {
    return mGroups.get(groupPosition);
}

/**
 * get object child
 */
@Override
public Object getChild(int groupPosition, int childPosition) {
    return mChilds.get(mGroups.get(groupPosition)).get(childPosition);
}

/**
 * get current group id
 */
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

/**
 * get current child id
 */
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    //String groupText = (String) getGroup(groupPosition);
    ViewHolder holder = null;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_layout, null);
        holder.expand_imgView = (ImageView) convertView.findViewById(R.id.expand_list_image);
        holder.rank_download = (Button) convertView.findViewById(R.id.rank_download);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.rank_download.setOnClickListener(this);

    if(isExpanded){
        holder.expand_imgView.setBackgroundResource(R.drawable.manage_collapse);
    }else{
        holder.expand_imgView.setBackgroundResource(R.drawable.manage_expand);
    }

    return convertView;
}

class ViewHolder{
    ImageView expand_imgView;
    Button manage_share,manage_redownload,manage_delete,rank_download;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    //String childText = (String) getChild(groupPosition, childPosition);
    ViewHolder holder = null;
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_layout, null);

        holder.manage_share = (Button) convertView.findViewById(R.id.manage_share);
        holder.manage_redownload = (Button) convertView.findViewById(R.id.manage_redownload);
        holder.manage_delete = (Button) convertView.findViewById(R.id.manage_clean);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.manage_share.setOnClickListener(this);
    holder.manage_redownload.setOnClickListener(this);
    holder.manage_delete.setOnClickListener(this);
    holder.manage_delete.setTag(groupPosition);


    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
    case R.id.manage_share:
        Toast.makeText(mContext, "share", 3).show();
        break;

    case R.id.manage_redownload:
        Toast.makeText(mContext, "redownload", 3).show();
        break;

    case R.id.manage_clean:
        showDeleteDialog();
        break;

    case R.id.rank_download:
        Toast.makeText(mContext, "open", 3).show();
        break;

    case R.id.delete_local:
        if (flag == false) {
            delete_local.setCompoundDrawablesWithIntrinsicBounds(          
                    R.drawable.checkbox_pressed, 0, 0, 0);  
            flag = true;
        }else if(flag==true){
            delete_local.setCompoundDrawablesWithIntrinsicBounds(R.drawable.checkbox,0,0, 0);
            flag=false;
        }
        break;

    case R.id.delete_confirm:
        Object tag = v.getTag();
        System.out.println("tag------------>" + tag);
        if(null != tag){
            mGroups.remove((Integer)tag);
            mChilds.remove((Integer)tag);
            dialog.dismiss();
            this.notifyDataSetChanged();
        }else{
            dialog.dismiss();

        }
        break;
    case R.id.delete_cancel:
        dialog.dismiss();
        break;
    default:
        break;
    }
}

private void showDeleteDialog() {
    dialog = new AlertDialog.Builder(mContext).create();
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
    delete_local = (TextView) view.findViewById(R.id.delete_local);
    delete_confirm = (TextView) view.findViewById(R.id.delete_confirm);
    delete_cancel = (TextView) view.findViewById(R.id.delete_cancel);
    delete_local.setOnClickListener(this);
    delete_confirm.setOnClickListener(this);
    delete_cancel.setOnClickListener(this);

    view.setMinimumWidth((int) (((Activity) mContext).getWindowManager()  
            .getDefaultDisplay().getWidth() * 0.8));
    dialog.setView(view);
    dialog.setInverseBackgroundForced(true);

    dialog.show();
}

}

1 个答案:

答案 0 :(得分:0)

我认为你的问题在这里

 case R.id.manage_clean:
    int pos = (int)v.getTag();
    showDeleteDialog(pos);
    break;

您没有在此代码中的任何位置为delete_confirm设置标记。您将其设置为manage_delete。所以我会将这个位置传递给你的showDeleteDialog方法,如此

private void showDeleteDialog(int pos) {
    dialog = new AlertDialog.Builder(mContext).create();
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
    delete_local = (TextView) view.findViewById(R.id.delete_local);
    delete_confirm = (TextView) view.findViewById(R.id.delete_confirm);
    delete_cancel = (TextView) view.findViewById(R.id.delete_cancel);
    delete_local.setOnClickListener(this);
    delete_confirm.setOnClickListener(this);
    delete_cancel.setOnClickListener(this);

    delete_confirm.setTag(pos);

    view.setMinimumWidth((int) (((Activity) mContext).getWindowManager()  
        .getDefaultDisplay().getWidth() * 0.8));
    dialog.setView(view);
    dialog.setInverseBackgroundForced(true);

    dialog.show();
}

然后在showDeleteDialog方法中将pos设置为delete_confirm的标记

ticketTable