我正在使用nav drawer。所以我需要从Fragment
开始Activity
我开始另一个Activity
,其中我有ListView
,点击我必须保存数据。
保存后,我需要返回保存数据必须显示的Fragment
。
这是我的Fragment
public class ArmoryFragment extends Fragment {
public ArmoryFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_armory, container, false);
Button button = (Button) rootView.findViewById(R.id.button_rifles);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Rifles.class);
startActivity(intent);
}
});
return rootView;
}
}
这是我需要返回的活动
public class Rifles extends Activity implements View.OnClickListener, OnItemSelectedListener {
DatabaseHelper db;
String BrandModel;
private RifleDAO rifleDAO;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rifles);
initList();
this.rifleDAO = new RifleDAO(this);
ListView listView = (ListView) findViewById(R.id.listView1);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, riflesList, android.R.layout.simple_list_item_1, new String[] {"rifle"}, new int[] {android.R.id.text1});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
BrandModel= ((TextView)view).getText().toString();
String[] parts=BrandModel.split(" ");
String Brand=parts[0];
String Model = parts[1];
// need to return from somewhere here
}
});
} next is parser code... supose it is not needed
那么如何从Activity
返回Fragment
?
答案 0 :(得分:1)
好吧,如果你想保存数据,我更喜欢将数据添加到数据库中的表中(显然你有一个databaseHelper类,所以让你拥有自己的表),当你从你的活动回来时,你需要额外的位置和打电话:
selectItem(mCurrentSelectedPosition);
所以mCurrentSelectedPosition
是您想要选择的片段的位置。