我有这个"帮助部分"在我的应用程序中,它由一个包含不同项目的ListView组成。当我点击ListView的每个项目时,我试图打开相同的片段,但是时间打开它应该显示不同的信息。
我尝试使用intent并在打开片段时使用getExtra,但我并不成功。我在这里有点迷失,任何帮助都将不胜感激。
这是我的"帮助活动"代码:
`
ListView list_main;
//Itens do LV
ListView list;
// ListView items
String[] item = {" Como começar?", " Como adcionar uma carona?", " Como remover uma carona?",
" Como faço para pegar uma carona com alguém?", " Minhas informações estão seguras?", " Como editar meu perfil?",
" Tive um problema com uma carona, o que fazer?", " Como trocar minha senha?", " Preciso dividir o valor da carona?",
"Recomendações aos usuários", "Entre em contato"
};
// ListView images
Integer[] imageId = {
R.drawable.information, R.drawable.plus, R.drawable.removecloud, R.drawable.network,
R.drawable.lock, R.drawable.user, R.drawable.siren, R.drawable.lock, R.drawable.piggy,
R.drawable.recomenda, R.drawable.mail};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_interno);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Dúvidas Frequentes");
HelpList adapter = new HelpList(HelpInterno.this,item,imageId); // Custom adapter
list_main = (ListView) findViewById(R.id.list_main);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// What to do ?
}
})
}
`
谢谢。
答案 0 :(得分:1)
尝试这样做:
//make an instance for your fragment inside your fragment
public static your_fragment_name newInstance(String var1, String var2, int var3) {
your_fragment_name fragment = new your_fragment_name();
Bundle args = new Bundle();
args.putString(first_key, var1);
args.putString(second_key, var2);
args.putInt(third_key, var3);
your_fragment_name.setArguments(args);
return fragment;
}
//get variables inside your fragment in this way
private getExtras(){
String var1 = getArguments().getString(first_key);
String var2 = getArguments().getString(second_key);
int var2 = getArguments().getInt(third_key);
}
//call your fragment in this way
your_fragment_name fragment = your_fragment_name.newInstance(var1, var2, var3);
getSupportFragmentManager().beginTransaction()
.replace(/*your view detail id*/,fragment).commit();
答案 1 :(得分:0)
您可以在documentation中看到如何将数据传递到新片段;在你的场景中你可以像下面这样做 将此代码放在片段类
中public static HelpDetailFragment getStartFragment(String item, int imageId) {
Bundle arguments = new Bundle();
arguments.putString(EXTRA_ITEM, imageId);
arguments.putString(EXTRA_IMAGE_ID, imageId);
HelpDetailFragment fragment = new HelpDetailFragment();
fragment.setArguments(arguments);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help, container, false);
Bundle extras = getArguments();
if (extras != null) {
String item = extras.getString(EXTRA_ITEM);
int imageid = extras.getInt(EXTRA_IMAGE_ID);
}
....
return rootView;
}
然后在单击列表项
时导航到片段@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getSupportFragmentManager()
.beginTransaction()
.replace(/*your view detail id*/, HelpFragment.getStartFragment(item[position], imageId[position]))
.addToBackStack("help")
.commit();
}
了解片段导航here