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();
}
}
}
Related
it says IllegalArgumentException: No view found for id for fragment when I'm trying to setview.I know problem lies at 3rd and 4th line. it's News.java file news is a fragment.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_news);
listView = (ListView) getActivity().findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
Fragment.java file is look like below code :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
//ButterKnife.bind(this, view);
init(view);
return view;
}
after that in init method type below code:
private void init(View view){
listView = (ListView) view.findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
}
also u don't forgot to extends Fragment
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'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 have a Spinner in my class TelaEntrada.java loading normally.
public class TelaEntrada extends Fragment {
public Fragment newInstance(Context context) {
TelaEntrada telaEntrada = new TelaEntrada();
return telaEntrada;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.telaentrada, container, false);
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
List<Tipo> tipos = banco.todosTipos();
List<String> item = new ArrayList<String>();
for(Tipo tipo : tipos){
item.add(tipo.getDescricao().toString());
}
final Spinner spn_Entrada = (Spinner) root.findViewById(R.id.spnEntradaTipo);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this.getActivity(),
android.R.layout.simple_spinner_dropdown_item, item);
spn_Entrada.setAdapter(adapter2);
I too have one DialogFragment in another class. Code is below.
public class DialogTipo extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View root = (View) inflater.inflate(R.layout.dialog_tipo, container, false);
final EditText txtDialogTipo = (EditText)root.findViewById(R.id.txtDialogTipo);
Button btnDialogTipoAdd = (Button) root.findViewById(R.id.btnDialogTipoAdd);
Button btnDialogTipoCancel = (Button) root.findViewById(R.id.btnDialogTipoCancel);
btnDialogTipoAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
banco.insereTipo(txtDialogTipo.getText().toString());
//I want update my spinner here!
dismiss();
}
});
getDialog().setTitle("Inserir Novo Tipo");
return root;
}
}
I wanted to add a new item, the spinner was directly updated the database to display the new information in the User spinner. Thanks. Sorry for my english!