我有两个活动,A和B.在A中,它有一个listView
和一个button
。在B中,它有imageView
和editText
。现在我想要实现的是将文本从B返回到listView
A.
活动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;
}
活动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>
错误
11-16 10:57:24.696 30313-30313 / com.example.project.project E / AndroidRuntime:致命异常:主要 处理:com.example.project.project,PID:30313 java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView
答案 0 :(得分:0)
ArrayAdapter的构造函数是错误的。
adapter=new ArrayAdapter<String>(this,R.layout.claims,c);
不是将int传递给ArrayAdapter构造函数,而是创建List并将其传入。稍后将从Activity B获得的数据添加到列表中。
List<String> list = new ArrayList<>();
adapter=new ArrayAdapter<String>(this, R.layout.claims, list);
当您从ActivityB获取数据时,请执行此操作
String data = Integer.toString(c);
list.add(data);
adapter.notifyDataSetChanged();