editText值不会更改

时间:2015-11-16 07:36:26

标签: android listview android-intent start-activity

我有3个活动,A,B和C.活动流程从A到C,然后再次返回图像和从C到A的值。所有返回的值和图像都将加载到listView A。

listView是可点击的,一旦点击,它将intent到B进行某些版本。但现在的问题是当我在B中编辑文本值并返回到A时,editText值仍然保持不变。

这里有什么问题?有人可以帮忙吗?

活动C

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_fit_screen);
        selectImage();
        b = (ImageView) findViewById(R.id.imageView3);
        t = (EditText) findViewById(R.id.editText38);
        cancel=(Button)findViewById(R.id.button15);
        ok=(Button)findViewById(R.id.button16);

        cancel.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0) {
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener()
        {  // return to B
            public void onClick(View arg0)
            {
                Intent returnIntent=new Intent();
                text=t.getText().toString();
                b.setDrawingCacheEnabled(true);
                b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
                b.buildDrawingCache(true);
                returnIntent.putExtra("text", text);
                if (b.getDrawingCache() != null) {
                    Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
                    if (bitmap == null) {
                        Log.e("TAG", "getDrawingCache() == null");
                    }
                    Global.img = bitmap;
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });
    }

活动B

    ImageButton imageButton;
    ImageView viewImage;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.project);
        txt = (EditText) findViewById(R.id.editText36);
        txt1=(TextView)findViewById(R.id.textView57);
        Button b = (Button) findViewById(R.id.button17);
        addListenerOnButton();
        viewImage = (ImageView) findViewById(R.id.imageView2);

        if(getIntent().getExtras()!=null) { //if  has value pass from A
            final String Amount = getIntent().getExtras().getString("result");
            final String description1 = getIntent().getExtras().getString("description");

            txt1.setText(description1);
            txt.setText(Amount);
        }

                b.setOnClickListener(new View.OnClickListener() {  // return to A
                    public void onClick(View arg0) {
                        Intent returnIntent = new Intent();
                        a = "Project";
                        text = txt.getText().toString(); // amount
                        returnIntent.putExtra("text", text);
                        returnIntent.putExtra("a", a);
                        returnIntent.putExtra("c", c);
                        setResult(Activity.RESULT_OK, returnIntent);
                        finish();
                    }
                });
                viewImage.setImageBitmap(Global.img);

                }



    public void addListenerOnButton() {
         imageButton = (ImageButton) findViewById(R.id.imageButton);
         imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Global.img=null;
                Intent i = new Intent(B.this,C.class);
                startActivityForResult(i, PROJECT_REQUEST_CODE);
            }

        });

    }

    public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
        if(requestCode==PROJECT_REQUEST_CODE) { // receive fom C
            if(data!=null&&data.hasExtra("text")) {
                c = data.getStringExtra("text");
                txt1.setText(c);
                viewImage.setImageBitmap(Global.img);  // image from C can be shown here
            }


        }
        else if (requestCode==CAMERA_REQUEST_CODE)
        {

        }
    }

}

活动A

  @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {  //receive value from B
        switch (requestCode) {
                    case 0:
                        result = data.getStringExtra("text");
                        name = data.getStringExtra("a");
                        description = data.getStringExtra("c");
                        as = Long.parseLong(result);
                        Log.d("FIRST", "result:"+result);
                        Text="  "+name+"                                  "+"RM"+result+"";
                        m_listItems.add(Text);
                        listV.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        break;
                        }

    listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {  // listview intent to B
                public void onItemClick(AdapterView<?> listView, View view,
                                        int position, long id) {

                    if(name.equals("Project")) {
                        Intent intent = new Intent(B.this.getActivity(), B.class);
                        intent.putExtra("bitmap",true);
                        intent.putExtra("name",name);
                        intent.putExtra("result",result);
                        startActivityForResult(intent,0);
                    }

                }
            });
            return claims;
        }

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.create1:  // activity start from here by clicking icon
                AlertDialogRadio();
                return true;
        }
        return (super.onOptionsItemSelected(item));
    }


    public void AlertDialogRadio() {
        final CharSequence[] ClaimsModel = {"B", "Petrol"};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());


 alt_bld.setTitle("Select a Claims");
    alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
            .OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {
            if (item == 0) {
                Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
                startActivityForResult(intent, 0);
            }

被修改

删除k值并在startActivityForResult中使用startActvity代替Activity A后,我的问题仍未解决,因为它创建了新的listView而不是编辑旧的。

1 个答案:

答案 0 :(得分:1)

要处理来自ActivityB的回调,您应该使用startActivityForResult()代替startActivity()开始活动

更改您的代码:

if(name.equals("Project")) {
   Intent intent = new Intent(this.ActivityA, B.class);
   intent.putExtra("k",k);
   intent.putExtra("bitmap",true);
   intent.putExtra("name",name);
   intent.putExtra("result",result);
   startActivityForResult(intent, 0); // here "0" because you choose "0" as requestcode in your onActivityResult() method
}