-1

I have two Activity, A and B. In A, it has a listView and a button. In B, it has imageView and editText . Now what I trying to achieve is return the text from B to listView A.

Activity A

   View claims = inflater.inflate(R.layout.receipt_text, container, false);
     listV = (ListView) claims.findViewById(R.id.listView);
  String [] status ={"Type of Claims :" +name,"Amount :" +result};

     adapter=new ArrayAdapter<String>(this,R.layout.claims,status);
     listV.setAdapter(adapter); 

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

Activity 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

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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" />

</android.support.v4.widget.DrawerLayout>

Error

11-16 10:57:24.696 30313-30313/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.project.project, PID: 30313 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Hoo
  • 1,806
  • 7
  • 33
  • 66

1 Answers1

0

The constructor for ArrayAdapter is wrong.

adapter=new ArrayAdapter<String>(this,R.layout.claims,c);

Instead of passing an int into the ArrayAdapter constructor, creating a List and pass it in. Later add the data you get from Activity B to the list.

List<String> list = new ArrayList<>();
adapter=new ArrayAdapter<String>(this, R.layout.claims, list);

When you get the data back from ActivityB, do this

    String data = Integer.toString(c);
list.add(data);
adapter.notifyDataSetChanged();
Eric Liu
  • 1,516
  • 13
  • 19