ChildClick Listner上的Android可扩展列表视图不起作用



我有一个ExpandableListView列表工作得很好,但是当我单击Childs时,我想在Clock a时钟图标更改颜色并做某事并保存下来,但是当我单击孩子时,第一个时钟时第一个孩子做这项工作,但是当我向下滚动一个随机的孩子或其中2 O时,其中3也会更改颜色,如果我关闭应用程序OS我的适配器代码

,它将保存下来
public class exTourListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<GroupTour> groups;
private int count;
SharedPreferences preferences;
AppCompatTextView timeTour ,tvPrize ,tvGameMode,tvBuyIn,tvLateReg
,tvReBuy,tvDays;
RelativeLayout childToursLayout;

Clock clock ;
public exTourListAdapter(Context context , ArrayList<GroupTour> groups,int count) {
    this.context = context;
    this.groups = groups;
    this.count = count;
}
@Override
public ChildTour getChild(int groupPosition, int childPosition) {
    GroupTour group = groups.get(groupPosition);
    exChild exchild = group.getChildren().get(groupPosition);
    ChildTour child = exchild.getChildTours().get(childPosition);
    return child;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
    ChildTour childTour= getChild(groupPosition,childPosition);
    int id= childTour.getTourId();
    return id;
}
@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    ChildTour child =  getChild(groupPosition,
            childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.ex_list_tours_child, null);
    }
    preferences = context.getSharedPreferences("timeonoff",Context.MODE_PRIVATE);
    timeTour = (AppCompatTextView) convertView.findViewById(R.id.time_tour);
    tvPrize = (AppCompatTextView) convertView.findViewById(R.id.tour_prize);
    tvGameMode = (AppCompatTextView) convertView.findViewById(R.id.tour_gamemode);
    tvBuyIn = (AppCompatTextView) convertView.findViewById(R.id.tour_buy_in);
    tvLateReg = (AppCompatTextView) convertView.findViewById(R.id.tour_late_reg);
    tvReBuy = (AppCompatTextView) convertView.findViewById(R.id.tour_rebuy);
    tvDays = (AppCompatTextView) convertView.findViewById(R.id.tour_days);
    clock = (Clock) convertView.findViewById(R.id.clock_layour);
    tvDays.setTag(child.getTourId());
    clock.setHours(child.getHour());
    clock.setMinutes(child.getMin());
    tvLateReg.setText("Late Register: "+child.getLateReg()+" min");
    int id = (int) getChildId(groupPosition,childPosition);
    if (preferences.getBoolean("time"+id,false) == true){
        clock.setClockColor(R.color.material_green_500);
    }else if (preferences.getBoolean("time"+id,true)==false){
        clock.setClockColor(R.color.material_brown_700);
    }
    if (child.getReBuy().equals("")){
        tvReBuy.setText("Rebuy: ?");
    }else {
        tvReBuy.setText("Rebuy: "+child.getReBuy());
    }
    tvDays.setText(child.getDay());
    tvBuyIn.setText("BuyIn: "+child.getBuyIn());
    tvGameMode.setText(child.getGameMode());
    tvPrize.setText(child.getPrize());
    timeTour.setText(child.getTime());
    return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
   int number = groups.get(groupPosition).getChildren().get(groupPosition).getChildNumbers();
    return number;
}
@Override
public GroupTour getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    GroupTour group =  getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.list_header_groups_tours, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.tours_site_name);
    tv.setText(group.getName());
    // TODO Auto-generated method stub
    return convertView;
}
@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}
public void onClockClick(View view){
    clock.setClockColor(R.color.material_red_300);
}

这是我的片段代码,时钟类是时钟图标

public class ListToursexFragment extends Fragment {
SwipeRefreshLayout refresh;
ExpandableListView listView;
SharedPreferences.Editor editor;
SharedPreferences pref;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.activity_tours,container,false);
    pref= getActivity().getSharedPreferences("timeonoff", Context.MODE_PRIVATE);
    editor =pref.edit();
    refresh = (SwipeRefreshLayout) view.findViewById(R.id.refresh_tours);
    listView = (ExpandableListView) view.findViewById(R.id.list_tours);
    refresh.setColorSchemeColors(
            R.color.material_green_200,
            R.color.material_green_400,
            R.color.material_green_600,
            R.color.material_green_800
    );
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            prepareData(url,listView);
        }
    });
    prepareData(url,listView);
    return view;
}
private void prepareData(String url, final ExpandableListView listView) {
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray array = response.getJSONArray("sites");
                int count = response.getInt("sitescount");
                exTourParser parser = new exTourParser();
                final ArrayList<GroupTour> items = parser.Parse(array);
                exTourListAdapter adapter = new exTourListAdapter(getContext(), items,count);
                listView.setAdapter(adapter);
                listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long id) {
                        Clock clock = (Clock) view.findViewById(R.id.clock_layour);
                        boolean b= pref.getBoolean("time"+id,false);
                        if (b ==true){
                            editor.putBoolean("time"+id,false);
                            editor.commit();
                            clock.setClockColor(R.color.material_brown_700);
                        }else if (b == false){
                            editor.putBoolean("time"+id,true);
                            editor.commit();
                            clock.setClockColor(R.color.material_green_700);
                        }
                        return false;
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(), "ridiok", Toast.LENGTH_SHORT).show();
        }
    });
    RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
    quew.add(request);
}

此屏幕屏幕首次拍摄仅单击第一个孩子,然后滚动几次,然后black是默认颜色

在您在适配器的getView方法中工作时,您正在使用它,如果有条件,请尝试使用" else"。在您的情况下,我可以看到" if"和'else if'

其他

缺少。

if (b ==true)
{
             editor.putBoolean("time"+id,false);
             editor.commit();
             clock.setClockColor(R.color.material_brown_700);
}
else if (b == false)
{
          editor.putBoolean("time"+id,true);
          editor.commit();
          clock.setClockColor(R.color.material_green_700);
}

可以转换为

 if (b)
    {
                 editor.putBoolean("time"+id,false);
                 editor.commit();
                 clock.setClockColor(R.color.material_brown_700);
    }
    else 
    {
              editor.putBoolean("time"+id,true);
              editor.commit();
              clock.setClockColor(R.color.material_green_700);
    }

最新更新