简体   繁体   English

如何删除ListView中的特定项目?

[英]How do you delete a specific item in a ListView?

In Android Studio, I have a listview on the main page where multiple courses can keep being added. 在Android Studio中,我在主页上有一个列表视图,可以在其中继续添加多个课程。 When I click on a list view course it takes me to a second page. 当我单击列表视图课程时,它将带我到第二页。 On the second page there is a delete button that will delete that specific course I clicked on and take me back to the main page. 在第二页上,有一个删除按钮,它将删除我单击的特定课程,并带我回到主页。

Can someone please help me with the on click listener for the delete button in order for the above procedure to work? 有人可以通过点击侦听器为我提供“删除”按钮来帮助我,以使上述过程正常进行吗?

A ListView takes a list of items and uses that list to show what you see on the screen. ListView获取项目列表,并使用该列表显示您在屏幕上看到的内容。 What you need to do is on the second page, just remove that item from the list and call notifyDataSetChanged() method on the adapter of the ListView. 您需要做的是在第二页上,只需从列表中删除该项目,然后在ListView的适配器上调用notifyDataSetChanged()方法即可。 This will cause the adapter to create all the items again and you won't see that deleted item anymore. 这将导致适配器再次创建所有项目,并且您将不再看到该已删除项目。

EDIT 编辑

Just something to get you started. 只是一些可以帮助您入门的东西。 If you have a class which contains the list of all the courses that are being added, you can simply remove that course on your delete button click. 如果您的班级包含要添加的所有课程的列表,则只需单击“删除”按钮即可删除该课程。

class Courses {
    List<Course> courseList;

    //Your other members and functions

    void removeCourse(Course course) {
        courseList.remove(course);
    }
}

class Course {
    //Some details
}

button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             //Call that remvoeCourse method here
             courses.removeCourse(selectedCourse);
         }
});    

How do you delete a specific item in a ListView? 如何删除ListView中的特定项目?

You can achieve your problem by Share Preference or startActivityForResult . 您可以通过Share PreferencestartActivityForResult解决问题。

Using startActivityForResult 使用startActivityForResult

Start your second activity with startActivityForResult() in your first Activity; 使用第一个Activity中的startActivityForResult()开始第二个活动;

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

and get the result back when you click delete function 单击删除功能并返回结果

Intent intent = new Intent();
intent.putExtra("result",position);
setResult(Activity.RESULT_OK,returnIntent);
finish();

add this in your OnBackButton 将此添加到您的OnBackButton中

Intent intent = new Intent();
setResult(Activity.RESULT_CANCELED, intent);
finish();

and finally get the result in onActivityResult() in First Activity 最后在第一个活动的onActivityResult()中得到结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getIntExtra("position");
           // Do your operation here 
           // delete position you getting here from intent
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}

Using Share Preference 使用共享首选项

Save your position or Id for Listview Item in share preference and call onBackPressed() method in your delete function. 在共享首选项中保存Listview项目的位置或ID,然后在delete函数中调用onBackPressed()方法。 Do these operation in 2 activity. 在2个活动中执行这些操作。

PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("position", position).commit();
onBackPressed();

In your first Activity do operation in your onRestart method 在您的第一个Activity中,使用onRestart方法执行操作

  @Override
    protected void onRestart() {
        super.onRestart();
        int position = PreferenceManager.getDefaultSharedPreferences(this).getInt("position", 0);
        // delete item from arraylist 
        // notify your adapter
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM