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>