【发布时间】: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