我正在使用monodroid来制作应用程序。在应用程序中,用户单击一个按钮进入列表视图,该列表视图有多个选项,用户选择选项,然后该选项被发送回原始类,其中textview被更改为与该选项相同的文本。已选中的列表视图。
以下是发送数据的类
protected override void OnListItemClick(ListView l, View v, int position, long id)
{
int t = labels[position];
String text = t + "";
Intent send = new Intent(this, typeof(CreateVehicle));
send.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
send.PutExtra("t", "Data from FirstActivity");
StartActivity(send);
}
以下是接收类:
protected override void OnResume()
{
base.OnResume(); // Always call the superclass first.
TextView tvYearChange = FindViewById<TextView>(Resource.Id.tvYearchange);
string text = Intent.GetStringExtra("t") ?? "work";
tvYearChange.SetText(text, null);
}
如果有人能弄明白为什么我没有收到那些很棒的数据。
答案 0 :(得分:0)
我不确定我是否正确解决了您的问题,但我猜您的主要活动是调用具有ListView的活动,然后您希望将结果返回到主活动中,对吗?
如果是这样,您显示的代码不是您想要的正确方法。您要找的是在主要活动中使用StartActivityForResult
并覆盖onActivityResult
方法。
我不确切知道如何使用Monodroid和C#,但我可以用Java给你一个例子,我相信它会帮助你理解如何得到你想要的东西:
假设我的ListView活动名为 myList 并扩展了ListActivity,我的主要活动称为 MainActivity 。下面是 myList 的OnListItemClick方法:
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
String text = String.valueOf(labels[position]);
// Create the intent with the extras
Intent send = new Intent();
send.putExtra("text_from_list", text);
// Set the result to OK (meaning the extras are put as expected by the caller)
setResult(RESULT_OK, send);
// you need this so the caller (MainActivity) knows that the user completed
// this activity (myList) as expected (clicking on the item to return a result)
// and didn't just leave the activity (back button)
// Close this List Activity
finish();
}
以下是我的主要活动中调用myList的方法:
private void callListActivity(){
Intent call = new Intent(MainActivity.this, myList.class);
startActivityForResult(call, 1);
// The second field is just a number to identify which activity
// returned a result when a result is received (you can have
// several calls to different activities expecting results from
// each one of them). This number is called requestCode.
}
您必须使用以下内容覆盖 MainActivity 中的onActivityResult
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check if the returned data came from myList (requestCode 1) and if
// data was actually received (check if resultCode = RESULT_OK)
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String text = data.getStringExtra("text_from_list");
// you have to use the same string identifier you used
// when you put the extra in the Intent in myList
TextView tvYearChange = (TextView)findViewById(R.id.tvYearchange);
tvYearChange.setText(text);
}
else {
// Do something if the Activity didn't return data
// (resultCode != RESULT_OK)
}
}
// If you are expecting results from other Activities put more if
// clauses here with the appropriate request code, for example:
// if (requestCode == 2) {
// DoSomething;
// }
// And so on...
}
将此Java代码修改为可以与Monodroid一起使用的C#代码应该不难。另外,请查看Android官方文档中的this link,它们在那里有很多有用的东西。
我希望这会对你有所帮助。
答案 1 :(得分:0)
在Receiver Activity中从intent获取数据,如下所示
Intent intent = getIntent();
string text = intent.getStringExtra("t");