I'm using below code to populate my Main Activity with Fragment containing Listview. I'm following a tutorial from http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/.
I would like to know, how to use intent to open a separate activity / Fragment when each item clicked on Listview.
For example, wWhen first item is clicked, it will open A Fragment and when second item is clicked, it will open B Fragment.
package com.nepalpolice.cdp;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Sagar on 2017/09/23.
*/
public class club extends Fragment {
// Array of strings storing country names
String[] countries = new String[]{
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.eka,
R.drawable.kat,
R.drawable.rat,
R.drawable.set,
R.drawable.ann,
R.drawable.kar,
R.drawable.suk,
R.drawable.sap,
R.drawable.him,
R.drawable.gor
};
// Array of strings to store currencies
String[] currency = new String[]{
"Indian Rupee",
"Pakistani Rupee",
"Sri Lankan Rupee",
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean Won",
"South Korean Won",
"Japanese Yen"
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_club, container, false);
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 10; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur", "Currency : " + currency[i]);
hm.put("flag", Integer.toString(flags[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = {"flag", "txt", "cur"};
// Ids of views in listview_layout
int[] to = {R.id.flag, R.id.txt, R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) view.findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
return view;
}
// Item Click Listener for the listview
AdapterView.OnItemClickListener itemClickListener = new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
if(position == 1/*or any other position*/){
Fragment fragment = new notices();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_frame, fragment).addToBackStack(null).commit();
}
else if(position == 2){
} // etc...
}
};
}