这是我的代码 我想在列表视图中显示我的事件,但它没有显示任何内容
这是我的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_events_by_date);
Intent i = getIntent();
dateString = i.getExtras().getString("Date");
elist = (ListView) findViewById(R.id.eventList);
arraylist = new ArrayList<YourEvent>();
adapter = new CustomAdapter(ShowEventsByDateActivity.this, arraylist);
getEvents();
elist.setAdapter(adapter);
}
private void getEvents() {
for(int a=0;a<StaticVariables.eventIds.size(); a++){
if(StaticVariables.eventDates.get(a).equals("6-12-2015")){
eId.add(StaticVariables.eventIds.get(a));
eTitle.add(StaticVariables.eventTitles.get(a));
eDetail.add(StaticVariables.eventDetails.get(a));
eType.add(StaticVariables.eventTypes.get(a));
eDate.add(StaticVariables.eventDates.get(a));
eTime.add(StaticVariables.eventTimes.get(a));
eLocation.add(StaticVariables.eventLocations.get(a));
eChoice.add(StaticVariables.eventChoices.get(a));
}
}
arraylist.clear();
for (int i = 0; i < eId.size(); i++) {
YourEvent ye = new YourEvent(eId.get(i), eTitle.get(i),
eType.get(i), eDetail.get(i), eDate.get(i),
eTime.get(i), eLocation.get(i), eChoice.get(i));
arraylist.add(ye);
}
adapter.notifyDataSetChanged();
}
这是我的customAdapter类
class CustomAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
private List<YourEvent> yeList = null;
private ArrayList<YourEvent> arraylist;
String title1[], detail1[], location1[], id1[];
public CustomAdapter(Context context, List<YourEvent> yeList) {
this.yeList = yeList;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arraylist = new ArrayList<YourEvent>();
this.arraylist.addAll(yeList);
}
public class ViewHolder {
ImageView iv;
// CheckBox cb;
TextView title, location, date, time, choiceText;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.event_list, null);
// Locate the TextViews in listview_item.xml
holder.iv = (ImageView) view.findViewById(R.id.eIv);
holder.title = (TextView) view.findViewById(R.id.eTitle);
holder.date = (TextView) view.findViewById(R.id.eDate);
holder.location = (TextView) view.findViewById(R.id.eLocation);
holder.time = (TextView) view.findViewById(R.id.eTime);
holder.choiceText = (TextView) view.findViewById(R.id.choiceText);
// holder.cb = (CheckBox) view.findViewById(R.id.checkbox);
// holder.cb.setVisibility(View.GONE);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
// holder.id.setText(yeList.get(position).getId());
holder.title.setText(yeList.get(position).getTitle());
holder.date.setText(yeList.get(position).getDate());
holder.location.setText(yeList.get(position).getLocation());
holder.time.setText(yeList.get(position).getTime());
holder.choiceText.setText(eChoice.get(position));
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ViewEventActivity.class);
intent.putExtra("title", eTitle.get(position));
intent.putExtra("type", eType.get(position));
intent.putExtra("detail", eDetail.get(position));
intent.putExtra("date", eDate.get(position));
intent.putExtra("time", eTime.get(position));
intent.putExtra("location", eLocation.get(position));
startActivity(intent);
}
});
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
我尝试过类似的代码,我从在线网络服务获取事件。但我不知道为什么这段代码不起作用。 Plz帮助
答案 0 :(得分:1)
问题在于适配器中的这些行:
this.arraylist = new ArrayList<YourEvent>();
this.arraylist.addAll(yeList);
这会将列表中的原始值(空白)复制到适配器内的新列表中。
然后,当您在活动中添加值时,将它们添加到原始列表中,该列表与适配器中的值不同。
当你执行.addAll()时,2个列表之间的引用会丢失,因为它会按值复制而不是按引用复制。
要解决此问题,只需:
1)将arraylist
重构为List而不是ArrayList
2)设置this.arraylist = yeList
而不是我在上面发布的2行。
这将正确保留2个列表之间的引用,因此当您在活动列表中添加新元素并调用notifyDataSetChanged()
时,它会在列表中显示它们。
答案 1 :(得分:0)
只需交换这两行:
getEvents(); //this would come after setAdapter
elist.setAdapter(adapter);
要
elist.setAdapter(adapter);
getEvents();