ListFragment shows "Empty List" when it most definitely is not - java

I'm trying to set an adapter to a ListFragment, but no matter what I do it always appears as an "Empty List". I'm instantiating studentList just before setting the adapter. There must be something very simple that I'm missing. How can I write this so that my code displays the given studentList?
Main Screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#android:id/empty"
android:layout_gravity="center"
android:text="#string/empty_list"
style="#style/font_large"/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainScreenFrag.java
public class MainScreenFrag extends ListFragment {
public final static String TITLE = "Student List";
public final static String TAG = "MainScreenFrag";
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
String[] studentList = {"me", "you", "everybody", "nobody","some people", "most people", "just me", "ok you too"};
setListAdapter(new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
studentList));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen, container, false);
}
}

You're not calling
super.onViewCreated(view, savedInstanceState);
in your onViewCreated
Give that a try

Its mostly because you have not given an id for your list Adapter.
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, studentList);
lv.setAdapter(arrayAdapter);
This should work, though the method seems redundant.

Related

ListView not showing on my app

I created a fragment connected to a list view to display a simple table view.
Here is my fragment:
public class ConfRoomListFragment extends Fragment {
ApplicationController activity;
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
activity = (ApplicationController) getActivity();
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
R.layout.activity_listview, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
}
Here is my fragment_conf_room_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Here is my activity_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
When I run my app, all that shows is a blank screen. I created the list view and added the adapter. There doesn't seem to be any variable issues, so I'm not sure what the problem is.
EDIT
I've already added my fragment to the activity:
ApplicationController.java
/* Instantiate all Fragments needed*/
directoryViewFragment = new DirectoryViewFragment();
directoryListFragment = new DirectoryListFragment();
officeViewFragment = new OfficeViewFragment();
officeListFragment = new OfficeListFragment();
confRoomListFragment = new ConfRoomListFragment();
settingsFragment = new SettingsFragment();
directionsFragment = new DirectionsFragment();
airportInfoFragment = new AirportInfoFragment();
publicationsFragment = new PublicationsFragment();
practiceGroupListFragment = new PracticeGroupListFragment();
practiceGroupFragment = new PracticeGroupFragment();
As you can see I instantiated my fragment.
This is where the selection occurs:
private class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position, false);
System.out.println(position);
current_position = position;
invalidateOptionsMenu();
}
}
/**
* Diplaying fragment view for selected nav drawer list item
*/
private void displayView(int position, boolean firstClick) {
// update the main content by replacing fragments
if (firstClick) {
Fragment fragment = directoryViewFragment;
currentPanel = Panel.HomeView;
getFragmentManager().beginTransaction()
.replace(R.id.fragmentHolder, fragment)
.addToBackStack(null)
.commit();
getFragmentManager().executePendingTransactions();
} else {
new ViewSelectionListener(this).onItemClick(null, null, position, 0);
}
// update selected item and title, then close the drawer
setTitle(navMenuTitles[position]);
drawerLayout.closeDrawer(navList);
}
I know it works because the setTitle function works and the title of the fragment is Conference Rooms.
This is my ViewSelectionListener.java, where I update the Fragment manager to show my fragment:
} else if (position == 2) {
//Conference Rooms
activity.setCurrentPanel(Panel.ConfRoomDirectory);
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction()
.setCustomAnimations(Tags.TRANSITION_IN, Tags.TRANSITION_OUT)
// TODO If there are other fragments to hide, put them here!
.hide(activity.getDirectoryViewFragment())
.hide(activity.getPublicationsFragment())
.hide(activity.getSettingsFragment())
.hide(activity.getAirportInfoFragment())
.hide(activity.getDirectionsFragment())
.hide(activity.getPracticeGroupListFragment())
.hide(activity.getPracticeGroupFragment())
.hide(activity.getOfficeViewFragment())
.show(activity.getConfRoomListFragment());
if (activity.isPhone) {
transaction.hide(activity.getDirectoryListFragment());
transaction.hide(activity.getOfficeListFragment());
} else if (activity.isPortrait()) {
transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_left);
if (DirectoryViewFragment.leftPanelIsShowing) {
transaction.remove(activity.getDirectoryListFragment());
DirectoryViewFragment.leftPanelIsShowing = false;
}
if (OfficeViewFragment.leftPanelIsShowing) {
transaction.remove(activity.getOfficeListFragment());
OfficeViewFragment.leftPanelIsShowing = false;
}
if (PracticeGroupFragment.leftPanelIsShowing) {
transaction.remove(activity.getPracticeGroupListFragment());
PracticeGroupFragment.leftPanelIsShowing = false;
}
}
transaction.commit();
}
As you can see, I hide all of the other fragments and show the fragment that I wanted. However, only the title get updated, the fragment isn't shown. So the issue doesn't lie with the Activity, there's some other error that I don't see.
Check if onCreateView() method of your fragment is really called. If not then your fragment is not added to activity. Make sure to call
getFragmentManager().beginTransaction() .add(R.id.fragmentHolder, ConfRoomListFragment) .addToBackStack(null) .commit();
before you call FragmentTransaction's show(ConfRoomListFragment) method.
If ConfRoomListFragment's onCreate() method is called try to use next constructor of ArrayAdapter:
ArrayAdapter adapter = = new ArrayAdapter<>(getContext(), R.layout.activity_listview, mobileArray);
And change list item (activity_listview.xml) a little bit:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
activity = (ApplicationController) getActivity();
Remove above line, this line will never use in this fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
fragment_conf_room_list.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
You need to specify TextView layout in adapter if you want use your own layout.
public class ConfRoomListFragment extends Fragment {
ApplicationController activity;
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
activity = (ApplicationController) getActivity();
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
R.layout.activity_listview, R.id.label, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
}
And for your ListView height must be set to match_parent
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

