🌳程序效果
点击listview的item,跳转页面 点击确定则删除。
🌳代码展示
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/MyListView"/>
</LinearLayout>
activity_secondary.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".SecondaryActivity"
android:orientation="vertical">
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/confirm"
android:layout_marginLeft="20dp"
android:text="确定"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:text="取消"
android:textSize="20sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package cn.edu.henu.lab_3_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static android.content.ContentValues.TAG;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public List<String> list;
private int index;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.MyListView);
String[] people={"叶秋","黄少天","喜羊羊","小灰灰","王俊凯"};
list=new ArrayList(Arrays.asList(people));
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(MainActivity.this,SecondaryActivity.class);
Log.d(TAG, "onItemClick: "+i);
Log.d(TAG, "onItemClick: "+l);
index=i;
String info1="删除 "+listView.getItemAtPosition(i).toString()+" 的记录吗";
intent.putExtra("sendinfo",info1);
startActivityForResult(intent,1);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
list.remove(index);
adapter.notifyDataSetChanged();
Log.d(TAG, "onActivityResult: " + list.get(index));
}
break;
default:
break;
}
}
}
SecondaryActivity.java
package cn.edu.henu.lab_3_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondaryActivity extends AppCompatActivity implements View.OnClickListener {
private TextView info;
private Button confirm;
private Button cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
info=findViewById(R.id.info);
confirm=findViewById(R.id.confirm);
cancel=findViewById(R.id.cancel);
Intent intent=SecondaryActivity.this.getIntent();
String info1=intent.getStringExtra("sendinfo");
info.setText(info1);
confirm.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.confirm:
SecondaryActivity.this.setResult(RESULT_OK,SecondaryActivity.this.getIntent());
break;
case R.id.cancel:
SecondaryActivity.this.setResult(RESULT_CANCELED,SecondaryActivity.this.getIntent());
break;
default:
break;
}
SecondaryActivity.this.finish();
}
}
SecondaryActivity.this.setResult(RESULT_CANCELED,SecondaryActivity.this.getIntent()); 原本我是写做 SecondaryActivity.this.setResult(RESULT_CANCELED); 不带intent,因为没有携带数据,而我只需要一个返回的ResultCode就可以了。 正常写没有问题,程序可以跑起来。但是最后功能没有实现。所以最好还是带上intent。
如果要返回数据的话 SecondaryActivity.this.getIntent().putExtra("returninfo","你要返回的数据");
|