android基础适配器不适配所有行

时间:2014-12-11 10:49:14

标签: android android-listview arraylist baseadapter

我有两个日期时间12/01/2014和12/10 / 2014.i更改了日期时间格式,我还检查了两个日期时间之间的不同,我也增加了日期之间的天数。更多信息 1 dec,2 dec,3 dec ..... 10 dec 这是我的来源

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

} 这是我的baseadapter xml代码

<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,我可以在日志中显示所有信息。this is a my log cat result(我是新用户,我无法发布图片) 在我的选项中我的baseadapter有问题因为在适配器中我只能显示第一个日期时间(1月12日) 这是我的基本适配器代码

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 正如我所说,我只能显示第一个日期时间12月12日。我不知道我的baseadapter有什么问题。如果有人知道解决方案,请帮助我 感谢

1 个答案:

答案 0 :(得分:0)

删除ViewHolder提取周围的} else块。将Holder初始化移动到if{块之后。现在试试。

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 */

编辑: 这个街区做了什么?

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

        viewHolder.start_time.setText(starttimes);

}