您好我创建了一个存储一些编辑文本的数据库。我有三个活动A,B和C.活动A显示列表视图中存储的值。单击活动A中的列表视图项时,它会将其位置和存储的数据传递给活动B,其中活动B将其显示为简单文本,无法编辑。现在我想将这些数据从活动B传递到活动C,在那里它可以编辑。如何通过Acitvity B将列表视图位置从A传递到C.
活动A
public class UserActivity extends Activity implements OnItemClickListener {
private ListAdapter users;
private ListView lista;
private AccessModel data;
private ArrayList<User> query;
private LinearLayout empty_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.listusers);
lista = (ListView) findViewById(R.id.lstusers);
empty_data= (LinearLayout) findViewById(R.id.empty_data);
data = new AccessModel(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
query = data.listUser();
if(query.size()>0)
empty_data.setVisibility(View.GONE);
else
empty_data.setVisibility(View.VISIBLE);
users = new ListAdapter(this, query);
lista.setAdapter(users);
lista.setOnItemClickListener(this);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.optionsbar, menu);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.adduser:
Intent i = new Intent(this, RegisterUser.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
User u = (User) query.get(position);
Intent i = new Intent(UserActivity.this, ViewPatientData.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
startActivity(i);
}
}
活动B
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.view_patient_data);
getActionBar().setDisplayHomeAsUpEnabled(true);
data = new AccessModel(this);
name = (TextView) findViewById(R.id.tv_name);
age = (TextView) findViewById(R.id.tv_age);
gender = (TextView) findViewById(R.id.tv_gender);
height = (TextView) findViewById(R.id.tv_height);
weight = (TextView) findViewById(R.id.tv_weight);
dateofbirth = (TextView) findViewById(R.id.tv_dateofbirth);
religion = (TextView) findViewById(R.id.tv_religion);
maritalstatus = (TextView) findViewById(R.id.tv_maritalstatus);
dateofadmission = (TextView) findViewById(R.id.tv_dateofadmission);
address = (TextView) findViewById(R.id.tv_address);
contact = (TextView) findViewById(R.id.tv_contact);
emergencycontact = (TextView) findViewById(R.id.tv_emergencycontact);
email = (TextView) findViewById(R.id.tv_email);
occupation = (TextView) findViewById(R.id.tv_occupation);
bloodtype = (TextView) findViewById(R.id.tv_bloodtype);
refferedvia = (TextView) findViewById(R.id.tv_refferedvia);
btnedit = (Button) findViewById(R.id.btedit);
btnedit.setOnClickListener(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Bundle v = getIntent().getExtras();
if(v==null){
back();
return;
}
userId = v.getString(getString(R.string.valuesId));
loadData(userId);
}
private void loadData(String id){
User u = (User) data.getUser(id);
Toast.makeText(this, u.getName(), Toast.LENGTH_LONG).show();
name.setText(u.getName().toString());
age.setText(u.getAge());
gender.setText(u.getGender());
height.setText(u.getHeight());
weight.setText(u.getWeight());
dateofbirth.setText(u.getDateofbirth());
religion.setText(u.getReligion());
maritalstatus.setText(u.getMaritalstatus());
dateofadmission.setText(u.getDateofadmission());
contact.setText(u.getContact());
emergencycontact.setText(u.getEmergencycontact());
email.setText(u.getEmail());
occupation.setText(u.getOccupation());
bloodtype.setText(u.getBloodtype());
refferedvia.setText(u.getRefferedvia());
address.setText(u.getAddress());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btedit:
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
back();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void back(){
Intent i = new Intent(this, UserActivity.class);
startActivity(i);
}
}
活动C
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Bundle v = getIntent().getExtras();
if(v==null){
back();
return;
}
userId = v.getString(getString(R.string.valuesId));
loadData(userId);
}
private void loadData(String id){
User u = (User) data.getUser(id);
Toast.makeText(this, u.getName(), Toast.LENGTH_LONG).show();
name.setText(u.getName().toString());
age.setText(u.getAge());
gender.setText(u.getGender());
height.setText(u.getHeight());
weight.setText(u.getWeight());
dateofbirth.setText(u.getDateofbirth());
religion.setText(u.getReligion());
maritalstatus.setText(u.getMaritalstatus());
dateofadmission.setText(u.getDateofadmission());
contact.setText(u.getContact());
emergencycontact.setText(u.getEmergencycontact());
email.setText(u.getEmail());
occupation.setText(u.getOccupation());
bloodtype.setText(u.getBloodtype());
refferedvia.setText(u.getRefferedvia());
address.setText(u.getAddress());
}
private void updateUser(String id){
String[] ids = {id};
try{
data.updateUser(ids,name.getText().toString(), age.getText().toString(), gender.getText().toString(), height.getText().toString(), weight.getText().toString(), dateofbirth.getText().toString()
,religion.getText().toString(), maritalstatus.getText().toString(), dateofadmission.getText().toString(), address.getText().toString(), contact.getText().toString(), emergencycontact.getText().toString()
,email.getText().toString(), occupation.getText().toString(), bloodtype.getText().toString(), refferedvia.getText().toString());
Toast.makeText(this, getString(R.string.good), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
}
}
private void deleteUser(String id){
String[] ids = {id};
try{
data.deleteUser(ids);
Toast.makeText(this, getString(R.string.good), Toast.LENGTH_LONG).show();
back();
}catch(Exception e){
Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btupdate:
updateUser(userId);
break;
case R.id.btdelete:
deleteUser(userId);
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
back();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void back(){
Intent i = new Intent(this, UserActivity.class);
startActivity(i);
}
}
答案 0 :(得分:2)
你已经得到了答案。它与您在活动B中添加到意图中的id非常相似。
在A中,执行以下操作:
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
i.putExtra("position", position);
在B中,将其解压缩为:
Bundle extras = getIntent().getExtras();
int position = extras.getInt("position");
Intent i = new Intent(ActivityB.this, ActivityC.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
i.putExtra("position", position);
在C中,像以前一样提取位置:
Bundle extras = getIntent().getExtras();
int position = extras.getInt("position");
答案 1 :(得分:0)
您必须从变量中的活动B中的intent bundle中检索值,然后将该值传递到活动C的新意图包中