我有两项活动:主要和编辑。当我点击列表视图中的某个项目时,会将我带到编辑活动,在其中显示各自文本视图中的项目详细信息(姓名,姓氏,单元格和电子邮件)。当我单击编辑按钮时,文本视图变得可编辑,当我单击保存按钮时,我需要更新主活动中的列表视图。请提供代码,因为我还是初学者。
我的主要课程的代码
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> list = new ArrayList<>();
Button btnAdd = (Button) findViewById(R.id.btnAdd);
ListView lstInfo = (ListView) findViewById(R.id.lstInfo);
Button btnName = (Button) findViewById(R.id.btnName);
Button btnSurname = (Button) findViewById(R.id.btnSurname
);
Button btnEmail = (Button) findViewById(R.id.btnEmail);
Button btnDelete = (Button) findViewById(R.id.btnDelete);
final List<Contact> items = new ArrayList<Contact>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(MainActivity.this, "Select a contact to delete", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
// get views
// Button btnAdd = (Button) findViewById(R.id.btnAdd);
// ListView lstInfo = (ListView) findViewById(R.id.lstInfo);
// Button btnName = (Button) findViewById(R.id.btnName);
// Button btnSurname = (Button) findViewById(R.id.btnSurname);
// Button btnEmail = (Button) findViewById(R.id.btnEmail);
// Button btnDelete = (Button) findViewById(R.id.btnDelete);
//
// final List<Contact> items = new ArrayList<Contact>();
//Hard-coding contacts
items.add(new Contact("Sally", "Van Der Walt", "0766838275", "sally@gmail.com"));
items.add(new Contact("Sarah", "Smith", "0847852693", "sarah@gmail.com"));
items.add(new Contact("John", "Doe", "0823691478", "john@gmail.com"));
// create adapter to transform string items
final ContactAdapter adapter = new ContactAdapter(this, items);
//final ContactAdapter conAdapter = new ContactAdapter(this, items);
// attach adapter to list view
lstInfo.setAdapter(adapter);
// add more items when clicked
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
adapter.add(new Contact("Name", "Surname", "Cell", "Email"));
Toast.makeText(MainActivity.this, "New contact has been added", Toast.LENGTH_SHORT).show();
}
});
//Delete the selected user - the selected user will appear in green
lstInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (list.size() >= 1)
for (int i = 0; i < list.size(); i++) {
if (position == list.get(i))
return;
}
Contact cur = items.get(position);
list.add(position);
cur.setSelected("(Selected)");
adapter.notifyDataSetChanged();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
items.remove(list.get(i).intValue());
}
adapter.notifyDataSetChanged();
list.clear();
Toast.makeText(MainActivity.this, "Contact has been deleted", Toast.LENGTH_SHORT).show();
}
});
//
// btnDelete.setOnItemClickListener(new Adapter.OnItemClickListener() {
// @Override public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
// list.remove(position);
// }
// });
//
//Sorting using the adapters sort method
btnName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact left, Contact right) {
return left.firstName.compareTo(right.firstName);
}
});
}
});
btnSurname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact left, Contact right) {
return left.surname.compareTo(right.surname);
}
});
}
});
btnEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact left, Contact right) {
return left.email.compareTo(right.email);
}
});
}
});
lstInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String nameItem = items.get(position).getFirstName();
String surnameItem = items.get(position).getSurname();
String cellItem = items.get(position).getCellNumber();
String emailItem = items.get(position).getEmail();
Intent intent = new Intent(MainActivity.this, EditingActivity.class);
intent.putExtra("name", nameItem);
intent.putExtra("surname", surnameItem);
intent.putExtra("cell", cellItem);
intent.putExtra("email", emailItem);
//startActivity(intent);
startActivityForResult(intent, 2);
}
});
}
**Code for my editing class:**
public class EditingActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.edit_contact);
Intent intent = getIntent();
if (intent != null)
{
Bundle extras = intent.getExtras();
if (extras != null)
{
//Get the bundled values
String name = extras.getString("name");
String surname = extras.getString("surname");
String cell = extras.getString("cell");
String email = extras.getString("email");
//Display the values
TextView txtName = (TextView) findViewById(R.id.nameTV);
TextView txtSurname = (TextView) findViewById(R.id.surnameTV);
TextView txtCell = (TextView) findViewById(R.id.cellTV);
TextView txtEmail = (TextView) findViewById(R.id.emailTV);
txtName.setText(name);
txtSurname.setText(surname);
txtCell.setText(cell);
txtEmail.setText(email);
}
}
Button editBtn = (Button) findViewById(R.id.editBtn);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView txtName = (TextView) findViewById(R.id.nameTV);
TextView txtSurname = (TextView) findViewById(R.id.surnameTV);
TextView txtCell = (TextView) findViewById(R.id.cellTV);
TextView txtEmail = (TextView) findViewById(R.id.emailTV);
txtName.setEnabled(true);
txtSurname.setEnabled(true);
txtCell.setEnabled(true);
txtEmail.setEnabled(true);
}
});
Button saveBtn = (Button) findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
答案 0 :(得分:0)
您可以使用startActivityForResult()
检查以下代码以供参考 -
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
onActivityResult
可用于检查已编辑数据的任何更新。
在编辑课内,你必须这样做 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}
为了进一步了解startActivityForResult()
,您可以参考此Java Point Link。
如果您需要更多帮助,请与我们联系。
答案 1 :(得分:0)
lstInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) {
String nameItem = items.get(position).getFirstName();
String surnameItem = items.get(position).getSurname();
String cellItem = items.get(position).getCellNumber();
String emailItem = items.get(position).getEmail();
Intent intent = new Intent(MainActivity.this, EditingActivity.class);
intent.putExtra("name", nameItem);
intent.putExtra("surname", surnameItem);
intent.putExtra("cell", cellItem);
intent.putExtra("email", emailItem);
startActivity(intent);
}
});
首先直接将项目对象替换为上面的代码
lstInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, EditingActivity.class);
intent.putExtra("ItemDetails", items.get(position));
startActivityForResult(intent,REQUEST_CODE);
}
});
使用
访问对象ItemObject details= getIntent().getSerializableExtra("ItemDetails");
在编辑活动中修改保存按钮上的代码
Button saveBtn = (Button) findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("ItemDetail",updateItems);
setResult(REQUEST_CODE,intent);
}
});