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;
}
}
Related
I'm trying to access from my code to listview and a textview, I read a lot of posts on the internet but I didn't find anything that works for me.
I have a blank fragment like this:
public class MyFrag extends Fragment {
//My Code
List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates = new ArrayList<String>();
List<String> Hours = new ArrayList<String>();
List<String> iDs = new ArrayList<String>();
Double TotalMy = 0.00;
SimpleAdapter adapter;
public MyFrag() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_my_frag, container, false);
//View view = inflater.inflate(R.layout.fragment_my_frag, container, false);
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_my_frag, container, false);
ListView listview = (ListView) getView().findViewById(R.id.list);
TextView textTotalMy = (TextView) getView().findViewById(R.id.textTotalMy1);
return view;
}
But when I open the fragment the application crashes.
Please help me
I solved it by creating the same Relative Layout programmatically
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'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 converting an existing activity (SherlockActivity) into fragment (SherlockListFragment) in order to use tabs.
The fragment returns a onCreateView when created and is using the following code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
That works fine but as I was doing in my activity I want to update the list view with my own content after I finish fetching it from the server.
ListView listContent = (ListView) getView().findViewById(R.id.contentlist);
listContent.setAdapter(myAdapter);
Whatever I have researched so far, I am fetching the listview through activity and setting my adapter to it but that is having no effect on fragment view and it remains the same.
Question is: How can I update listView from outside onCreateView?
First, make your listview a global var. Then set its value on the onCreateView function:
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_placelist, container, false);
list = rootView.findViewById(/*your id*/)
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
//change your listview content here when refresh or something else
public void updateListView(){
List l = /*fetch new data from server*/
listAdapter.addAll(l);
listAdapter.notifyDataSetChanged();
}
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_placelist, container, false);
list = rootView.findViewById(/*your id*/)
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return rootView;
}
from here android versions must be a arraylist or string array or hashmap
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.