gpt4 book ai didi

android - 通过第二个 Activity 从数据库传递 ListView 的项目位置

转载 作者:行者123 更新时间:2023-11-30 02:23:08 26 4
gpt4 key购买 nike

嗨,我创建了一本日记,人们可以在其中保存他们的笔记。我有 3 个 Activity ,比方说 A、B 和 C Activity 。现在我在 ListView 中保存 Activity A 中编辑的注释,当单击项目位置时,它会转到 Activity B,向我显示我在编辑框中输入的 TextView 表单。现在在 Activity B 中向我显示 TextView 注释以及一个编辑按钮,我可以在其中编辑或更新 Activity C 中的相同数据。所以我希望当我单击 Activity B 中的编辑/更新按钮时,它会传递列表的项目位置保存在 Activity A 中。简而言之,我想通过 Activity B 将项目位置从 A 传递到 C。我可以成功地从 A 传递到 B,但不能从 B 传递到 C。请提供代码,这样对我来说很容易。这是一些代码。

Activity A

@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 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(ActivityA.this, ActivityB.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
i.putExtra("position", position);
startActivity(i);
}



}

这是 Activity B

    @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);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btedit:
Bundle extras = getIntent().getExtras();
int position = extras.getInt("position");
Intent i = new Intent(ActivityB.this, ActivityC.class);
i.putExtra("position", position);
startActivity(i);
break;
}
}

这是 Activity C

    data = new AccessModel(this);

name = (EditText) findViewById(R.id.mn_name);
age = (EditText) findViewById(R.id.mn_age);
gender = (EditText) findViewById(R.id.mn_gender);
height = (EditText) findViewById(R.id.mn_height);
weight = (EditText) findViewById(R.id.mn_weight);
dateofbirth = (EditText) findViewById(R.id.mn_dob);

btnupdate = (Button) findViewById(R.id.btupdate);
btndelete = (Button) findViewById(R.id.btdelete);

btnupdate.setOnClickListener(this);
btndelete.setOnClickListener(this);

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Bundle extras = getIntent().getExtras();
if(extras==null){
back();
return;
}

userId = extras.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());
}


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());

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, ActivityA.class);
startActivity(i);
}

}

最佳答案

这很简单。

在Activity B中右传字符串如

i.putExtra("message", userId);

在 Activity C 中,得到这个

userId  = extras.getString("message");

关于android - 通过第二个 Activity 从数据库传递 ListView 的项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com