我正在尝试制作记事卡应用程序。基本上,用户输入术语和定义,并允许从主要活动中显示的选择中查看数据。我无法通过意图发送信息并显示发送的信息。由于用户控制了信息量,我试图设置一个arraylist和arrayadapter来维护它。这是我的主要活动代码:
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class Main extends ListActivity {
ArrayList<String> cards;
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//This will hold the new items
cards = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cards);
Button b = (Button) findViewById(R.id.button1);
View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
//Set the adapter
this.setListAdapter(adapter);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Main.this, NewNote.class);
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
}
if (resultCode == RESULT_OK) {
String s1 =(String) getIntent().getCharSequenceExtra("term");
String s2 = (String) getIntent().getCharSequenceExtra("definition");
cards.add(s1);
cards.add(s2);
if(cards != null && cards.size() > 0){
for(int i=0;i<cards.size();i++)
adapter.add(cards.get(i));
}
adapter.notifyDataSetChanged();
}
}
}
这是我从用户那里检索信息的第二个活动:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class NewNote extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
final EditText et1 = (EditText) findViewById(R.id.editText1);
final EditText et2 = (EditText) findViewById(R.id.editText2);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("term", et1.getText());
intent.putExtra("definition", et2.getText());
setResult(0, intent);
finish();
}
});
}
}
此代码无法通过意图将信息发送回我的主要活动,并将其添加到我的arraylist中,应该显示。我也不知道如何将布局xml文件上的listview连接到java文件中的arraylist。我不知道如何继续,或者我是否走在正确的轨道上。有人可以帮忙吗?
答案 0 :(得分:1)
你有一个简单的错误,在NewNote
课程中你打电话给setResult(0, intent);
其中0是RESULT_CANCELED
,而Main
类'onActivityResult
则尝试处理RESULT_OK
个案。
致电setResult(RESULT_OK, intent);
,一切都会好的。)
至于你的评论,是什么样的问题?但是无论如何都要试试这个补丁
注意:
public class NewNote extends Activity {
public static final String EXTRA_TERM = "com.yourpackage.EXTRA_NOTE_TERM";
public static final String EXTRA_DEFINITION = "com.yourpackage.EXTRA_NOTE_DEFINITION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
final EditText et1 = (EditText) findViewById(R.id.editText1);
final EditText et2 = (EditText) findViewById(R.id.editText2);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra(EXTRA_TERM, et1.getText().toString());
intent.putExtra(EXTRA_DEFINITION, et2.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});
}
}
主页中的:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final Intent intent = getIntent();
String s;
if((s = (String) intent.getStringExtra(NewNote.EXTRA_TERM)) != null) {
adapter.add(s);
}
if((s = (String) intent.getStringExtra(NewNote.EXTRA_DEFINITION)) != null) {
adapter.add(s);
}
if(s != null) {
adapter.notifyDataSetChanged();
}
}
}
}