1

我正在尝试制作记事卡应用程序。基本上,用户输入一个术语和定义,并允许从我的主要活动中显示的选择中查看数据。我无法通过意图发送信息并显示发送的信息。由于用户控制信息量,我尝试设置一个 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。我不确定如何进行,或者我是否走在正确的轨道上。任何人都可以帮忙吗?

4

1 回答 1

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();
                }
            }
        }
    }
于 2012-05-26T21:17:23.507 回答