0

在我的应用程序中,我有一个隐藏图标的标题,当我单击列表视图时,我有一个带有列表视图的适配器我使用侦听器进入登录屏幕,当登录成功时应该返回列表视图(适配器)并且图标应该得到在标题上可见。我发送的意图如下: http: //pastebin.com/4SKnyjVX

下面是onclick登录按钮

public void onClick(View v) {

    String password = etPassword.getText().toString();

    if(password.equals("guest")){
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result",result);
        setResult(1,returnIntent);     
        finish();
    } else {
        //----
    }

    finish();                  
}

如何使用 onActivityResult 设置标志并在单击侦听器中使用此标志,并使用它使图标可见。任何建议表示赞赏

4

3 回答 3

2

你必须覆盖onActivityResult(int requestCode, int resultCode, Intent resultIntent)

  • requestCode is the integer that you originally passed as the second parameter to startActivityForResult(). You should check this matches your original request, in case you have multiple requests you have to differentiate between.
  • resultCode and resultIntent is the integer and intent you pass to setResult() in your code above.

So your method should look something like:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
    if (requestCode == 1) { // original request
        if (resultCode == 1) { // success
            String result = resultIntent.getStringExtra("result");
            // Login successful, update your model and call adapter.notifyDataSetChanged() or something
        }
    }
}
于 2012-12-04T10:15:29.603 回答
1

在您之前的活动中,您在标题中有带有隐藏图标的列表视图,覆盖函数 onActivityResult() 类似这样 -

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    boolean success = data.getBooleanExtra("result", false);
    if(success) {
        hiddenIcon.setVisibility(View.VISIBLE);
    }
    else {
        showDialog("Error");
   }
}

我已经用这种方式改变了你的 onClick

public void onClick(View v) {

        String password = etPassword.getText().toString();
        Intent returnIntent = new Intent();
        if(password.equals("guest")){
            returnIntent.putExtra("result", true);
            setResult(RESULT_OK, returnIntent);     
        } 
        else {
            returnIntent.putExtra("result", false);
            setResult(RESULT_OK, returnIntent);
        }

        finish();                  
    }
于 2012-12-04T10:10:19.423 回答
0
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        Bundle basket=data.getExtras();
        String s=basket.getString("result");
        //Your Code Here
    }
}

这会有所帮助

于 2012-12-04T10:02:12.573 回答