android baseadapter不工作,只显示最后的元素

时间:2014-10-15 08:55:04

标签: android datetime android-listview baseadapter days

我正在使用BaseAdaper。我试图在ListView中添加一些信息。

PS我有两个日期时间,我在白天之间检查了不同的日子,我试图像这样添加ListView:两天是2 sep和5 sept,我尝试添加2 sep,3sep,4 sep,5月

我写了一些代码,但是在ListView中每次只添加5个sep。这是我的基础代码:

public class HollAdapters extends BaseAdapter {
private Context mContext;

private final ArrayList<CinemaInfoModel> hollitems;
//private CinemaInfoModel objBean;
private TextView start_time,holle,time;

private static LayoutInflater inflater = null;

public HollAdapters(Context context, ArrayList<CinemaInfoModel> hollitems) {
    mContext = context;

    this.hollitems = hollitems;

    inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {

    return hollitems.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}

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

    View grid;

    grid = new View(mContext);
    grid = inflater.inflate(R.layout.cinema_holl_adapter, null);
    start_time = (TextView) grid.findViewById(R.id.adapter_day);
    holle = (TextView) grid.findViewById(R.id.adapter_holl);
    time = (TextView) grid.findViewById(R.id.adapter_time);
    //objBean = hollitems.get(position);

    start_time.setText(hollitems.get(position).getStartTime());

    Log.wtf("adapter time ", hollitems.get(position).getStartTime());
    holle.setText(hollitems.get(position).getHole());
    String start_time=hollitems.get(position).getTime();
    start_time=start_time.replace(",", "\n");
    time.setText(start_time);
    return grid;
}

}

这是我的主要代码:

for (int j = 0; j < cinemasJsonArray.length(); j++) {
                    CinemaModel cinema = new CinemaModel();
                    cinema.setName(cinemasJsonArray.getJSONObject(j)
                            .getString("cinemaName"));

                    JSONArray timeJsonArray = cinemasJsonArray
                            .getJSONObject(j).getJSONArray("info");
                    cinemaInfoArray = new ArrayList<CinemaInfoModel>();
                    for (int k = 0; k < timeJsonArray.length(); k++) {
                        CinemaInfoModel cinemaTime = new CinemaInfoModel();
                        cinemaTime.setTime(timeJsonArray.getJSONObject(k)
                                .getString("time"));
                        cinemaTime.setHole(timeJsonArray.getJSONObject(k)
                                .getString("hole"));

                        SimpleDateFormat df = new SimpleDateFormat(
                                "MM/dd/yyyy");

                        Date _d = df.parse(timeJsonArray.getJSONObject(k)
                                .getString("start_time"));

                        Date _d1 = df.parse(timeJsonArray.getJSONObject(k)
                                .getString("end_time"));

                        SimpleDateFormat new_df = new SimpleDateFormat(
                                "d MMM");



                        String datetimeis = getDateDiffString(_d1, _d);
                        Log.wtf("differentis ", datetimeis);

                        int abc = Integer.parseInt(datetimeis);
                        for (int l = 0; l < abc; l++) {

                            String ab = dateFormatterforLukka(timeJsonArray
                                    .getJSONObject(k).getString(
                                            "start_time"));

                            SimpleDateFormat df2 = new SimpleDateFormat(
                                    "d MMM");

                            Date datetime = df2.parse(ab);

                            Calendar cal = Calendar.getInstance();
                            cal.setTime(datetime);
                            cal.add(Calendar.DATE, l);

                            datetime = cal.getTime();

                            String ttime = new_df.format(datetime);

                            cinemaTime.setStartTime(ttime);
                            Log.wtf("timeeeeeeeee", ttime);

                            cinemaInfoArray.add(cinemaTime);
                        }
                    }

                    cinema.setCinemTimeInfo(cinemaInfoArray);
                    cinemasArrayList.add(cinema);
                }
                movie.setCinema(cinemasArrayList);
                arrayOfList.add(movie);

            }




public String getDateDiffString(Date dateOne, Date dateTwo) {
    long timeOne = dateOne.getTime();
    long timeTwo = dateTwo.getTime();
    long oneDay = 1000 * 60 * 60 * 24;
    long delta = (timeTwo - timeOne) / oneDay;

    if (delta > 0) {

        for (int i = 0; i < delta; i++) {

        }
        return String.valueOf(delta);
    } else {
        delta *= -1;
        return String.valueOf(delta);
    }
}

public static String dateFormatterforLukka(String inputDate) {

    String inputFormat = "MM/dd/yyyy";
    String outputFormat = "d MMM";

    Date parsed = null;
    String outputDate = "";
    try {
        SimpleDateFormat df_input = new SimpleDateFormat(inputFormat,
                new Locale("en", "US"));
        SimpleDateFormat df_output = new SimpleDateFormat(outputFormat,
                new Locale("en", "US"));

        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);

        // Log.wtf("outputDate", outputDate);
    } catch (Exception e) {
        outputDate = inputDate;
    }
    return outputDate;
}

我以这种方式改编了ListView:

public class MoviesRolls extends Fragment {

public final static String TAG = MoviesRolls.class.getSimpleName();

private ListView holllistview;
private HollAdapters adapter;
public static ArrayList<CinemaInfoModel> cinemaInfoArray;


private String title, category1, imdb;

public static TextView movies_title, category, imdbreting;


public static MoviesRolls newInstance() {
    return new MoviesRolls();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.moviesholls, container,
            false);

    holllistview = (ListView) rootView.findViewById(R.id.holllistview);



    holllistview = (ListView) rootView.findViewById(R.id.holllistview);
    movies_title = (TextView) rootView.findViewById(R.id.movies_title1);
    category = (TextView) rootView.findViewById(R.id.category1);

    imdbreting = (TextView) rootView.findViewById(R.id.imdbreting1);

    Bundle bundle = getArguments();

    title = bundle.getString("title");

    imdb = bundle.getString("imdb");

    category1 = bundle.getString("category");

    imdbreting.setText(imdb);

    imdbreting.setText(imdb);

    movies_title.setText(title);
    category.setText(category1);


     adapter = new HollAdapters(getActivity(), MainmoviesList.cinemaInfoArray);
        holllistview.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    ;
    return rootView;

}

正如我所说,如果我的两天是2 sep和5 sep,总是添加3和4 sep,虽然日志似乎没问题。

            Log.wtf("timeeeeeeeee", ttime)

我做错了什么?如果有人知道解决方案,请帮助我

感谢

1 个答案:

答案 0 :(得分:0)

尝试改变:

@Override
public int getCount() {

    return hollitems.size();
}

@Override
public Object getItem(int position) {

    return hollitems.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}