基于CheckBox

时间:2016-12-04 21:28:29

标签: java android listview checkbox

所以我有一个列表视图,当你点击一行时,它会打开一个新的活动。在新活动中有一个复选框。如果选中该复选框,然后返回列表视图活动,则应在最初单击的列表视图项旁边设置复选标记。

现在发生的事情是当我选中复选框并返回列表视图时,无论选中哪一行复选框,每一行旁边都有一个复选标记。

继承了我对listview的主要活动以及启动第二个复选框活动的点击监听器

        //fill list view with xml array of routes
    final CharSequence[] routeListViewItems =    getResources().getTextArray(R.array.routeList);


    //custom adapter for list view
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
    final ListView routeListView = (ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(routeAdapter);

    routeListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    int listViewItemPosition = position;
                    CharSequence route = routeListViewItems[position];
                    int imageId = (int) image.getResourceId(position, -1);
                    if (route.equals(routeListViewItems[position]))
                    {
                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", routeDetail[position]);
                        intent.putExtra("imageResourceId", imageId);
                        intent.putExtra("routeName", routeListViewItems[position]);
                        intent.putExtra("listViewItemPosition", listViewItemPosition);
                        startActivity(intent);
                    }
                }
            }
    );
}

接下来是我从第二个活动回到列表视图活动

的内容
 @Override ///////for back button///////
public void onBackPressed() {
    super.onBackPressed();
            ////////sets checkmark next to listview item//////
            if (routeCheckBox.isChecked())
            {
                Intent check = new Intent(RouteDetails.this,MainActivity.class);
                check.putExtra("checkImageResource", R.drawable.checkmark);
                check.putExtra("listViewItemPosition", listViewItemPosition);
                startActivity(check);
            }
}

然后回到我的listview活动中,我从复选框活动中收到信息并设置复选标记

编辑以包含完整的适配器类 编辑,包括我发现的解决我的问题的代码!

        class CustomAdapter extends ArrayAdapter<CharSequence> {

    public CustomAdapter(Context context, CharSequence[] routes) {
        super(context, R.layout.custom_row ,routes);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater routeInflater = LayoutInflater.from(getContext());
        View customView = convertView;
        if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}

        CharSequence singleRoute = getItem(position);
        TextView routeText = (TextView) customView.findViewById(R.id.routeText);
        routeText.setText(singleRoute);

       /////////////set check mark/////////////
        ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);

        int checkImageResourceId = ((Activity) getContext()).getIntent().getIntExtra("checkImageResource",0);

        int listViewItemPosition = ((Activity) getContext()).getIntent().getIntExtra("listViewItemPosition",0);

        /////my solution was just setting where listviewitemposition == position in my getview method//////
        if (listViewItemPosition == position)
        {
        checkImageView.setImageResource(checkImageResourceId);}
        //////////////////////////////////////////////////
  return customView;
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

解决方案是我只需要使用if语句在我的getView方法中设置listviewItemPosition ==位置

  /////my solution was just setting where listviewitemposition == position in my getview method//////
    if (listViewItemPosition == position)
    {
    checkImageView.setImageResource(checkImageResourceId);}
    //////////////////////////////////////////////////