2014-10-02 58 views
0

對此進行了幾天研究之後,我發佈了我的問題,希望您能夠幫助我。 我正在使用ExpandableListView和CursorTreeAdapter來顯示數據庫中的數據。該組的數據顯示沒有問題。當我展開一個組來展示孩子時,我會上下滾動,孩子們的數據會發生變化。例如父母A,B,C有一個孩子,每個孩子都有數據A1,B1和C1。當我展開父A和C並向上和向下滾動時,A1數據更改(大多數情況下爲B1數據)。只有當我崩潰C的溫度將A1數據爲母公司A.帶CursorTreeAdapter的ExpandableListView當滾動上下時展開擴展組更改

子佈局

你可以建議,爲什麼它是happenning,什麼可以做再一次說明了什麼?

下面是從newGroupView,bindGroupView,newChildView,bindChildView我CursorTreeAdapter代碼:

public class MyCursorTreeAdapter extends CursorTreeAdapter { 

    String name = ""; 
    String phone = ""; 
    String cityStreet = ""; 
    String userNote2 = ""; 
    DBConnector values; 
    long id = 0; 
    DBConnector cmpDb; 
    LayoutInflater mInflater; 
    Context context; 
    int oldGroupPosition; 

    public MyCursorTreeAdapter(Context context, Cursor cursor) { 
     super(cursor, context); 
     this.context = context; 
     values = new DBConnector(context); 
     this.values = values; 
     cmpDb = new DBConnector(context); 
    } 

    @Override 
    public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { 
     final View view = mInflater.inflate(R.layout.group_list_view, parent, false); 
     return view; 
    } 

    @Override 
    public void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { 

     TextView textsubText1 = (TextView) view.findViewById(R.id.subText1); 
     TextView textsubText2 = (TextView) view.findViewById(R.id.subText2); 

     textsubText1.setText(cursor.getString(cursor.getColumnIndex(values.Name))); 
     textsubText2.setText(cursor.getString(cursor.getColumnIndex(values.Name))); 

     userNote2 = cursor.getString(cursor.getColumnIndex(values.Note2)); 
    } 

    @Override 
    public View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) { 
     final View view = mInflater.inflate(R.layout.child_view, parent, false); 
     return view; 
    } 

    @Override 
    public void bindChildView(final View view, final Context context, final Cursor cursor, boolean isLastChild) { 
     values.open(); 
     TextView nameTv = (TextView) view.findViewById(R.id.tvName); 
     TextView phoneTv = (TextView) view.findViewById(R.id.tvPhone); 
     TextView addressTv = (TextView) view.findViewById(R.id.tvAddress); 

     ImageButton btData1 = (ImageButton) view.findViewById(R.id.btData1); 
     ImageButton btData2 = (ImageButton) view.findViewById(R.id.btData2); 
     ImageButton btSaveData = (ImageButton) view.findViewById(R.id.btSaveData); 

     extractDataFromUserNote2(); 

     nameTv.setText(""+ name); 
     phoneTv.setText(""+ phone); 
     addressTv.setText(""+ cityStreet); 

     android.view.View.OnClickListener doWithBtData1 = new Button.OnClickListener() { 
      @Override 
     public void onClick(View v) { 
       // action on btData1 
      } 
     }; 

     android.view.View.OnClickListener doWithBtData2 = new Button.OnClickListener() { 
      @Override 
     public void onClick(View v) { 
       // action on btData2 
      } 
     }; 


     android.view.View.OnClickListener doWithBtSaveData = new Button.OnClickListener() { 
      @Override 
     public void onClick(View v) { 
       // action on btSaveData 
      } 
     }; 

     btData1.setOnClickListener(doWithBtData1); 
     btData2.setOnClickListener(doWithBtData2); 
     btSaveData.setOnClickListener(doWithBtSaveData); 
     values.close(); 

    } 

    @Override 
    protected Cursor getChildrenCursor(Cursor groupCursor) { 
     return getCursor(); 
    } 

    @Override 
    public int getChildrenCount (int groupPosition) { 
     return 1; 
    } 

     @Override 
    public void onGroupCollapsed (int groupPosition) { 
    } 

    @Override 
    public void onGroupExpanded (int groupPosition) { 
    } 

    protected Cursor setChildrenCursor(Cursor groupCursor) { 
     return groupCursor; 
    } 

    @Override 
    public void setGroupCursor (Cursor cursor) { 

    } 

感謝。

回答

1

好吧,經過多日的研究,我能夠找到一種方式這個問題。我添加了一個OnScroll回調函數,一旦用戶在列表中滾動,就會摺疊擴展組。

我希望我能找到更優雅的方式,但它工作。

下面是代碼:

在主參數列表:

int previousItem = -1; 

回調函數:

elv.setOnScrollListener(new OnScrollListener() { 
    // This call back function close the group on scroll. It is used to override the but of view change. 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     if (previousItem != -1 && scrollState == 1) { 
      elv.collapseGroup(previousItem); 
      previousItem = -1; 
     } 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
    } 
}); 

回調函數來管理的展開/收起組:

elv.setOnGroupExpandListener(new OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 
     if(groupPosition != previousItem) { 
      elv.collapseGroup(previousItem); 
     } 
     previousItem = groupPosition; 
    } 
}); 

我希望這會幫助你。

相關問題