2013-10-02 47 views
0

我使用BaseExpandableListAdapter顯示數據是可擴展列表視圖。我有一個場景,我必須在小組中彈出並推動孩子。但不幸的是getchild方法爲每個孩子調用兩次,所以我不能刪除它們,因爲它給索引超出範圍異常。我錯過任何重要的事情嗎?爲什麼BaseExpandableListAdapter中的getchild方法從每個孩子調用兩次?

import android.content.Context; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.EditText; 
import android.widget.TextView; 

public class adpFolderWithTaskDone extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<clsTaskDoneDates> _listDataHeader; // header titles 
    SimpleDateFormat dateFormat1 = new SimpleDateFormat("EEE MMM dd"); 
    SimpleDateFormat dateFormat2 = new SimpleDateFormat("HH:mm"); 
    // child data in format of header title, child title 
    private HashMap<clsTaskDoneDates, List<clsTasks>> _listDataChild; 
    int i=0; 
    int j=0; 

    public ArrayList<View> ChildIds=new ArrayList<View>(); 


    Typeface tf ; 

    public adpFolderWithTaskDone(Context context, List<clsTaskDoneDates> listDataHeader, 
      HashMap<clsTaskDoneDates, List<clsTasks>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 

     tf = Typeface.createFromAsset(context.getApplicationContext().getAssets(), "font/HelveticaNeueLTStd-LtEx.otf"); 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final clsTasks childText = (clsTasks) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_tasks_done, null); 
     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItemDone); 

     txtListChild.setText(childText.getDescription()); 



     EditText txtTaskId = (EditText) convertView 
       .findViewById(R.id.TaskIdDone); 

     txtTaskId.setText(Integer.toString(childText.getTaskId())); 

     TextView lblTaskTime = (TextView) convertView.findViewById(R.id.lblTaskDueTimeDone); 

     if (childText.getTaskDate()==null) 
     { 
      //lblTaskTime.setText("Today"); 
     } 
     else 
     { 
     lblTaskTime.setText(dateFormat1.format(childText.getTaskDate()) + " at " + dateFormat2.format(childText.getTaskDate())); 
     } 



     if(ChildIds.contains(convertView)==false) 
     { 
     ChildIds.add(convertView); 
     } 

     txtListChild.setTypeface(tf, Typeface.NORMAL); 
     lblTaskTime.setTypeface(tf, Typeface.NORMAL); 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 


    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     clsTaskDoneDates headerTitle = (clsTaskDoneDates) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_folder_done, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.folderNameDone); 
     lblListHeader.setTypeface(tf, Typeface.NORMAL); 
     lblListHeader.setText(dateFormat1.format(headerTitle.getTaskDate())); 

     TextView lblFolderId = (TextView) convertView 
       .findViewById(R.id.CurrentfolderIdDone); 

     lblFolderId.setText(dateFormat1.format(headerTitle.getTaskDate())); 



     return convertView; 
    } 


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

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



} 
+0

請增加你的代碼 –

+0

顯示你的代碼 – HpTerm

回答

0

在組中添加和刪除子視圖我創建了適配器類的方法,並把它稱爲從我的活動在那裏我從數據庫中添加或刪除的項目。

public void removeChild(int groupPosition, int childPosition) { 

     if (getChildrenCount(groupPosition)>0 && getChildrenCount(groupPosition)-1 >= childPosition) 
     { 
      Object task = this.getChild(groupPosition, childPosition); 
      _listDataChild.remove(task); 
      this.notifyDataSetChanged(); 
     } 



    } 

    public void AddChild(int groupPosition, clsTasks Newchild) { 

     if (getGroupCount() > 0 && getGroupCount()-1 >= groupPosition) 
     {   


       _listDataChild.get(this.getGroup(groupPosition)).add(Newchild); 


      this.notifyDataSetChanged(); 
     } 



    } 
相關問題