【发布时间】:2016-02-09 09:37:28
【问题描述】:
我有一个问题,我想点击列表,调用一个新的活动并将按钮重命名为另一个名称。
我尝试了几件事,但没有成功,有人可以帮助我吗?
我的班级EditarTimes:
私有 AdapterView.OnItemClickListener 选择时间 = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
t = times.get(pos);
地籍时间 cad = new CadastroTimes();
CadastroTimes.salvar.setText("Alterar");
Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
开始活动(意图);
}
};
公共类 CadastroTimes 扩展 AppCompatActivity {
私人时间 t;
私人时间数据库;
私人 EditText edID;
私人 EditText edNome;
公共按钮萨尔瓦;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastro_times);
edID = (EditText) findViewById(R.id.edID);
edNome = (EditText) findViewById(R.id.edNome);
db = new timeDatabase(getApplicationContext());
萨尔瓦=(按钮)findViewById(R.id.btnCadastrar);
salvar.setText("地籍");
字符串新字符串;
if (savedInstanceState == null) {
捆绑附加服务 = getIntent().getExtras();
如果(额外==空){
新字符串=空;
} 别的 {
newString= extras.getString("Alterar");
}
} 别的 {
newString= (String) savedInstanceState.getSerializable("Alterar");
}
//CadastroTimes 活动中的按钮以将该字符串作为文本
System.out.println(newString + " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
salvar.setText(newString);
}
公共无效salvarTime(查看v){
t = 新时间();
t.setNome(edNome.getText().toString());
if (salvar.getText().equals("Alterar")) {
db.atualizar(t);
exibirMensagem("时间 atualizado com sucesso!");
} 别的 {
db.salvar(t);
exibirMensagem("时间地籍 com sucesso!");
}
Intent intent = new Intent(this, EditarTimes.class);
开始活动(意图);
}
私人无效limparDados(){
edID.setText("");
edNome.setText("");
edNome.requestFocus();
}
私人无效exibirMensagem(字符串味精){
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
公共类 EditarTimes 扩展 AppCompatActivity {
私人时间 t;
私人名单时间;
私人时间数据库;
私有 ListView lvTimes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editar_times);
lvTimes = (ListView) findViewById(R.id.lvTimes);
lvTimes.setOnItemClickListener(selecinarTime);
lvTimes.setOnItemLongClickListener(excluirTime);
次 = 新的 ArrayList();
db = new timeDatabase(getApplicationContext());
atualizarLista();
}
私人无效excluirTime(最终int idTime){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("排除时间?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Deseja 排除时间?")
.setCancelable(假)
.setPositiveButton(getString(R.string.sim),
新的 DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
如果(db.deletar(idTime)){
atualizarLista();
exibirMensagem(getString(R.string.msgExclusao));
} 别的 {
exibirMensagem(getString(R.string.msgFalhaExclusao));
}
}
})
.setNegativeButton(getString(R.string.nao),
新的 DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
对话框.取消();
}
});
builder.create();
builder.show();
atualizarLista();
}
私人无效 atualizarLista() {
次 = db.listAll();
如果(次!= null){
如果 (times.size() > 0) {
TimeListAdapter tla = 新的 TimeListAdapter(
getApplicationContext(), 次);
lvTimes.setAdapter(tla);
}
}
}
私有 AdapterView.OnItemClickListener 选择时间 = 新 AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
t = times.get(pos);
Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
String strName = "Alterar";
intent.putExtra("Alterar", strName);
开始活动(意图);
//preecherDados(t);
}
};
私有 AdapterView.OnItemLongClickListener excluirTime = new AdapterView.OnItemLongClickListener() {
公共布尔 onItemLongClick(AdapterView arg0,查看 arg1,
int pos, long arg3) {
excluirTime(times.get(pos).getId());
返回真;
}
};
/*private void preecherDados(Time time) {
edID.setText(String.valueOf(time.getId()));
edNome.setText(time.getNome());
}*/
私人无效exibirMensagem(字符串味精){
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
公共无效 telaCadastrar(查看视图){
Intent intent = new Intent(this, CadastroTimes.class);
开始活动(意图);
}
public void botaoSair(查看视图) {
Intent intent = new Intent(this, TelaInicial.class);
开始活动(意图);
}
}
【问题讨论】:
-
如果按钮在
CadastroTimes中,则在CadastroTimes中而不是在EditarTimes中 -
按钮在 CadastroTimes.class 中,一切正常,我只需要点击一个列表项,调用活动 CadastroTimes 并更改按钮名称。
标签: java android android-intent settext