如何将多个数据从EditText(主活动)传递到ListView(另一个活动)


  1. MainActivity有一个列表视图和编辑按钮。单击"编辑"按钮后,将跳转到"编辑AddItem"页面。

  2. EditAddItem类有一个编辑文本和按钮,每次我在编辑文本上键入内容,然后单击"保存"时,数据都会传递到MainActivity,并显示在列表视图中。有人能给我一些建议吗?

    protected void onCreate(Bundle savedInstanceState) {//This is code part of EditAddItem Class
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_add_item);
    editText = (EditText)findViewById(R.id.editTextTextPersonName);
    button = (Button) findViewById(R.id.button);
    cancelButton = (Button) findViewById(R.id.cancel);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    Intent intent = new Intent(EditAddItem.this, MainActivity.class);
    intent.putExtra("text", editText.getText().toString());
    startActivityForResult(intent, 0);
    }
    });
    protected void onCreate(Bundle savedInstanceState) {//Code part of MainActivity
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.btnAddItem);
    listView = (ListView) findViewById(R.id.lstView);
    addItemEditText = (EditText)findViewById(R.id.editTextTextPersonName);
    items = new ArrayList<String>();
    items.add("item one");
    items.add("item two");
    
    itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    listView.setAdapter(itemsAdapter);
    
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    openEditItemView();
    }
    });
    

基本上您的逻辑是切换的。您应该从MainActivity中startActivityForResult,并以EditAddItemActiity的结果结束。然后在MainActivity中,您应该覆盖onActivityResult以获取从EditAddItemActivity传递的文本。

你应该这样做:

主要活动.java

public static final int EDIT_ADD_ITEM_REQUEST = 1;
public static final String EXTRA_EDIT_ADD_ITEM = "EXTRA_EDIT_ADD_ITEM";
protected void onCreate(Bundle savedInstanceState) {//Code part of MainActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btnAddItem);
listView = (ListView) findViewById(R.id.lstView);
addItemEditText = (EditText)findViewById(R.id.editTextTextPersonName);
items = new ArrayList<String>();
items.add("item one");
items.add("item two");

itemsAdapter = new ArrayAdapter<String(this,android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemsAdapter);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(this, EditAddItem.class);
startActivityForResult(intent, EDIT_ADD_ITEM_RESULT);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
if(requestCode == EDIT_ADD_ITEM_REQUEST) {
String name = data.getStringExtra(EXTRA_EDIT_ADD_ITEM);
updateList(name);
}
}
}
private void updateList(String name) {
itemsAdapter.add(name);
}
}

编辑AddItem.java

protected void onCreate(Bundle savedInstanceState) {//This is code part of EditAddItem Class
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_add_item);
editText = (EditText)findViewById(R.id.editTextTextPersonName);
button = (Button) findViewById(R.id.button);
cancelButton = (Button) findViewById(R.id.cancel);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent();
resultIntent.putExtra(MainActivity.EXTRA_EDIT_ADD_ITEM, editText.getText().toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});

在第一个活动中覆盖onActivityResult((,您需要正确实现将数据作为捆绑包从第二个活动中传递,并获取数据、更新列表和调用notify data change。

在您的MainActivity中(与您有ListView的活动相同(-

editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, EditAddItem.class);
startActivityForResult(intent, 0);
}
});
// also override below method
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode ==0 && resultCode== RESULT_OK && data != null)
{
Sting editTextContent = data.getStringExtra("editText"); // your data

//now put data into your ListView
items = new ArrayList<String>();
items.add(editTextContent);

itemsAdapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemsAdapter);
}
}

在您的EditAddItem活动中(编辑文本活动(-

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!TextUtils.isEmpty(editText.getText().toString()))
{
Intent resultIntent = new Intent();
resultIntent.putExtra("editText", editText.getText().toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
} else
{ 
Toast.makeText(this, "EditText is empty!", Toast.LENGTH_SHORT).show();
}
}
});

相关内容

  • 没有找到相关文章