【问题标题】:Add Items in a listview to another Activity将列表视图中的项目添加到另一个活动
【发布时间】:2017-03-18 17:03:20
【问题描述】:

我创建了一个自定义列表视图。我想从 MainActivity 中的 secondActivity 将项目添加到我的列表视图中,即使我的 Activity 没有打开。当我的 Mainactivity(带有列表视图)打开时,也会向我展示我添加的内容。

这是我的头等舱代码:

 class TableItemsClass
{
    public string Description { get; set; }
    public string Quantity { get; set; }
    public string ItemPrice { get; set; }
    public string TotalPrice { get; set; }

}

我的第二堂课

   class MyListViewAdapter : BaseAdapter<TableItemsClass>
{
    public List<TableItemsClass> mitems;
    private Context mContext;
    public MyListViewAdapter(Context context, List<TableItemsClass> items)
    {
        mitems = items;
        mContext = context;

    }
    public override int Count
    {
        get
        {
            return mitems.Count;
        }
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override TableItemsClass this[int position]
    {
        get
        {
            return mitems[position];
        }

    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.TableItems, null, false);

        }
        TextView txtDescription = row.FindViewById<TextView>(Resource.Id.txtDescription);
        txtDescription.Text = mitems[position].Description;


        TextView txtQuantity = row.FindViewById<TextView>(Resource.Id.txtQuantity);
        txtQuantity.Text = mitems[position].Quantity;

        TextView txtItemPrice = row.FindViewById<TextView>(Resource.Id.txtItemPrice);
        txtItemPrice.Text = mitems[position].ItemPrice;

        TextView txtTotalPrice = row.FindViewById<TextView>(Resource.Id.txtTotalPrice);
        txtTotalPrice.Text = mitems[position].TotalPrice;



        return row;
    }
}

}

所以我在第二个活动中使用了这样的代码,以便在主活动中向我的列表视图添加一个额外的项目。但不幸的是它没有添加任何东西。

   private List<TableItemsClass> mItems;
    private ListView mlistview;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);



        SetContentView(Resource.Layout.TableItemsMain);
        mlistview = FindViewById<ListView>(Resource.Id.listView1);

        mItems = new List<TableItemsClass>();

        mItems.Add(new TableItemsClass() { Description = "test", Quantity = "3", ItemPrice = "2.00", TotalPrice = "6.00" });

        MyListViewAdapter adapter = new MyListViewAdapter(this, mItems);


        mlistview.Adapter = adapter;

【问题讨论】:

  • 有几种解决方案。告诉我更多关于你的情况然后我会建议一个完整代码的解决方案 1. 将数据从活动传递到具有意图的 ListView 活动 2. 在第二个活动中保存数据(在文件、SharedPref、sqlite 中)并重新加载列表视图 3. 保存数据(在静态变量)在第二个活动中并重新加载列表视图
  • 我认为最好的主意是如果我可以有意地从列表视图中添加项目。当打开我的 MainActivity 向我展示我添加的所有项目时。谢谢
  • 所以使用这个intent.putExtra("key", String[] items);
  • 这种方法灵活吗?我的意思是我可以从我的数据中删除一项吗?

标签: java c# android xamarin


【解决方案1】:

尝试LocalBroadcastReceiver 解决您的问题。这是您如何在您的情况下使用 -

将此代码放入您的接收器activity,即MainActivity

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    TableItemsClass message = intent.getSerializableExtra("message");
    // Add this data into your list dataset... Later on you can update your
    // `onResume()` to update data...
  }
};

将接收者注册到您的oncreate()

@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Register to receive messages.
  // We are registering an observer (mMessageReceiver) to receive Intents
  // with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

别忘了销毁你的receiver -

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

在您的发件人activity 中执行此代码,即SecondActivity

// Send an Intent with an action named "custom-event-name". The Intent sent should 
// be received by the ReceiverActivity.
private void sendMessage() {
  Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "your model to be put in receiver's listview");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多