我已创建自定义列表视图。我想将项目添加到我的列表视图中从MainActivity中的secondActivity甚至我的活动都没有打开。当我的Mainactivity(列表视图)打开时,也会告诉我我添加的内容。
这是我的第一堂课代码:
<?php
require("app/start.php");
$s3->uploadDirectory('app', 'cbtest', '', array(
'params' => array('ACL' => 'public-read'),
'concurrency' => 20,
'debug' => true
));
?>
我的第二堂课
class TableItemsClass
{
public string Description { get; set; }
public string Quantity { get; set; }
public string ItemPrice { get; set; }
public string TotalPrice { get; set; }
}
}
所以我在第二个活动中使用这样的代码,在Main Activity中为我的listview添加一个额外的项目。但不幸的是它没有添加任何内容。
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;
}
}
答案 0 :(得分:0)
尝试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);
}