在Fragment中,单击listView项以打开新活动并将值传递给其他活动

时间:2018-05-21 11:00:04

标签: android listview

在片段中,单击listView项目以打开新活动并将值传递给其他活动

   list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    sowing sowing=new sowing();
                    FragmentManager fragmentManager=getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.FragmentContainer,sowing,sowing.getTag())
                            .addToBackStack("fragBack").commit();
                    String selectedFromList =(list.getItemAtPosition(position).toString());                
        }
    });

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您要找的是Bundles

以下是您在案例中如何使用它的示例:

list.setOnClickListener(new AdapterView.onItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    sowing sowing = new sowing();
    FragmentManger fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.FragmentContainer, sowing, sowing.getTag())
        .addToBackStack("fragBack").commit();
    String selectedFromList = (list.getItemAtPosition(position).toString));

    // THE BUNDLE
    // start a new intent to open the activity
    Intent newIntent = new Intent(Context, Activity.class);
    newIntent.putStringExtra("NAME_OF_BUNDLE", selectedFromList); // I used putStringExtra because you defined 'selectedFromList' to be a string
    startActivity(newIntent);
   }
});

现在,在您打开的活动中,只需调用包的名称,如下所示:

Intent intent = getIntent(); // get the intent that caused this activity to be opened
String selectedFromLastActivity = intent.getStringExtra("NAME_OF_BUNDLE"); // from the intent that caused this activity to be opened, get any extras passed through