繁体   English   中英

我的自定义适配器未在片段中显示

[英]My custom adapter does not show in fragment

我尝试将一系列日期及其详细信息放在片段中的列表视图中。 但是,当我运行该应用程序时,根本没有列表视图,只显示文本视图的区域。 文本视图用于测试,细节正确。

我认为这有什么问题

> MyAdapter myAdapter=new MyAdapter(fragTable.this.getActivity(),R.layout.list_view_item,listofDates);
        lv.setAdapter(myAdapter);

或者

//To get dates between 2 Dates (dates)
        while (cal.getTime().before(stringToDaTe(mdateTo))) {
            cal.add(Calendar.DATE, 1);
            dates.add(datetoString(cal.getTime()));
        }

片段表

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link fragTable#newInstance} factory method to
 * create an instance of this fragment.
 */
public class fragTable extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_LOCNAME = "param1";
    private static final String ARG_LATI = "param2";
    private static final String ARG_LONGI = "param3";
    private static final String ARG_TIMEZONE = "param4";
    private static final String ARG_DATEFROM = "param5";
    private static final String ARG_DATETO = "param6";

    // TODO: Rename and change types of parameters
    private String mlocName;
    private Double mLati;
    private Double mLongi;
    private String mtimeZone;

    private String mdateFrom;
    private String mdateTo;


    ListView lv;
    ArrayList<RSDate> listofDates = new ArrayList<>();

    public fragTable() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment fragTable.
     */
    // TODO: Rename and change types and number of parameters
    public static fragTable newInstance(String locName, double lati, double longi, String timeZone, String datefrom, String dateto) {
        fragTable fragment = new fragTable();
        Bundle args = new Bundle();
        args.putString(ARG_LOCNAME, locName);
        args.putDouble(ARG_LATI,lati);
        args.putDouble(ARG_LONGI,longi);
        args.putString(ARG_TIMEZONE, timeZone);
        args.putString(ARG_DATEFROM,datefrom);
        args.putString(ARG_DATETO,dateto);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mlocName = getArguments().getString(ARG_LOCNAME);
            mLati = getArguments().getDouble(ARG_LATI);
            mLongi = getArguments().getDouble(ARG_LONGI);
            mtimeZone = getArguments().getString(ARG_TIMEZONE);
            mdateFrom = getArguments().getString(ARG_DATEFROM);
            mdateTo = getArguments().getString(ARG_DATETO);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View view = inflater.inflate(R.layout.fragment_frag_table, container, false);


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tv = view.findViewById(R.id.test);



        Integer daysBetween = getDays(mdateFrom,mdateTo);
        TimeZone tz = TimeZone.getTimeZone(mtimeZone);
        GeoLocation geolocation = new GeoLocation(mlocName, mLati, mLongi, tz);
        AstronomicalCalendar ac = new AstronomicalCalendar(geolocation);

        tv.setText(mlocName+" "+mLati+" "+mLongi+" "+mtimeZone+" "+ mdateFrom+"  to   " + mdateTo +"  =  " +daysBetween
                +"------------"+getSunRiseTime(ac,stringToDaTe(mdateFrom))+"------------"+getSunSetTime(ac,stringToDaTe(mdateFrom)));

        //Table
        List<String> dates = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringToDaTe(mdateFrom));
        //To get dates between 2 Dates (dates)
        while (cal.getTime().before(stringToDaTe(mdateTo))) {
            cal.add(Calendar.DATE, 1);
            dates.add(datetoString(cal.getTime()));
        }
        // add to listofDates(RSDate) - dates+ details
        for(String date: dates)
        {
            String clone1= getSunRiseTime(ac,stringToDaTe(date));
            String clone2= getSunSetTime(ac,stringToDaTe(date));
            listofDates.add(new RSDate(date,clone1,clone2));
        }



        lv = view.findViewById(R.id.listOfDate);


        MyAdapter myAdapter=new MyAdapter(fragTable.this.getActivity(),R.layout.list_view_item,listofDates);
        lv.setAdapter(myAdapter);


    }






    public String getSunRiseTime(AstronomicalCalendar ac, Date date)
    {
        String result="";
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        ac.getCalendar().set(year, month, day);

        result = sdf.format(ac.getSunrise());
        return result;
    }
    public String getSunSetTime(AstronomicalCalendar ac,Date date)
    {
        String result="";
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        ac.getCalendar().set(year, month, day);

        result = sdf.format(ac.getSunset());
        return result;
    }

    /**Change from string to date*/
    public Date stringToDaTe(String dateString)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public String datetoString(Date date)
    {
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date today = Calendar.getInstance().getTime();
        String result = df.format(today);
        return result;
    }
    /**Calculate Days between two days using Joda Library*/
    public int getDays(String dateFrom, String dateTo)
    {
        int result;

        java.util.Date date1 = stringToDaTe(dateFrom);
        DateTime dateTime1 = new DateTime(date1);

        java.util.Date date2 = stringToDaTe(dateTo);
        DateTime dateTime2 = new DateTime(date2);

        result = Days.daysBetween(dateTime1, dateTime2).getDays();
        return result;
    }   
}

我的适配器

public class MyAdapter extends ArrayAdapter<RSDate> {

ArrayList<RSDate> dateList = new ArrayList<>();

public MyAdapter(Context context, int textViewResourceId, ArrayList<RSDate> objects) {
    super(context, textViewResourceId, objects);
    dateList = objects;
}

@Override
public int getCount() {
    return super.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.list_view_item, null);
    TextView dateView = (TextView) v.findViewById(R.id.listView_Date);
    TextView srtView = (TextView) v.findViewById(R.id.listView_sunrise);
    TextView sstView = (TextView) v.findViewById(R.id.listView_sunset);
    dateView.setText(dateList.get(position).getDates());
    srtView.setText(dateList.get(position).getSunRiseTime());
    sstView.setText(dateList.get(position).getSunSetTime());

    return v;

}

}

fragment_frag_table.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"
tools:context="com.example.davenguyen.a08suncalculator.fragChooseRange">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />
<ListView
    android:id="@+id/listOfDate"
    android:background="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/colorAccent"
    android:dividerHeight="2dp"></ListView>

@Override
public int getCount() {
    return super.getCount();
}

这将是0 ,您想使用自己数据的计数:

@Override
public int getCount() {
    return dateList.size(); 
}

另外,尝试更换

v = inflater.inflate(R.layout.list_view_item, null);

v = inflater.inflate(R.layout.list_view_item, parent, false);

另外:检查您的ListView是否正确显示在Fragment以缩小适配器或布局上的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM