Piyush Gupta Piyush Gupta - 1 month ago 6
Android Question

Listview not loading data inside Fragment

New in Android, trying to understand how it works. I am trying to load a Fragment Dynamically. I have a Layout with Four Buttons, and a Frame Layout to load an activity inside that frame layout according to the button clicked. Activity Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#00BFF3">
<Button
android:id="@+id/teachers"
android:layout_width="180sp"
android:layout_height="25sp"
style="?attr/borderlessButtonStyle"
android:background="#00BFF3"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_marginTop="10sp"
android:text="Staff Directory"
android:textAlignment="viewStart"
android:layout_marginStart="15sp"
android:onClick="Teachers"/>
<Button
android:id="@+id/attendances"
android:layout_width="175sp"
android:layout_height="25sp"
style="?attr/borderlessButtonStyle"
android:background="#00BFF3"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_marginTop="10sp"
android:text="Attendance List"
android:textAlignment="viewStart"
android:layout_marginStart="185sp"
android:onClick="Attendances"/>
<Button
android:id="@+id/todayAttandance"
android:layout_width="190sp"
android:layout_height="25sp"
style="?attr/borderlessButtonStyle"
android:background="#00BFF3"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_marginTop="10sp"
android:text="Today Attendance"
android:textAlignment="viewStart"
android:layout_marginStart="355sp"
android:onClick="TodayAttendance"/>

<Button
android:id="@+id/students"
android:layout_width="150sp"
android:layout_height="25sp"
style="?attr/borderlessButtonStyle"
android:background="#00BFF3"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_marginTop="10sp"
android:text="Student List"
android:textAlignment="viewStart"
android:layout_marginStart="545sp"
android:onClick="Students"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_marginTop="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>


Listview Template:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="40sp"
android:text="TextView"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/classname"
android:layout_width="0dp"
android:layout_height="40sp"
android:text=""
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/fname"
android:layout_width="0dp"
android:layout_height="40sp"
android:text=""
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/fphone"
android:layout_width="0dp"
android:layout_height="40sp"
android:text=""
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


Activity.Java:

public class firstWindow extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_window);

}
public void Students(View v){
OpenWindow(1);
}
public void OpenWindow(int index){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
studentFragment fragment = new studentFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
}


Fragment Code: Downloads the data from an API:

public class studentFragment extends ListFragment implements iEvents<student> {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);


student s = new student();
ArrayList<student> arraylist = new ArrayList<student>();
s.setName("Vikas");
s.setId(1);
s.setAddress("G-20, Arjun Nagar");
s.setPhone("9818899114");
arraylist.add(s);
studentAdapter simpleAdapter = new studentAdapter(getActivity().getApplicationContext(),arraylist);
setListAdapter(simpleAdapter);


}

@Override
public void Success(List<student> extenders) {

if(extenders!=null){
if(extenders.size()==1){
//may be a failure

student s = extenders.get(0);
if(s.getCode()==-1){
//error
return;
}
}

if(extenders.size() > 0){
//valid collection
// setContentView(R.layout.studentlistview);

ArrayList<student> arraylist = new ArrayList<student>(extenders);
context.get_context().setStudentList(extenders);
studentAdapter simpleAdapter = new studentAdapter(getActivity().getApplicationContext(),arraylist);

setListAdapter(simpleAdapter);
// setContentView(R.layout.activity_student_show_all);
}
}

}

@Override
public void Failure(List<student> extenders) {

}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
////outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//showDetails(position);
student selectedItem = (student) context.get_context().getStudentList().get(position);
context.get_context().StudentHolder = selectedItem;
ShowDetails(selectedItem);
}

private void ShowDetails(student stu){

}

/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {

}

}


Adapter Code:

public class studentAdapter extends ArrayAdapter<student> {

public studentAdapter(Context context, ArrayList<student> students) {

super(context, R.layout.studentlistview, students);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
student user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.studentlistview, parent, true);
}
// Lookup view for data population
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView className = (TextView) convertView.findViewById(R.id.classname);
TextView father = (TextView) convertView.findViewById(R.id.fname);
TextView fatherMobile = (TextView) convertView.findViewById(R.id.fphone);


name.setText(user.getName());
className.setText(user.getClassName());
father.setText(user.getFatherName());
fatherMobile.setText(user.getFatherPhone());
return convertView;
}
}


So when I debug, it shows that 40 items of type Student are loaded, but the view is not refreshing it shows me this:Screenshot of image

It seems that the view is loading the values, but the view coming up on screen is different from the view which is loading the values so it is not showing up. Any help?

Answer

Since you claim that your list is showing 40 elements come through, I think your XML layout for the adapter is wrong or your student objects return all empty strings for getName(), getClassName(), etc.

For example, you've set the "row" layout to both match_parent on the width and height. This will cause one "row" to occupy the entire screen.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" <!-- change to "wrap_content" -->
>

If that isn't the problem, you could try getActivity() instead of getActivity().getApplicationContext(), which, yes, is the Activity, but an Activity is a Context. For details on that, you can see Using Application context everywhere?

Comments