我正在尝试制作记事卡应用程序。基本上,用户输入一个术语和定义,并允许从我的主要活动中显示的选择中查看数据。我无法通过意图发送信息并显示发送的信息。由于用户控制信息量,我尝试设置一个 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();
}
});
}
}
此代码无法通过意图将信息发送回我的主要活动并将其添加到我的数组列表中,这应该显示。我也不知道如何将布局xml文件上的listview连接到java文件中的arraylist。我不确定如何进行,或者我是否走在正确的轨道上。任何人都可以帮忙吗?