2016-07-04 22 views
0

私はカスタムExpandablelistviewを作成しました。リストビューにはプラスとマイナスのイメージビューと1つのテキストビューが含まれています。しかし、次の行に移動すると、値の増分が2から始まります。子アイテムごとに値を0から増やしたいとします。拡張可能リストビュー内のテキストの値を増やす

public class ExpandListAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private ArrayList<Group> groups; 
    ArrayList<Child> ch_list=new ArrayList<Child>(); 
    private ViewHolder viewHolder; // make it global 
    private int count=0; 
    int[] myIntegerArray = new int[10100]; 

    public class ViewHolder { 

     TextView tv ; 
     ImageView food_image; 
     ImageView minus,plus ; 
     TextView item_count; 

    } 


    public ExpandListAdapter(Context context, ArrayList<Group> groups) { 
     this.context = context; 
     this.groups = groups; 

    } 


    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition).getItems(); 
     return chList.get(childPosition); 
    } 

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

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

     final Child child = (Child) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater inflator = LayoutInflater.from(parent.getContext()); 
      convertView = inflator.inflate(R.layout.detail_list, null); 
      viewHolder = new ViewHolder(); 

      viewHolder.tv = (TextView) convertView.findViewById(R.id.type); 
      viewHolder.food_image = (ImageView) convertView.findViewById(R.id.food_image); 
      viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus); 
      viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus); 
      viewHolder.item_count = (TextView) convertView.findViewById(R.id.count); 


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

     viewHolder.tv.setText(child.getChildName()); 
     viewHolder.item_count.setText(child.getCount()); 
     viewHolder.plus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Child modelChild = groups.get(groupPosition).getItems().get(childPosition); 
       count = count + 1; 
       modelChild.setCount(count); 
       modelChild.setChildName(modelChild.getChildName()); 
       // set your other items if any like above 
       groups.get(groupPosition).getItems().set(childPosition, child); 
       notifyDataSetChanged(); 
      } 
     }); 
     viewHolder.minus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(count!=0) { 
        Child modelChild = groups.get(groupPosition).getItems().get(childPosition); 
        count = count - 1; 
        modelChild.setCount(count); 
        modelChild.setChildName(modelChild.getChildName()); 
        // set your other items if any like above 
        groups.get(groupPosition).getItems().set(childPosition, child); 
        notifyDataSetChanged(); 
       } 
      } 
     }); 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition).getItems(); 
     return chList.size(); 
    } 

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

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     Group group = (Group) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater inf = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = inf.inflate(R.layout.group_item, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.group_name); 
     tv.setText(group.getName()); 
     ExpandableListView eLV = (ExpandableListView) parent; 
     int count = getGroupCount(); 
     if(count<1){ 

      eLV.expandGroup(groupPosition); 
      // eLV.setGroupIndicator(null); 
     } 


     return convertView; 
    } 



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

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

} 

子供モデル:

public class Child { 

    private String Name; 
    private int Image,count; 

    public String getName() { 
     return Name; 
    } 

    public void setName(String Name) { 
     this.Name = Name; 
    } 

    public int getImage() { 
     return Image; 
    } 

    public void setImage(int Image) { 
     this.Image = Image; 
    } 

    public void setcount(int count) { 
     this.count = count; 
    } 

    public int getcount() { 
     return count; 
    } 
} 

グループモデル:

public class Group { 

    private String name; 
    private ArrayList<Child> Items; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public ArrayList<Child> getItems() { 
     return Items; 
    } 

    public void setItems(ArrayList<Child> Items) { 
     this.Items = Items; 
    } 



} 

Logcatエラー:

FATAL EXCEPTION: main 
                    Process: abservetech.com.foodapp, PID: 14471 
                    android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 
                     at android.content.res.Resources.getResourcePackageName(Resources.java:1871) 
                     at android.content.res.SPRDResources.getThemeResources(SPRDResources.java:94) 
                     at android.content.res.SPRDResources.getText(SPRDResources.java:155) 
                     at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
                     at android.widget.TextView.setText(TextView.java:3927) 
                     at abservetech.com.foodapp.ExpandListAdapter.getChildView(ExpandListAdapter.java:83) 
                     at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451) 
                     at android.widget.AbsListView.obtainView(AbsListView.java:2280) 
                     at android.widget.ListView.measureHeightOfChildren(ListView.java:1271) 
                     at android.widget.ListView.onMeasure(ListView.java:1183) 
                     at android.view.View.measure(View.java:16512) 

enter image description here

+0

問題は正しくインクリメントされません。 – Nisarg

+0

最初はカウント値がすべての子アイテムで0です.1行目でプラスをクリックすると、カウント値が0から1に増加しました。その後、2行目をクリックするとカウント値が1から2に増加しました。 2行目で1から2ではなく0から1のカウント値を増やす –

+0

POJOクラスの値で静的なカウントではなく、今使っているのです – Nisarg

答えて

1
private ViewHolder viewHolder; // make it global 

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

    Child child = (Child) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater inflator = LayoutInflater.from(parent.getContext()); 
     convertView = inflator.inflate(R.layout.detail_list, null); 
     viewHolder = new ViewHolder(); 

     viewHolder.tv = (TextView) convertView.findViewById(R.id.type); 
     viewHolder.food_image = (ImageView) convertView.findViewById(R.id.food_image); 
     viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus); 
     viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus); 
     viewHolder.item_count = (TextView) convertView.findViewById(R.id.count); 


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

    viewHolder.childName.setText(child.getChildName()); 
    viewHolder.item_count.setText(child.getCount()); 
    viewHolder.ivPlus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
       Child modelChild = groups.get(groupPosition).getChildrens().get(childPosition); 

       if(modelChild.getCount()>=0) // set your count default 0 when you bind data initially 
       int count = modelChild.getCount() + 1; 

       modelChild.setCount(count); 
       modelChild.setChildName(modelChild.getChildName()); 
       // set your other items if any like above 
       groups.get(groupPosition).getChildrens().set(childPosition, child); 
       notifyDataSetChanged(); 
     } 
    }); 
    viewHolder.ivMinus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(count!=0) { 
       Child modelChild = groups.get(groupPosition).getChildrens().get(childPosition); 

       if(modelChild.getCount()>0) // set your count default 0 when you bind data initially 
       int count = modelChild.getCount() - 1; 

       modelChild.setCount(count); 
       modelChild.setChildName(modelChild.getChildName()); 
       // set your other items if any like above 
       groups.get(groupPosition).getChildrens().set(childPosition, child); 
       notifyDataSetChanged(); 
      } 
     } 
    }); 

    return convertView; 
} 
+0

この行を説明できます。 "child modelChild = groups.get(groupPosition).getChildrens()。get(childPosition);" –

+0

getChildrens()? –

+0

@Abserve Techは、あなたがクリックした特定の位置のあなたの子供のアイテムを取得し、それは機能しましたか? – Nisarg

関連する問題