我有一个包含4个类的项目:2个活动,1个适配器和1个简单对象类。这些类的名称为:MainActivity,AddingItemsActivity,ItemAdapter,SimpleItem。在与MainActivity相对应的布局中,有一个名为SimpleListView的列表视图。 SimpleListView应该包含SimpleItem对象。 ItemAdapter用于处理SimpleListView。从MainActivity更新项目到SimpleListView很容易。我想达到的目的是从AddingItemsActivity(当用户回到MainActivity时出现)更新,添加显示在SimpleListView上的项目。您能告诉我该怎么做吗?

PS:我想问:“如何从AddingItemsActivity更新SimpleListView?”但是我已经读到这不是一个正确的问题,因为在AddingItemsActivity中不存在SimpleListView。

主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button toAddingActivity = findViewById(R.id.toAddingActivitybutton);
        final ListView simpleItemsListView = findViewById(R.id.SimpleListView);
        final ItemAdapter mAdapter = new ItemAdapter(this, R.layout.simple_item_adapter);


        toAddingActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getBaseContext(), AddingItemsActivity.class);
                startActivity(intent);
            }
        });
    }
}


AddingItemsActivity

public class AddingItemsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adding_items);
        Button addItem = findViewById(R.id.AddItembutton);
//        final ListView simpleItemsListView = findViewById(R.id.SimpleListView);
//        final ItemAdapter mAdapter = new ItemAdapter(this, R.layout.simple_item_adapter);



        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                first reflex try, which does not work:

//                SimpleItem item = new SimpleItem("String number 1", "String number 2");
//                mAdapter.add(item);
//                simpleItemsListView.setAdapter(mAdapter);
            }
        });
    }
}


物品适配器

public class ItemAdapter extends ArrayAdapter<SimpleItem>{
    public  ItemAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    public ItemAdapter(Context context, int resource, List<SimpleItem> items) {
        super(context, resource, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.simple_item_adapter, null);
        }

        SimpleItem  item = getItem(position);

        if (item != null) {
            TextView tv1 = v.findViewById(R.id.adapterTextView1);
            TextView tv2 = v.findViewById(R.id.adapterTextView2);

            tv1.setText(item.getStr1());
            tv2.setText(item.getStr2());
        }
        return v;
    }
}


SimpleItem

public class SimpleItem {
    private String str1;
    private String str2;


    public SimpleItem(String s1, String s2)
    {
        str1 = s1;
        str2 = s2;
    }

    public String getStr1()
    {
        return str1;
    }


    public String getStr2()
    {
        return str2;
    }


}

最佳答案

使用startActivityForResultSimpleItem获得结果作为AddingItemsActivity

Intent intent = new Intent(getBaseContext(), AddingItemsActivity.class);startActivityForResult(intent,1);
SimpleItem中创建一个AddingItemsActivity,向其中添加值,然后使用setResult将简单项目实例返回给MainActivity

// inside on click
SimpleItem item = new SimpleItem("String number 1", "String number 2");
Intent returnIntent = new Intent();
returnIntent.putExtra("result", item);
setResult(Activity.RESULT_OK,returnIntent);
finish();`

在MainActivity中

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        SimpleItem result = (SimpleItem)data.getSerializableExtra("result");
        // add result to the list, used by adapter
        // notify adapter using notifyDataSetChanged
    }
}
}//onActivityResult



注意:添加public class SimpleItem implements Serializable似乎忘记了创建列表并将其传递给适配器实例,因此只需创建它

10-06 07:05