I have this "help section" in my application, which consists of a ListView with different items. I'm trying to open the same fragment when I click on each item of the ListView, but every time It opens, it should display different information.
I tried to use an intent and use getExtra when I open the fragment, but I was not successful. I'm a bit lost here, any help will be appreciated.
Here's my "help activity" code:
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 ?
}
})
}
Thank You.
You can see in the documentation how to pass data to a new fragment; in you scenario you can do like below Put this code in your fragment class
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;
}
Then navigate to the fragment when clicking the list item
@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();
}
Read about fragment navigation here