Button listener in ListView

I used AsyncTask to fulfill my ListView.
public class SIPSettingsFragment extends ListFragment implements View.OnClickListener, AsyncResponse {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sipsettings, container, false);
new DownloadJSON().execute();
return rootView;
}
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
/*
some code
*/
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
listView = (ListView) getActivity().findViewById(android.R.id.list);
adapter = new SimpleAdapter(
getActivity(),
usersList,
R.layout.sipuser_list_item,
new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
new int[] { R.id.username, R.id.addr, R.id.state}
);
((SimpleAdapter) adapter).notifyDataSetChanged();
setListAdapter(adapter);
Log.d("lab", "Done");
}
And my xml
fragment_sipsettings.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userListLayout"
android:layout_gravity="center_horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
sipuser_list_item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username: "
android:layout_alignParentLeft="true"
android:id="#+id/usernameSIP" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/username"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/usernameSIP" />
<Button
android:id="#+id/deleteSIPUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete user"
android:layout_alignParentRight="true"/>
</RelativeLayout>
List with buttons are display correctly. How to implement button OnClickListerer for button from sipuser_list_item.xml?
Edit:
I solve my question by extend SimpleAdapter and override getView() in AsyncTask #Krupal Shah and #cylon
You can either extend SimpleAdapter or use Anonymous inner class like below, In the extending or anonymous class, you have to override getView() method. Inside getView method, you can find button by id and set click listener on that.
SimpleAdapter k=new SimpleAdapter(
getActivity(),
usersList,
R.layout.sipuser_list_item,
new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
new int[] { R.id.username, R.id.addr, R.id.state}
)
{
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
Button btn=(Button)v.findViewById(R.id.sipuser_list_item);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// click of the button
}
});
return view;
}
};
Try to overwrite the getView() method of the SimpleAdapter or write your own adapter that extends the BaseAdapter class.
adapter = new SimpleAdapter(...) {
#Override
public View getView (int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
// get your button here
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(....); // set your onclick listener
return view;
}
}

Change view on button click from CustomAdapter

