所以我试图弄清楚我是否可以将自定义适配器的额外意图传递给活动,然后将该意图传递给另一个活动?
这样的事情是可能的还是有更好的方法来做我想做的事情?
答案 0 :(得分:1)
Activity A:
Intent intent = new Intent(A.this, B.class);
intent.putExtra("someString", "string");
startActivity(intent):
Activity B:
onCreate(...) {
String myString = getIntent().getStringExtra("someString");
MyAdapter myAdapter = new MyAdapter(B.this, myString);
}
MyAdapter :
Context myContext;
String myString;
MyAdapter(Context context, String string) {
this.myContext = context;
this.myString = string
}
现在您将活动A中的String添加到适配器中:)
答案 1 :(得分:0)
我假设您知道如何向活动发送意图附加内容。而你正在寻找的是一种将同样的意图转发给另一个活动的方法。你可以这样做:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(getIntent());
答案 2 :(得分:0)
上面提到的所有答案都有效。我最终使用的是listview上的onItemClickListener并以这种方式获取信息。我没有在自定义适配器中启动意图,而是在自定义适配器所在的活动中启动了意图。
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});
答案 3 :(得分:0)
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});