I'm making a simple app where I'll have two level lists. I've created the first level list with ListView but I'm not sure how to go about creating second level list.
Example:
First level list is:
Fruits
Cookies
When Fruits is clicked then the list below is shown:
Bananans
Grapes
Apples
When cookies is clicked then the list below is shown:
Oreo's
Choc Chip
Code for first level list:
public class MainActivity extends SherlockActivity {
SimpleAdapter simpleAdpt;
List<Map<String, String>> list1 = new ArrayList<Map<String,String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, list1, android.R.layout.simple_list_item_1, new String[] {"list1"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
}
private void initList() {
// We populate the planets
list1.add(creatList1("list1", "Fruits"));
list1.add(creatList1("list1", "Cookies"));
}
private HashMap<String, String> creatList1(String key, String name) {
HashMap<String, String> list = new HashMap<String, String>();
list.put(key, name);
return list;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Check out for ExpandableListView. I suits to your need.
Related
Hello ladies and gentlmen,
Though I can receive data (reservations) on my screen, however I can not display some textviews on the screen.
public class Res extends Fragment {
ListView lview;
ListAdapter adapter;
ProgressDialog loading;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reslist, container, false);
lview = view.findViewById(R.id.lview);
getReservations();
return view;
here If I inflate fragment_reservation instead of reslist , I can display textviews,but I can display listview.
and here my listadapter code;
adapter = new SimpleAdapter(getActivity(),list,R.layout.fragment_reservations,
new String[]{"name", "email", "phone", "arriving", "departure", "pax", "roomtype", "rate"}, new int[]{R.id.textView, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, R.id.textView7, R.id.textView8});
lview.setAdapter(adapter);
loading.dismiss();
maybe problem about getActivty() and I need to use different method calling..
Thanks in advance...
here the photo of the screen
SCREEEN
I think problem is with parameter "list"(ArrayList) while creating SimpleAdapter object. Here is my code to do the same thing, it is working fine.
public class BlankFrag extends Fragment {
private View v;
private ListView listView;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
v = inflater.inflate(R.layout.blank_fragment, container, false);
listView = v.findViewById(R.id.dummy_list);
ArrayList<HashMap<String, String>> list = new ArrayList<>();
//populating dummy content
for(int i=0; i<5; i++){
HashMap<String, String> temp = new HashMap<>();
temp.put("name", "name"+Integer.toString(i));
temp.put("sname", "sname"+Integer.toString(i));
list.add(temp);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.single_item, new String[]{"name", "sname"}, new int[]{R.id.name, R.id.sname});
listView.setAdapter(adapter);
return v;
}
}
enter image description hereBelow is the coding i have and it seems i have problem on the
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
How do i use image and text together for the list adapter i have in my fragment? Please help.
public class AgentFragment extends Fragment{
String[] member_names;
TypedArray profile_pics;
ListView mylistview;
String[] agentname={"Robert","Shanni","Rachel","Mady","Nikhil"};
Integer [] imgid = {R.drawable.robert,R.drawable.shanni,R.drawable.rachael,R.drawable.maddy_pic,R.drawable.nikhil_pic};
public AgentFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_layout1, null);
setupList(view);
return view;
}
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
}
Since its possible that the fragment has not been placed in any activity at runtime you can afford for this by changing the getActivity() to be the context from the view parameter:
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(view.getContext(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
I have been working on a list fragment with certain items . on clicking on each item ,i need to open a new listfragment against that item in fragment. So im posting my listfragment code ....
public class SellListFragment extends ListFragment implements OnItemClickListener {
String[] menutitles;
TypedArray menuIcons;
SellCustomAdapter adapter;
private List<RowItem> rowItems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, null, false);
}
#Deprecated
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.titles);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menutitles.length; i++) {
RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(i, -1));
rowItems.add(items);
}
adapter = new SellCustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// ((CategorySelectedListener)getActivity()).categorySelected(position);
//Toast.makeText(getActivity(), menutitles[position], Toast.LENGTH_SHORT).show();
;
}
}
Create these arrayLists in you current SellListFragment at Class level.
ArrayList<ArrayList<String>> subCategoriesList=new ArrayList<>(); //Arraylist which will contain all the subCategories.
ArrayList<String> subCategoriesForCategory1=new ArrayList<>(); //ArrayList of sub categories for category 1.
ArrayList<String> SubCategoriesForCategory2=new ArrayList<>(); //Repeat for as many categories you have.
In onCreate() fill your sub-category arrylists.
subCategoriesForCategory1.add("SubCat1");
subCategoriesForCategory1.add("SubCat2"); //Repeat as per your needs
subCategoriesForCategory2.add("SubCat1");
subCategoriesForCategory2.add("SubCat2"); //Repeat as per your needs
subCategoriesList.add(subCategoriesForCategory1); //add to main arrayList
subCategoriesList.add(subCategoriesForCategory2); //Repeat
Put this code inside onItemClick()
FragmentManager fm=getActvity().getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
FragmentToBeReplacedWith fragmentObj=new FragmentToBeReplacedWith();
Bundle bundle=new Bundle();
bundle.putStringArrayList("subcategories",subCategoriesList.get(position)); //Repeat for as many values you want to pass. Explore for what suits your needs.
fragmentObj.setArguments(bundle);
ft.replace(R.id.id_of_fragment_container,fragmentObj);
ft.addToBackStack("setSomeUniqueName"); //Optional and nullable
ft.commit();
In FragmentToBeReplacedWith create a global arraylist
ArrayList<String> subCategories=new ArrayList<>();
In onCreate() of FragmentToBeReplacedWith do
Bundle bundle=getArguments();
subCategories=bundle.getStringArrayList("subcategories");
Now use the values in ArrayList subCategories where ever you need.
Hope this helps.
I'm trying to display data from SQLite and display it in the listview in fragment.This is my code and when I run it, it closes automatically. This code is inside my main activity
public static class ListDoctorFragment extends Fragment {
ListView list;
DataDB data = new DataDB();
ArrayAdapter<String> listAdapter;
public ListDoctorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listdoctor, container, false);
ArrayList<String> names = new ArrayList<String>();
try {
names = data.getDoctorlistDB(getActivity());
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);
// set the adapter
list.setAdapter(listAdapter);
return view;
}
}
Your 'ListView list' is not initialized. It must be something like:
list = (ListView) view.findViewById(R.id.listview);
I am a newbie in Android programming and I have this problem:
In this simple example, when I click on start button, I run a fragment. When I click on alarm button, I write in a log file.
Now, if I click first the start button, later the alarm button don't run. I think the problem is the fragment don't leave the interaction with user. Can you help me?
best regads
A.
public class MainActivity extends FragmentActivity {
private static final String FRAG1_TAG = MainActivity.class.getCanonicalName() +".fragment1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("exemple", "start button Start !!!!!!");
goFragment();
}
});
Button buttonAllarm = (Button) findViewById(R.id.buttonAllarm);
buttonAllarm.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("exemple", "start button allarm !!!!!!");
}// fine onClick
});// fine onClickListner
}
void goFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction xact = fm.beginTransaction();
if (null == fm.findFragmentByTag(FRAG1_TAG)) {
xact.replace(android.R.id.content, new ListFramment(), FRAG1_TAG);
xact.addToBackStack (null); // questo serve devi metterlo
xact.commit();
}
}
this is the fragment
public class ListFramment extends ListFragment {
// onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
AppLog.logString("Parte onCreateView");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppLog.logString("Parte onActivityCreated");
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("iPhone", "iPhone"));
return list;
// TODO Auto-generated method stub
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
Your problem is here:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
AppLog.logString("Parte onCreateView");
return view;
}
Your Fragment is inflating the same layout as the Activity that created it. But the Fragment does not have any onClickListeners. I suspect that the Fragment layout is covering up the Activity layout, duplicating it exactly, with non-functional buttons. That would explain why the button click works only the first time.
It would be normal for the Fragment to have its own layout, different from the Activity.
Not seeing your layout file does not help. But I suspect it got nothing to do with fragments.