Question description:
When I click the group ,it expand and show the childView, then click the 'delete' button in childView , it pops up an AlertDialog and after click the 'confirm' button ,the current item will be removed from the list,but My current code can not achieve this effect. Anyone has any ideas?
here is my code:
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();
}
}
I think your problem is here
You're not setting a tag to delete_confirm anywhere in this code. You're setting it to manage_delete. So I would pass the the position into your showDeleteDialog method like so
And then set pos as the tag for delete_confirm in your showDeleteDialog method