I have created a layout that contains a listView, the view is populated with multiple similar elements in the onCreateView function from a Fragment, the view is populated in the CustomAdapter extends BaseAdapter. In each element of the listView I have a button for which I want to implement a onClickListener. When the button is clicked I want to switch to another view, something similar to viewing comments on the GooglePlus mobile app.
I have tried doing so this way in the CustomAdapter:
public void setOnClickComment(View view) {
final ImageButton btn = (ImageButton)view.findViewById(R.id.addComment);
if (btn != null) {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONCLICK", "I just clicked this shit" + btn.getTag());
manager.beginTransaction()
.replace(R.id.feedLayout, new CommentFragment()).commit();
}
});
} else {
Log.d("CLICK", "ESTE NULL");
}
}
The manager is a FragmentManager and it's passed from the fragment to the CustomAdapter constructor. I can see the Log in LogCat but that's it nothing else happens, the tag of the button is the position of the element.
This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/feedLayout"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
And in the fragment I inflate the above layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.swipe_screen_left, container, false);
view = (ListView) linear.getChildAt(0);
adapter = new CustomAdapter(getActivity(), data, getFragmentManager());
view.setAdapter(adapter);
return linear;
}

Cannot resolve method setListAdapter

I try to show listView in fragment. But method setListAdapter - is not resolved.
I think, that i must to get id of listView (android.R.id.list);
and then : lv.setAdapter(mAdapter);but its dont work too.
public class MyEmployeeFragment extends Fragment {
private CustomAdapter sAdapter;
private List<User> userList;
ProgressDialog mProgressDialog;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
userList = new ArrayList<User>();
sAdapter = new CustomAdapter(getActivity(),userList);
setListAdapter(sAdapter);
new CustomAsync().execute();
}
private class CustomAdapter extends ArrayAdapter<User> {
private LayoutInflater inflater;
public CustomAdapter(Context context, List<User> objects) {
super(context, 0, objects);
inflater = LayoutInflater.from(context);
}
class ViewHolder {
TextView id;
TextView name;
TextView lastName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vH;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_employee, null);
vH = new ViewHolder();
vH.id = (TextView) convertView.findViewById(R.id.tv_employee_id);
vH.name = (TextView) convertView.findViewById(R.id.tv_employee_name);
vH.lastName = (TextView) convertView.findViewById(R.id.tv_employee_last_name);
convertView.setTag(vH);
} else
vH = (ViewHolder) convertView.getTag();
final User user = getItem(position);
vH.id.setText(user.getId());
vH.name.setText(user.getName());
vH.lastName.setText(user.getLastName());
return convertView;
}
}
private class CustomAsync extends AsyncTask<Void,Void,List<User>>
{}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#B7B7B7"
/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/empty"
android:layout_above="#+id/progressBar2"
android:layout_alignRight="#+id/progressBar2"
android:layout_alignEnd="#+id/progressBar2"
android:layout_marginBottom="83dp">
</TextView>
</RelativeLayout>
setListAdapter is a method of ListFragment while your fragment extends Fragment.
http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter)
So you either extend ListFragment
or
Extend Fragment inflate a layout with ListView, initialize ListView in onCreateView of Fragment and then use listView.setAdapter(adapter).
In case you want to extend Fragment
<ListView
android:id="#+id/list"
Then
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourxml, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.list);
lv.setAdapter(youradapter);
return rootView;
}
setListAdapter() is a method in ListFragment..For this you need to extend ListFragmnet instead of Fragment.
Change this line
public class MyEmployeeFragment extends Fragment
into
public class MyEmployeeFragment extends ListFragment
You need define the ListView first:
ListView list = (ListView) findById(android.R.id.list);
And later you need put the adapter:
list.setAdapter(sAdapter);
Or change
extends Fragment
to
extends ListFragment
extends ListFragment should be working fine.
Below is a sample that works for me..
public class Chicks extends ListFragment implements OnItemClickListener {
}

Getting a constructor error with ArrayAdapter in a ListFragment on Android

Here's my code. I'm getting the error The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined on ArraryAdapter.
How do I populate the listView1 with the array items?
public class MainFragment extends ListFragment {
String[] items = { "12 Monkeys", "(500) Days of Summer", "Chariots of Fire" };
#Override
public void onCreate(Bundle savedInstanceState)
{
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
and here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Fragments are not context objects. You need to pass the Activity object (Fragment.getActivity()) as first argument instead.

Categories

Resources