我有一对夫妇的ListView项目时,我就特别列表视图点击进入Fragment_2与被点击的ListView的价值,我需要。我尝试下面的Fragment_1我得到了适当的值就显示Tosat通知代码,但在Fragment_2束总是空。
在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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句