2

I am working on a BaseAdaper. I'm trying to adaper some information in a ListView.

P.S i have two datetimes and I checked different days between there daytimes and i tried to add in ListView like this: the two daytimes are 2 sep and 5 sept, and I try to add 2 sep , 3sep,4 sep,5 sep

I wrote some code, but in the ListView only 5 sep get added, every time. This is a my baseadaper code:

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

}

this is a part my main code:

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

and I adaptered the ListView in this way:

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;

}

as I said if my two daytimes are 2 sep and 5 sep, always 3th and 4 sep get added, although the logs seem ok.

            Log.wtf("timeeeeeeeee", ttime)

What am I doing wrong? if anyone knows solution please help me

thanks

Rick77
  • 3,121
  • 25
  • 43
viviano
  • 113
  • 1
  • 7
  • i think the issue is the in parsing logic. try to place this cinemaInfoArray.add(cinemaTime); outside the for loop. Also, getItem(int position) must be changed as suggested below. – SKK Oct 15 '14 at 09:07
  • @viviano: as a community, here at SO we are all but picky about form/syntax, as we understand how hard it is to write in a foreign language; yet you are not even **trying** to be clear. I edited your post in the hope to make it at least readable but, in the future avoid at least childish typos (missing spaces/punctuation, for instance): they are a clear statement about how little you care about your reader and are a slap in the face of everybody trying to help you. – Rick77 Oct 15 '14 at 09:11

1 Answers1

0

Try to change:

@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;
}
Quoc Truong
  • 545
  • 4
  • 9