我有几个listview项,当我单击特定的listview时,需要使用被单击的listview值转到Fragment_2。我在Fragment_1上尝试下面的代码,我得到了正确的值,它在Tosat通知中显示,但是在Fragment_2包中始终为null。
在Fragment_1的onCreate方法中
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
MainFragment fragment = new MainFragment ();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
fragment.setArguments(bundle);
在Fragment_2的onCreate方法中
Bundle bundle = this.getArguments();
if (bundle != null) {
String myString = bundle.getString("view");
Toast.makeText(getContext(), myString, Toast.LENGTH_SHORT).show();
UDPATE :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
MainFragment fragment = new MainFragment ();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
fragment.setArguments(bundle);
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
// ETC .......
}
});
答案 0 :(得分:2)
只需替换onItemClick方法中的以下代码即可。
String value = ((TextView)
view.findViewById(R.id.username)).getText().toString();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra(bundle);
startActivityForResult(myIntent, 0);
在MainActivity上,像这样替换MainFragment,
MainFragment fragment = new MainFragment ();
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.replace("your fragment container id here", fragment)
.commit();
答案 1 :(得分:1)
您没有提交片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
MainFragment fragment = new MainFragment ();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
fragment.setArguments(bundle);
// This line is important
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
答案 2 :(得分:0)
您正在启动活动“ MainActivity.class”,并将捆绑软件传递给Fragment类“ MainFragment”,但从未真正启动MainFragment。 因此,很明显,您的代码无法正常工作
答案 3 :(得分:0)
您正在调用活动,以便如何获取片段中的数据以及如果您这样做:
首先,您要以此启动
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
然后在此活动中,您将启动片段
getFragmentManager().beginTransaction().replace("your fragment container id here",
fragment).commit();
在这种情况下,首先将数据传递给意图
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putString("data","Put the data which you want to pass");
startActivityForResult(myIntent, 0);
并使用包将此数据传递给片段 希望它对您有用。