如何将列表视图中的数据从一个片段发送到另一个片段
How to send data from listview from one fragment to another
我有几个列表视图项目,当我单击特定的列表视图时,我需要转到 Fragment_2 并单击一个列表视图的值。我在 Fragment_1 上尝试下面的代码,我得到了它在 Tosat 通知中显示的正确值,但在 Fragment_2 中 bundle 总是空的。
在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 .......
}
});
只需在 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();
您正在启动 Activity "MainActivity.class" 并将您的包传递给 Fragment class "MainFragment" 但从未真正启动 MainFragment。
因此,很明显您的代码将无法正常工作
你没有提交片段
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();
您正在调用 activity 那么如何获取片段中的数据,如果您这样做的话:
首先,您要通过
启动 activity
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
然后在此 activity 中您将启动片段
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);
并使用包将此数据传递给片段
希望它对你有用。
我有几个列表视图项目,当我单击特定的列表视图时,我需要转到 Fragment_2 并单击一个列表视图的值。我在 Fragment_1 上尝试下面的代码,我得到了它在 Tosat 通知中显示的正确值,但在 Fragment_2 中 bundle 总是空的。
在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 .......
}
});
只需在 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();
您正在启动 Activity "MainActivity.class" 并将您的包传递给 Fragment class "MainFragment" 但从未真正启动 MainFragment。 因此,很明显您的代码将无法正常工作
你没有提交片段
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();
您正在调用 activity 那么如何获取片段中的数据,如果您这样做的话:
首先,您要通过
启动 activity Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
然后在此 activity 中您将启动片段
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);
并使用包将此数据传递给片段 希望它对你有用。