如何在 Android 中从 ListView 的链接列表中删除元素?
androidmobile developmentapps/applications
此示例演示了如何在 Android 中从 ListView 的链接列表中删除元素
步骤 1 − 在 Android Studio 中创建一个新项目,转到"文件"->"新建项目",并填写所有必需的详细信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayoutxmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical"> <EditText android:id = "@+id/name" android:layout_width = "match_parent" android:hint = "Enter Name" android:layout_height = "wrap_content" /> <LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content"> <Button android:id = "@+id/save" android:text = "Save" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> <Button android:id = "@+id/refresh" android:text = "Refresh" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> <Button android:id = "@+id/delete" android:text = "Delete" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </LinearLayout> <ListView android:id = "@+id/listView" android:layout_width = "match_parent" android:layout_height = "wrap_content"> </ListView> </LinearLayout>
在上面的代码中,我们将名称作为编辑文本,当用户点击"保存"按钮时,它会将数据存储到arraylist中。点击"删除"按钮即可从列表视图中删除该记录。
步骤 3 − 将以下代码添加到 src/MainActivity.java
package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;
public class MainActivityextends AppCompatActivity {
Button save, refresh;
EditTextname;
ArrayAdapterarrayAdapter;
LinkedList<String>link_list;
private ListViewlistView;
@Override
protected void onCreate(Bundle readdInstanceState) {
super.onCreate(readdInstanceState);
setContentView(R.layout.activity_main);
link_list = new LinkedList<String>();
name = findViewById(R.id.name);
listView = findViewById(R.id.listView);
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
listView.refreshDrawableState();
}
});
findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (link_list.size() >0) {
if (!name.getText().toString().isEmpty()) {
link_list.remove(name.getText().toString());
arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, link_list);
listView.setAdapter(arrayAdapter);
Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "There is no element to delete", Toast.LENGTH_LONG).show();
}
}
});
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
link_list.add(name.getText().toString());
arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, link_list);
listView.setAdapter(arrayAdapter);
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
} else {
name.setError("Enter NAME");
}
}
});
}
}
让我们尝试运行您的应用程序。假设您已将实际的 Android 移动设备连接到电脑。要从 Android Studio 运行该应用程序,请打开项目的某个 Activity 文件,然后点击工具栏上的"运行"图标
。选择您的移动设备作为选项,然后检查您的移动设备是否显示您的默认屏幕 –

在上面的结果中,我们将名称插入到链接列表中,并在列表视图中显示名称。

现在,我们使用的是同一个元素通过点击删除按钮从 listivew 中删除。
点击此处下载项目代码

