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);
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;
}
}
I have 3 ListViews in one layout but in 3 different fragments. I want, when i scroll down, to first load the first listview and the other two listviews to scroll background at same the time when 1 change fragment same position
display at 2nd and 3rd fragment i loaded listview from database.
Pic of 1st fragment
//first fragment code
public class Quran extends Fragment {
ListView quranlst;
Parcelable state;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_quran, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
quranlst = (ListView) getActivity().findViewById(R.id.quranlist);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
int p = it.getIntExtra("paya", 0);
int i = it.getIntExtra("saya", 0);
if (i != 0) {
List<String> lstsa;
db.open();
lstsa = db.getsaya(i);
db.close();
MyCustomAdapter adapter = new MyCustomAdapter(lstsa, Quran.this.getActivity());
quranlst.setAdapter(adapter);
} else if (p != 0) {
List<String> lstpa;
db.open();
lstpa = db.getpaya(p);
db.close();
MyCustomAdapter adapter = new MyCustomAdapter(lstpa, Quran.this.getActivity());
//handle listview and assign adapter
quranlst.setAdapter(adapter);
}
}
}
pic of Second fragment
that is my 2nd fragment code
public class englisht extends Fragment {
ListView el;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_englisht, container, false);}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
el = (ListView) view.findViewById(R.id.englishlst);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
int i = it.getIntExtra("saya", 0);
int p = it.getIntExtra("paya", 0);
if (i != 0) {
List<String> lstenglishsurah;
db.open();
lstenglishsurah = db.geteaya(i);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lstenglishsurah);
el.setAdapter(adapter);
} else if (p != 0) {
List<String> lstenglishparah;
db.open();
lstenglishparah = db.paraheng(p);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lstenglishparah);
el.setAdapter(adapter);
}
}
}
**that is my third fragment code**
public class urdut extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.activity_urdut, container, false);}
ListView urdulst;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
urdulst = (ListView) view.findViewById(R.id.urdulst);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
//SurahAyat
int i = it.getIntExtra("saya", 0);
int p = it.getIntExtra("paya", 0);
if (i!=0) {
List<String> lsturdusurah;
db.open();
lsturdusurah = db.getuaya(i);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lsturdusurah);
urdulst.setAdapter(adapter);
}
else if (p!=0) {
List<String> lsturduparah;
db.open();
lsturduparah = db.parahurdu(p);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lsturduparah);
urdulst.setAdapter(adapter);
}
}
}
First get scroll position of the list view using this code
View c = listview.getChildAt(0);
int no= -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();
and then set listview scroll position to other fragment
getListView().setSelection(no);
no is : like 1,2,3....
You can use the methods smoothScrollToPosition and smoothScrollToPositionFromTop to scroll list(other lists) with respect to first list to which you are scrolling
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 tried following the recommendations here (How to refresh Android listview?) to update my ListView, but to no avail.
The relevant code is below. Please note that I call runOnUiThread in my MainActivity to refresh the ListView of this (fragment).
protected ArrayAdapter<String> adapter;
protected View view;
protected ArrayList<String> theList;
protected ListView listView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_1, container, false);
listView = (ListView) view.findViewById(R.id.listview);
theList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, theList);
listView.setAdapter(adapter);
if(view != null) { return view;}
((ViewGroup) listView.getParent()).removeView(listView);
container.addView(listView);
return view;
}
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.clear();
adapter.addAll(theList);
listView.invalidateViews();
adapter.notifyDataSetChanged();
listView.refreshDrawableState();
}
You already have the reference to the arraylist list so you dont need to add it again to the listView just simply add the string to the arraylist and notifyDataSetChanged;
example:
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.notifyDataSetChanged();
}
The code used here I pulled out of another fragment class which works very well.
I want to pull a list of files from the data/data/ directory and display them in a ListView.
The error thrown is at line 58. lv.setAdapter.
I have switched to Android studio from Eclipse so maybe that has something to do with it?
Logcat :
02-04 12:21:48.535 7433-7433/com.super8bit.singoffbeta E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.super8bit.singoffbeta.UserGamesHandler.SetListViewAdapter(UserGamesHandler.java:58)
at com.super8bit.singoffbeta.UserGamesHandler.onCreateView(UserGamesHandler.java:33)
My code is:
public class UserGamesHandler extends android.support.v4.app.Fragment implements
android.view.View.OnClickListener {
File path = new File(Environment.getRootDirectory().getAbsolutePath().toString());
ArrayList<String> fileList = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_games, container,
false);
getVideos(path);
SetListViewAdapter();
return view;
}
#Override
public void onClick(View view) {
}
private void getVideos(File f){
File[] files = f.listFiles();
//fileList.clear();
for (File file : files){
fileList.add(file.getName().toString()+".MP4 ");
}
}
private void SetListViewAdapter() {
ListView lv = (ListView) getActivity().findViewById(R.id.listViewTest);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.custom_listview, fileList);
lv.setAdapter(adapter);
Toast.makeText(getActivity().getApplicationContext(), fileList.toString(), Toast.LENGTH_LONG).show();
}
}
Declare you view variable above onCreateView, and replace this line
ListView lv = (ListView) getActivity().findViewById(R.id.listViewTest);
with
ListView lv = (ListView) view.findViewById(R.id.listViewTest);
public class UserGamesHandler extends android.support.v4.app.Fragment implements
android.view.View.OnClickListener {
File path = new File(Environment.getRootDirectory().getAbsolutePath().toString());
ArrayList<String> fileList = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_games, container,
false);
getVideos(path);
SetListViewAdapter();
return view;
}
#Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getVideos(path);
SetListViewAdapter();
}
private void getVideos(File f){
File[] files = f.listFiles();
//fileList.clear();
for (File file : files){
fileList.add(file.getName().toString()+".MP4 ");
}
}
private void SetListViewAdapter() {
ListView lv = (ListView) getView().findViewById(R.id.listViewTest);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.custom_listview, fileList);
lv.setAdapter(adapter);
Toast.makeText(getActivity().getApplicationContext(), fileList.toString(), Toast.LENGTH_LONG).show();
}
}
}