没有文本从B返回到listView A.

时间:2015-11-16 03:29:55

标签: android listview android-intent android-listview return-value

我有两个活动,A和B.在A中,它有一个listView和一个button。在B中,它有imageVieweditText。现在我想要实现的是将文本从B返回到listView A.

活动A

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       View claims = inflater.inflate(R.layout.receipt_text, container, false);
      listV = (ListView) claims.findViewById(R.id.listView);
      adapter=new ArrayAdapter<String>(getActivity(),R.layout.claims,R.id.textView1,status);
     m_listItems.add(name);
    m_listItems.add(result);
    listV.setAdapter(adapter);
    adapter.notifyDataSetChanged();
     return claims;
        }

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {   // receive  text from B

        switch (requestCode) {
                case 0:
                    result = data.getStringExtra("text");
                    name = data.getStringExtra("a");
                    description = data.getStringExtra("c");
                    break;
                    }

活动B

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

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

        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(Project1.this, C.class);
                startActivityForResult(i, PROJECT_REQUEST_CODE);
            }

        });

    }

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


        }
        else if (requestCode==CAMERA_REQUEST_CODE)
        {

        }
    }

}

receipt_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:padding="10dp"
        android:text="@string/text" android:textSize="20sp" />

    <TextView
      android:id="@+id/textView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
    </TextView>

    <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

claims.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

这是我到目前为止所尝试的,然而,从B到A没有任何值的回报。这里有什么问题?

1 个答案:

答案 0 :(得分:0)

移动

m_listItems.add(name);
m_listItems.add(result);
listV.setAdapter(adapter);
adapter.notifyDataSetChanged();

到案例0