我有baseadapter,我尝试在listview.i loged字符串中适配一些值,我在baseadapter中调整了我和我有不同的结果。在BaseAdapter类也传递这个文本,这两个文本是不同的 这是我的主要Java源代码
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();}
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("logcat start time",
timeInfo.getTimeformat().get(j));
Log.wtf("log cat end time",
timeInfo.getEndTimeFormat().get(
j));
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date _d;
try {
SimpleDateFormat new_df = new SimpleDateFormat("d MMM");
_d = df.parse(timeInfo.getTimeformat().get(
j));
Date _d1 = df.parse(timeInfo.getEndTimeFormat().get(
j));
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(j));
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;
}
}
这是我的基本适配器代码
public class HollAdapters extends BaseAdapter {
private Context mContext;
private final ArrayList<CinemaInfoModel> hollitems1;
public HollAdapters(Context context, ArrayList<CinemaInfoModel> hollitems) {
mContext = context;
this.hollitems1 = hollitems;
}
@Override
public int getCount() {
return hollitems1.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 = hollitems1.get(position);
if (objectItem != null) {
String starttimes = objectItem.getStartTimePeriod().get(
position);
viewHolder.start_time.setText(starttimes);
Log.e("adapter start time", starttimes);
}
return convertView;
}
static class ViewHolderItem {
private TextView start_time;
}
}
this is a my logcat message's screenshot(我是新用户,我无法发布图片)
我不知道我的代码中有什么问题......如果有人知道解决方案,请帮助我 感谢
答案 0 :(得分:0)
试试这个,请改变你这样的代码。如果convertView为null意味着它将使布局膨胀,否则它将重用该视图。如果您需要任何澄清,请告诉我
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);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
/*** Do your stuff here***/