Android:从列表中添加和删除项目到Listview

时间:2017-08-22 09:24:02

标签: android listview

我正在为棋盘游戏构建计算器应用程序。 该应用程序包含播放器可用的所有土地的列表,应用程序应该能够添加和删除播放器列表中的项目。

旁注:应用程序应根据玩家掌握的土地计算玩家的利润。

Player View Lands View 当用户点击添加按钮并在点击删除按钮时从播放器中移除平台时,我希望能够从界面列表中向玩家添加平台。基于每个列表中的选定项目。

注意:我使用相同的适配器列出播放器和平台视图中的项目

public class DetailsActivity extends AppCompatActivity {

private ListView ProjectsLV;
private ProjectAdapter Adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton add_fab = (FloatingActionButton) findViewById(R.id.add_fab);
    add_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Add Project", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    FloatingActionButton remove_fab = (FloatingActionButton) findViewById(R.id.remove_fab);
    remove_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Remove Projects", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Player Player = (Player)getIntent().getSerializableExtra("Player");

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

    ProjectsLV = (ListView)findViewById(R.id.ProjectsListView);
    ProjectsLV.setAdapter(Adapter);
}

}

2 个答案:

答案 0 :(得分:0)

我添加了一些我自己的风格如何做到这一点,你可以按照相同的方式来实现你想要的,但我会详细解释发生了什么。 首先我创建了一个字段DataTemplate,所以我可以在类中的任何地方访问它,在添加或删除此列表中的项目之后,您只需要调用适配器上的<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="wpf.dll.xaml"/> <ResourceDictionary Source="B.exe.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> ,这将更新您拥有的列表视图。我希望这很好。

projectList

答案 1 :(得分:0)

更新了课程

public class DetailsActivity extends AppCompatActivity {

private ListView ProjectsLV;
private ProjectAdapter Adapter;
private ArrayList<Project> PlayerProjectList;
private Project selectedProject;
private ArrayList<Project> Projects;
private Player Player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton add_fab = (FloatingActionButton) findViewById(R.id.add_fab);
    FloatingActionButton remove_fab = (FloatingActionButton) findViewById(R.id.remove_fab);

    Player = (Player)getIntent().getSerializableExtra("Player");

    Projects = (ArrayList<Project>) getIntent().getSerializableExtra("Projects");
    PlayerProjectList = Player.getProjects();

    Adapter = new ProjectAdapter(getApplicationContext(), PlayerProjectList);

    ProjectsLV = (ListView)findViewById(R.id.ProjectsListView);
    ProjectsLV.setAdapter(Adapter);

    ProjectsLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedProject =(Project) (ProjectsLV.getItemAtPosition(position));
        }
    });

    add_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Add Project", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            // create the new project and call the addProjectToList method;
            PlayerProjectList = addProjectToList(PlayerProjectList, selectedProject);
            Adapter.notifyDataSetChanged();
        }
    });

    remove_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Remove Projects", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            // choose the project to remove and call the removeProjectFromList method;
            removeProjectFromList(PlayerProjectList, selectedProject);
            Adapter.notifyDataSetChanged();
        }
    });
}

private ArrayList<Project> addProjectToList(ArrayList<Project> projectsList,Project project){
    projectsList.add(project);
    return projectsList;
}
private ArrayList<Project> removeProjectFromList(ArrayList<Project> projectsList,Project project){
    projectsList.remove(project);
    return projectsList;
}

}