android base adapter does not adapter all rows

2019-09-04 06:40发布

问题:

I have two datetimes 12/01/2014 and 12/10/2014.i changed datetime format and i also checked different between there two datetime and i also incremented days between there datetimes.for more information 1 dec,2 dec,3 dec .....10 dec this is a my source

public class MoviesRolls extends Fragment {

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

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


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

@SuppressLint("SimpleDateFormat")
@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);



    ServerItems mainItem = MainmoviesList.arrayOfList
            .get(MainmoviesList.mPosition);

    ArrayList<CinemaModel> cinemas = mainItem.getCinema();
    ArrayList<CinemaInfoModel> infos = new ArrayList<CinemaInfoModel>();

    for (int i = 0; i < cinemas.size(); i++) {
        CinemaModel cinema = cinemas.get(i);
        cinemaInfoArray = cinema.getCinemTimeInfo();
        for (int j = 0; j < cinemaInfoArray.size(); j++) {

            timeInfo = cinemaInfoArray.get(j);

            Log.wtf("starttime1",
                    timeInfo.getTimeformat().get(MainmoviesList.mPosition));
            Log.wtf("endtime1",
                    timeInfo.getEndTimeFormat().get(
                            MainmoviesList.mPosition));

            SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            Date _d;
            try {
                SimpleDateFormat new_df = new SimpleDateFormat("d MMM");
                _d = df.parse(timeInfo.getTimeformat().get(
                        MainmoviesList.mPosition));
                Date _d1 = df.parse(timeInfo.getEndTimeFormat().get(
                        MainmoviesList.mPosition));
                String datetimeis = getDateDiffString(_d1, _d);
                int differentdays = Integer.parseInt(datetimeis);
                Log.e("Different is ", String.valueOf(differentdays));
                for (int k = 0; k < differentdays; k++) {

                    String datetimeformat = dateFormatter(timeInfo
                            .getTimeformat().get(MainmoviesList.mPosition));

                    Date datetime = new_df.parse(datetimeformat);

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

                    datetime = cal.getTime();

                    String ttime = new_df.format(datetime);
                    timeInfo.setStartTimePeriod(ttime);
                    System.out.println(ttime);
                infos.add(timeInfo);
                }
            } catch (ParseException e) {

                e.printStackTrace();
            }


        }

        adapter = new HollAdapters(getActivity(), infos);
        holllistview.setAdapter(adapter);

    }
    return rootView;
}

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

} this is a my baseadapter xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/adapter_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignRight="@+id/imageView1"
        android:layout_marginBottom="15dp"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:text="day"
        android:textColor="#c42a29"
        android:textSize="13dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/imageView1"
        android:layout_below="@+id/adapter_day"
        android:layout_marginRight="25dp"
        android:background="@drawable/justline" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:background="@drawable/adapter_circle" />
</RelativeLayout>

i logend and i can show all information in log.this is a my log cat result (i'm new user and i can't post image) in my option i have problem in my baseadapter because in adapter i can show only first datetime(1 dec) this is a my base adapter code

public class HollAdapters extends BaseAdapter {
private Context mContext;

private final ArrayList<CinemaInfoModel> hollitems;

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

    this.hollitems = hollitems;

}

@Override
public int getCount() {

    return hollitems.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return position;
}

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

    ViewHolderItem viewHolder;

    if (convertView == null) {

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(R.layout.cinema_holl_adapter,
                parent, false);

        viewHolder = new ViewHolderItem();
        viewHolder.start_time = (TextView) convertView
                .findViewById(R.id.adapter_day);

        convertView.setTag(viewHolder);

    } 

        viewHolder = (ViewHolderItem) convertView.getTag();


    CinemaInfoModel objectItem = hollitems.get(position);
    if (objectItem != null) {

        if (position < objectItem.getStartTimePeriod().size()) {
            String starttimes = objectItem.getStartTimePeriod().get(
                    position);

            viewHolder.start_time.setText(starttimes);

        }

    }

    return convertView;

}

static class ViewHolderItem {
    private TextView start_time;

}

}

this is a my divice's screenshot as i said i can show only first datetime 1 Dec.i have no idea what is a wrong in my baseadapter.if anyone knows solution please help me thanks

回答1:

Remove the } else block around the ViewHolder extraction. Move the Holder initialization to after the if{ block. And try now.

if (convertView == null) {

    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    convertView = inflater.inflate(R.layout.cinema_holl_adapter,
            parent, false);

    ViewHolderItem newViewHolder = new ViewHolderItem();
    newViewHolder.start_time = (TextView) convertView
            .findViewById(R.id.adapter_day);

    convertView.setTag(newViewHolder);

}

ViewHolderItem viewHolder = (ViewHolderItem) convertView.getTag(); //This should be called always

/* Rest of your code */

EDIT: What does this block do?

if (position < objectItem.getStartTimePeriod().size()) {
        String starttimes = objectItem.getStartTimePeriod().get(
                position);

        viewHolder.start_time.setText(starttimes);

}