2017-10-05 125 views
0

複製當我率第一ExpandableListView東西:的RatingBar值ExpandableListView

Rating Bar Pic 1

後來,當我打開第二ExpandableListView或任何其他的值複製:

Rating bar Pic 2

我已從https://www.youtube.com/watch?v=jZxZIFnJ9jE&ab_channel=EDMTDev 中獲得幫助,並通過添加評分欄進行修改。 評級未保存在視圖中。

代碼:

的Java

expListview = (ExpandableListView)findViewById(R.id.expSkills); 
    initData(); 
    expListadapter = new ExpandablelistAdapter(getApplicationContext(),listDataheader,listHash); 
    expListview.setAdapter(expListadapter); 

    private void initData() 
    { 
    listDataheader = new ArrayList<>(); 
    listHash = new HashMap<>(); 

    listDataheader.add("Communication Skills"); 
    listDataheader.add("Social Skills"); 
    listDataheader.add("Music and Movement"); 
    listDataheader.add("Gross Motor Skills"); 
    listDataheader.add("Fine Motor Skills"); 
    listDataheader.add("Healthy Habits"); 

    List<String> a = new ArrayList<>(); 
    a.add("Follows Instructions :"); 
    a.add("Expresses in Words :"); 
    a.add("Asks Questions :"); 
    a.add("Answers Questions :"); 
    a.add("Uses Facial Expressions :"); 
    a.add("Listens and Enjoys :"); 

    List<String> b = new ArrayList<>(); 
    b.add("Shows Feelings and Emotions :"); 
    b.add("Participates in Group Activities :"); 
    b.add("Shares with Others :"); 
    b.add("Plays with Teacher :"); 
    b.add("Plays with all Friends :"); 
    b.add("Plays with only one or two Friends :"); 

    List<String> d = new ArrayList<>(); 
    d.add("Enjoys Physical Activities :"); 
    d.add("Enjoys EPL Activities :"); 
    d.add("Enjoys Playing :"); 
    d.add("Can Throw a Ball :"); 
    d.add("Can Catch a Ball :"); 
    d.add("Can Run,Jump :"); 
    d.add("Can Climb Equipment :"); 
    d.add("Can Balance on a Beam :"); 

    List<String> c = new ArrayList<>(); 
    c.add("Enjoys Listening Rhymes :"); 
    c.add("Enjoys Singing RRhymes :"); 
    c.add("Enjoys Performing Actions :"); 
    c.add("Enjoys Dancing :"); 
    c.add("Enjoys Attention of Others :"); 

    List<String> e = new ArrayList<>(); 
    e.add("Can String Beads :"); 
    e.add("Can Colour With Crayons :"); 
    e.add("Can Paint with Brush :"); 
    e.add("Can Put Together a Piece of Puzzle :"); 
    e.add("Can Stack a Net Of Blocks :"); 

    List<String> f = new ArrayList<>(); 
    f.add("Greets Everybody :"); 
    f.add("Eats Healthy Foode :"); 
    f.add("Drinks Water :"); 
    f.add("Eats Independently :"); 
    f.add("Happy to Come to School :"); 
    f.add("Plays Safely :"); 

    listHash.put(listDataheader.get(0),a); 
    listHash.put(listDataheader.get(1),b); 
    listHash.put(listDataheader.get(2),c); 
    listHash.put(listDataheader.get(3),d); 
    listHash.put(listDataheader.get(4),e); 
    listHash.put(listDataheader.get(5),f); 

} 

適配器

