EditText 不会 return 到 ListView
EditText does not return to ListView
我有两个Activity,A和B。在A中,它有一个listView
和一个button
。在 B 中,它有 imageView
和 editText
。现在我想要实现的是 return 从 B 到 listView
A 的文本。
Activity一个
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>
错误
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
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();
我有两个Activity,A和B。在A中,它有一个listView
和一个button
。在 B 中,它有 imageView
和 editText
。现在我想要实现的是 return 从 B 到 listView
A 的文本。
Activity一个
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>
错误
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
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();