Listview没有在Fragment中加载数据

时间:2016-10-11 17:11:19

标签: android listview android-fragments

Android中的新功能,试图了解它的工作原理。我正在尝试动态加载片段。我有一个带有四个按钮的布局,以及一个框架布局,用于根据单击的按钮在该框架布局内加载活动。活动代码:

<?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模板:

<?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();
    }
}

片段代码:从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) {

}

}

适配器代码:

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;
    }
}

因此,当我调试时,它显示已加载了40个类型为Student的项目,但视图未刷新,它显示为:Screenshot of image

似乎视图正在加载值,但是屏幕上显示的视图与加载值的视图不同,因此它没有显示。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

由于您声称列表显示了40个元素,我认为适配器的XML布局错误或getName()个对象返回getClassName()match_parent的所有空字符串等等。

例如,您已在宽度和高度上将“行”布局设置为<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" --> > 。这将导致一个“行”占据整个屏幕。

getActivity()

如果这不是问题,您可以尝试getActivity().getApplicationContext()而不是<?php if ($get_result != false) { while ($row = mysqli_fetch_array($get_result, MYSQLI_ASSOC)) { ?> <iframe width="560" height="315" src="<?php echo $row["url"]; ?>" frameborder="0" allowfullscreen></iframe> <?php } } ?> ,其中,是,是活动,但活动是上下文。有关详情,请参阅Using Application context everywhere?