【问题标题】:Setting ImageView at certain positions in a ListView based on CheckBox基于CheckBox在ListView的特定位置设置ImageView
【发布时间】:2016-12-06 02:05:57
【问题描述】:

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

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

这是我的主要活动与列表视图和启动第二个复选框活动的点击侦听器

        //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);
            }
}

然后回到我的列表视图活动中,我从我的复选框活动中接收信息并设置复选标记

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

        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;
    }

感谢您的帮助!

【问题讨论】:

  • 根据该实体创建一个具有两个属性(Say name 和 checked:boolean)的实体(sya Route)和一个您的自定义适配器。在适配器的 getView 方法中实现逻辑,以根据“选中”值显示和隐藏复选框。
  • 无论如何你可以发布一个带有代码的示例吗?我真的是编程新手,视觉上会容易得多。
  • 请从“我从我的复选框活动中接收信息并设置复选标记”的活动中粘贴更多内容
  • 嘿,我编辑了我的原始帖子以包含需要设置复选标记的完整适配器类。
  • 嘿,我解决了我的问题,这非常简单,我之前怎么没注意到

标签: java android listview checkbox


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多