我正在尝试从列表中检索所选项目以添加到用户列表中。
用户将选择将在不同活动中添加到用户的项目,然后返回主活动,并将新添加的项目添加到列表中。
当用户点击活动中显示的浮动按钮时,用户从列表中选择多个或单个项目时,应该完成添加功能。
活动
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>
答案 0 :(得分:0)
制作另一个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
}
});
当用户单击浮动按钮时,在其onClick()方法中,您可以使用selectedProjects
传递intent extras
ArrayList。请参阅此link以了解如何在活动之间传递对象。
最后,在您的其他活动中,使用selectedProjects
Arraylist添加新项目。从notifyDataSetChanged()
添加新项目后,请致电selectedProjects
。
编辑 - 要获取第2步中的复选框,您可以执行 -
RelativeLayout rl = (RelativeLayout) parent.getAdapter().getItem(position);
CheckBox cb = (CheckBox) rl.getChildAt(4);