【问题标题】:Android - Retrieve Selected Items in a CheckBoxAndroid - 检索复选框中的选定项目
【发布时间】:2017-08-23 09:24:59
【问题描述】:

我正在尝试从要添加到用户列表的列表中检索所选项目。

用户将在不同的活动中选择要添加给用户的项目,然后返回到主活动,新添加的项目将添加到列表中。

添加的功能应该在用户点击活动中呈现的浮动按钮时从列表中选择多个或单个项目时完成。

活动

    public class ProjectSelectionActivity extends AppCompatActivity {
    private ArrayList<Project> Projects;
    private Player Player;
    private ProjectSelectionAdapter Adapter;
    private ListView ProjectSelectionLV;
    private FloatingActionButton AddProject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_selection);


        AddProject = (FloatingActionButton) findViewById(R.id.ProjectSelectionFloatingActionButton);

        Player = (Player)getIntent().getSerializableExtra("Player");
        Projects = (ArrayList<Project>) getIntent().getSerializableExtra("Projects");
        Adapter = new ProjectSelectionAdapter(getApplicationContext(), Projects);

        ProjectSelectionLV = (ListView)findViewById(R.id.ProjectSelectionListView);
        ProjectSelectionLV.setAdapter(Adapter);

        AddProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
 }

适配器:

    public class ProjectSelectionAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Project> projects;
    private LayoutInflater inflator;
    private View view;

    public ProjectSelectionAdapter(Context context, ArrayList<Project> projects) {
        this.context = context;
        this.projects = projects;
    }

    @Override
    public int getCount() {
        return projects.size();
    }

    @Override
    public Object getItem(int position) {
        return projects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        inflator = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            view = new View(context);
            view = inflator.inflate(R.layout.project_selection_view, null);

            TextView Title = (TextView)view.findViewById(R.id.ProjectSelectionTitleTextView);
            TextView Value = (TextView)view.findViewById(R.id.ProjectSelectionValueTextView);
            TextView Revenue = (TextView)view.findViewById(R.id.ProjectSelectionRevenueTextView);
            ImageView Image = (ImageView)view.findViewById(R.id.ProjectSelectionImage);

            Title.setText(projects.get(position).getTitle());
            Value.setText("التكلفة: " + Integer.toString(projects.get(position).getValue()));
            Revenue.setText("الربح: " + Integer.toString(projects.get(position).getRevenue()));
            Image.setImageResource(projects.get(position).getImage());
        }
        return view;
    }
}

项目视图

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp">

    <TextView
        android:id="@+id/ProjectSelectionTitleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/ProjectSelectionImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/ProjectSelectionImage"
        android:layout_width="100sp"
        android:layout_height="100sp"
        app:srcCompat="@drawable/farm_fruit"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/ProjectSelectionCheckbox"
        android:layout_toStartOf="@+id/ProjectSelectionCheckbox" />

    <TextView
        android:id="@+id/ProjectSelectionValueTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Value"
        android:layout_marginBottom="24dp"
        android:layout_above="@+id/ProjectSelectionRevenueTextView"
        android:layout_toLeftOf="@+id/ProjectSelectionImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/ProjectSelectionRevenueTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Revenue"
        android:layout_alignBottom="@+id/ProjectSelectionImage"
        android:layout_toLeftOf="@+id/ProjectSelectionImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <CheckBox
        android:id="@+id/ProjectSelectionCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ProjectSelectionValueTextView"
        android:layout_alignBottom="@+id/ProjectSelectionValueTextView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

【问题讨论】:

    标签: android


    【解决方案1】:
    1. 创建另一个Arraylist,它将在您的活动中存储所有选定的项目。

      private ArrayList<Project> selectedProjects;
      

    2.您可以像这样在 Listview 上添加 onItemClickListener() -

    ProjectSelectionLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   //inside this check if your checkbox corresponding to this item is selected or not. You can do this by taking checkbox from **parent** which you are getting in this method and checking if this checkbox is selected or not. 
                  //If the checkbox is selected, then add the project of this position to your selectedProjects ArrayList -
                  //selectedProjects.add(Projects.get(position));
                  //Make sure to check that selectedProjects does not contain this already to avoid duplicates
                }
            });
    
    1. 当用户单击浮动按钮时,在其 onClick() 方法中,您可以使用 intent extras 传递 selectedProjects ArrayList。请参阅此link 了解如何在活动之间传递对象。

    2. 最后,在您的其他活动中,使用selectedProjects Arraylist 添加新项目。从selectedProjects 添加新项目后,请致电notifyDataSetChanged()

    EDIT - 要在第二步中获得复选框,您可以这样做 -

    RelativeLayout rl = (RelativeLayout) parent.getAdapter().getItem(position);
    CheckBox cb = (CheckBox) rl.getChildAt(4);
    

    【讨论】:

    • 太棒了!还有一个问题,如何从传递给 onItemClickListener 的视图中访问复选框值?
    • 我试过 if( ((CheckBox)view.findViewById(R.id.ProjectSelectionCheckbox)).isChecked() ){ SelectedProjects.add(Projects.get(position)); }
    • 我认为你必须使用父参数。让我编辑一下
    • 我在 SelectedProject 列表中收到 NullException。我已将代码添加到 Listview 侦听器 RelativeLayout rl = (RelativeLayout) parent.getAdapter().getItem(position); CheckBox cb = (CheckBox) rl.getChildAt(position); if(cb.isChecked()){ if(!SelectedProjects.contains(Projects.get(position))){ SelectedProjects.add(Projects.get(position)); } }
    • 使用前必须先初始化列表 - selectedProjects = new ArrayList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多