public class ExpandablelistAdapter extends BaseExpandableListAdapter 
{ 
private Context context; 
private List<String> listDataheader; 
private HashMap<String,List<String>> listHashMap; 

public ExpandablelistAdapter(Context context, List<String> listDataheader, HashMap<String, List<String>> listHashMap) 
{ 
    this.context = context; 
    this.listDataheader = listDataheader; 
    this.listHashMap = listHashMap; 
} 

@Override 
public Object getChild(int i, int i1) 
{return (listHashMap.get(listDataheader.get(i)).get(i1));} // i = Group Item , i1 = ChildItem 

@Override 
public long getChildId(int i, int i1) { 
    return i1; 
} 

@Override // i - group position , i1 - child position b - isLastchild 
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) 
{ 
    final String childText = (String)getChild(i,i1); 
    ViewHolder holder; 

    if(view==null) 
    { 
     LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.expandable_list_items, null); 

     holder = new ViewHolder(view); 
     view.setTag(holder); 
    } 

    holder = (ViewHolder)view.getTag(); 
    holder.features.setText(childText); 
    holder.ratingbar.setTag(i1); 

    holder.ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() 
    { 
     @Override 
     public void onRatingChanged(RatingBar ratingBar, float v, boolean b) 
     { 
      Toast.makeText(context, "Rating : " + ratingBar.getRating() + " Skills : "+ i +" Position : " + i1, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return view; 
} 

@Override 
public int getChildrenCount(int i) { return listHashMap.get(listDataheader.get(i)).size(); } 


@Override 
public Object getGroup(int i) { 
    return listDataheader.get(i); 
} 

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

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

@Override // i - group position b - isExpanded 
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) 
{ 
    String headerTitle = (String)getGroup(i); 
    if(view == null) 
    { 
     LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.expandable_list_group,null); 
    } 
    TextView listHeader = view.findViewById(R.id.expListHeader); 
    listHeader.setTypeface(null, Typeface.BOLD_ITALIC); 
    listHeader.setText(headerTitle); 
    return view; 
} 

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

@Override 
public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 

private static class ViewHolder 
{ 
    RatingBar ratingbar; 
    TextView features; 

    public ViewHolder(View view) { 
     ratingbar = view.findViewById(R.id.rate_img); 
     features = view.findViewById(R.id.expListItem); 
    } 
} 
} 

我已經浪費了很多時間在this..Where是我的問題呢?

+0

您好,您需要設置等級數''上getChildView' ratingbar',原因列表視圖將重新使用視圖=>它會保持以前的狀態就是爲什麼你必須設置' ratingbar' – vuhung3990

+0

yes,更新的答案被正確執行。 –

回答

0

當使用支架: - 永遠記得設置它的標籤時的觀點是new/null並且當它不是時得到它的標籤。 @azizbekian邏輯是存儲和檢索也有效的評價值。

代碼

@Override // i - group position , i1 - child position b - isLastchild 
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) 
    { 
    final String childText = (String)getChild(i,i1); 
    ViewHolder holder; 

    if(view==null) 
    { 
     LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.expandable_list_items, null); 

     holder = new ViewHolder(view); 
     view.setTag(holder); 
    } 

    else 
    { 
     holder = (ViewHolder)view.getTag(); 
    } 

    holder.ratingbar.setOnRatingBarChangeListener(onRatingChangedListener(holder, i, i1)); 

    holder.features.setText(childText); 
    holder.ratingbar.setTag(i1); 
    holder.ratingbar.setRating(arr[i][i1]); 

    return view; 
} 
0

ListView回收您的意見,並重新使用它們。一旦你檢查你的RatingBar並向下滾動,完全相同的視圖正在被重用。您已經檢查了評級欄並沒有指示重置它,因此您可以通過選中RatingBar來查看相同的視圖。

要解決你在某種數據結構的保留檢查評分欄值的問題,說在一個陣列:

 

    int[][] arr = new int[groupCount][childCount]; 

    public View getChildView(final int groupPosition, final int childPosition, boolean b, View view, ViewGroup viewGroup) 
    { 
     ... 
     holder = (ViewHolder) view.getTag(); 
     holder.ratingBar.setRating(arr[groupPosition][childPosition]); 
     ... 

     holder.ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 
      @Override 
      public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { 
       arr[groupPosition][childPosition] = (int) ratingBar.getRating(); 
      } 
     }); 

     return view; 
    } 
 
+0

我明白你在說什麼,我正在努力實現,但上面的代碼不起作用。 –

+0

這是一個草圖代碼,只是爲了讓您更進一步。 – azizbekian

+0

在這個適配器中,getChildView()被調用兩次..你明白爲什麼了嗎?如果不是這樣,我可以解決問題 –