<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="@color/design_default_color_secondary_variant"
android:dividerHeight="5dp"/>
LinearLayout>
(2)MainActivity.java
package com.example.lab_3_1;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<String >list=new ArrayList<>();
private ArrayAdapter adapter;
private String temp=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listView);
list.add("张三");
list.add("李四");
list.add("王二");
list.add("赵五");
adapter=new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
temp=listView.getItemAtPosition(i).toString();
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("info",temp);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1: if(resultCode==2)
adapter.remove(temp);
break;
}
}
}
(3)SecondActivity.java
package com.example.lab_3_1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
private String info=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
info=intent.getStringExtra("info");
Dialog alertDialog=new AlertDialog.Builder(this).setIcon(R.drawable.jihe).setTitle("信息如下:").setMessage("删除 "+info+" 的记录吗?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new Intent();
setResult(2,intent);
finish();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
}).create();
alertDialog.show();
}
}
Icon:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)