startActivityForResult

startActivityForResult

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private Button button_add;
private GridView gv;
List<Map<String, String>> list = new ArrayList<Map<String, String>>(); @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == 1) {
// 获取map集合
final HashMap<String, String> map = (HashMap<String, String>) data
.getSerializableExtra("map"); // 将map集合添加到list集合中 list.add(map); // 为gridview设置适配器
final SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.item, new String[] { "name", "author" },
new int[] { R.id.item_name, R.id.item_author });
gv.setAdapter(adapter); // 为gv设置条目点击监听事件
gv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) { gv.setSelector(android.R.color.holo_red_light); new AlertDialog.Builder(MainActivity.this)
.setTitle("确认删除")
.setMessage("确认删除吗?")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(
DialogInterface dialog,
int which) {
Toast.makeText(MainActivity.this,
"您放弃了删除", 0).show(); }
}).setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,
"删除成功", 0).show();
list.remove(position);
adapter.notifyDataSetChanged();
}
}).show(); }
}); } } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 寻找控件
findView(); // 为添加按钮设置点击监听事件
setClick();
} private void setClick() { button_add.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { // 回调跳转到添加界面
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivityForResult(intent, 1); }
}); } private void findView() { button_add = (Button) findViewById(R.id.button_add);
gv = (GridView) findViewById(R.id.gv);
TextView tv = (TextView) findViewById(R.id.tv_manager); } }
startActivityForResult 页面跳转回调-LMLPHP
startActivityForResult 页面跳转回调-LMLPHP
import java.util.HashMap;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class AddActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add); //找到该布局文件下的控件
final EditText edit_name=(EditText) findViewById(R.id.edit_name);
final EditText edit_author=(EditText) findViewById(R.id.edit_author);
Button button_a=(Button) findViewById(R.id.button_a); //点击添加按钮实现监听事件
button_a.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { //获取输入框中的信息
String name = edit_name.getText().toString();
String author = edit_author.getText().toString(); if(TextUtils.isEmpty(name))
{
Toast.makeText(AddActivity.this, "图书名字不能为空", 0).show();
return;
} if(TextUtils.isEmpty(author))
{
Toast.makeText(AddActivity.this, "作者不能为空", 0).show();
return;
} //将页面信息封存为map集合
HashMap<String, String> map=new HashMap<String, String>();
map.put("name", name);
map.put("author", author); //将map集合存放在intent中并回调到主界面
Intent intent = getIntent();
intent.putExtra("map", map);
setResult(1, intent); //将本界面关闭
finish(); }
});
} }
04-30 05:52