0

I'm trying to retrieve the selected item from a list to be added to the user's List.

The user would select the project that would be added to the user in a different activity then return to main activity with the newly added projects to the list.

The functionality of adding should be done when the user select multiple or single item from the list when the use click on the floating button presented in the activity.

Activity

    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) {

            }
        });
    }
 }

Adapter:

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

Item 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>
Nasser X
  • 175
  • 3
  • 10

1 Answers1

0
  1. Make another Arraylist which will store all the selected projects in your activity.

    private ArrayList<Project> selectedProjects;
    

2.You can add onItemClickListener() on your Listview like this -

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. When user clicks the floating button, inside its onClick() method, you can pass the selectedProjects ArrayList using intent extras. Refer this link to see how to pass objects between activities.

  2. Finally, in your other activity, use selectedProjects Arraylist to add new projects. Call notifyDataSetChanged() when you have added the new projects from selectedProjects.

EDIT - To get the checkbox in 2nd step, you can do -

RelativeLayout rl = (RelativeLayout) parent.getAdapter().getItem(position);
CheckBox cb = (CheckBox) rl.getChildAt(4);
Sachin Aggarwal
  • 1,095
  • 8
  • 17
  • Great! One more question, how can I access the checkbox value from the view passed onItemClickListener? – Nasser X Aug 23 '17 at 10:58
  • I've tried if( ((CheckBox)view.findViewById(R.id.ProjectSelectionCheckbox)).isChecked() ){ SelectedProjects.add(Projects.get(position)); } – Nasser X Aug 23 '17 at 11:14
  • I think you have to use parent argument. Let me make an edit – Sachin Aggarwal Aug 23 '17 at 11:17
  • I'm getting NullException on the SelectedProject list. I've added the code to to the Listview listener 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)); } } – Nasser X Aug 23 '17 at 11:24
  • You have to initialise the list before using it - selectedProjects = new ArrayList<>(); – Sachin Aggarwal Aug 23 '17 at 11:25
  • For testing I'm displaying a toast with the size of the selectedproject and it's showing me 0 – Nasser X Aug 23 '17 at 11:29
  • Yes, but the list of selectedprojects isn't updating with the selected project from the list. – Nasser X Aug 23 '17 at 11:33
  • You will have to debug it as I can't seem to locate the problem :). Try putting log statements inside your if(cb.isChecked()) and other locations. Identify where it's going wrong. I will be able to help you then :) – Sachin Aggarwal Aug 23 '17 at 11:36
  • I've added a toast under the isChecked statement to be fired when the user check on the item but it seems the code is unreachable. – Nasser X Aug 23 '17 at 11:40
  • Great !! Can you add cb.performClick(); before this "if condition". Also, if it doesn't work, try our previous approach where we used view.findViewByID() – Sachin Aggarwal Aug 23 '17 at 11:47
  • No luck with both approaches. – Nasser X Aug 23 '17 at 11:56
  • Ahh !! I guess then you have to change the approach. I did this with recycler view long back and it worked. Try this - http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html It is exactly what you want. If you get stuck anywhere, ping me. Thanks – Sachin Aggarwal Aug 23 '17 at 11:58
  • Step 3 and 4 will remain same :) – Sachin Aggarwal Aug 23 '17 at 11:59
  • Great I was able to implement the checkbox through the tutorial you have submitted but I had to add a condition to solve a IndexOutOfBound at the position. I will post the solution to include the adding of project to the player. – Nasser X Aug 24 '17 at 11:47
  • @NasserX My answer is according to your question. Since your further problems were out of the scope of this question, I followed the discussion in comments. Please accept the answer since it is the answer to your question. You can raise another question for "One more question, how can I access the checkbox value from the view passed onItemClickListener?" and answer it yourself so that it is helpful for the community. Thanks :-) – Sachin Aggarwal Aug 24 '17 at 23:14