Я пытаюсь создать представление списка. Я сталкиваюсь с некоторой проблемой при изменении ориентации.
Проблема в том, что: когда я меняю ориентацию экрана, добавляю дубликаты в список. как ограничить это изменение данных
Код:
public class DayPlannerActivity extends Activity {
private TextView txtHeader;
private Context mContext;
private ListView lvDayplanner;
private DayPlannerAdapter adapter;
private Activity activity;
private static Vector<DayPlanner> list = new Vector<DayPlanner>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dayplanner);
mContext = this;
activity = this;
txtHeader = (TextView) findViewById(R.id.txtHeader);
txtHeader.setText(R.string.haivlate);
String[] Checks = {"select","Check1","Check2"};
DayPlanner dp = new DayPlanner("11:00 PM", Checks);
list.add(dp);
dp = new DayPlanner("12:00 PM", Checks);
list.add(dp);
lvDayplanner = (ListView) findViewById(R.id.lvDayplanner);
adapter= new DayPlannerAdapter(activity,list);
lvDayplanner.setAdapter(adapter);
}
}
Список Адаптер:
public class DayPlannerAdapter extends BaseAdapter {
private Activity mActivity;
private static Vector<DayPlanner> list;
private static LayoutInflater inflater;
public DayPlannerAdapter ( Activity _activity,Vector<DayPlanner> _list) {
mActivity = _activity;
list = _list;
inflater = (LayoutInflater)mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView txtScheduledTime;
public Spinner spnrChecks;
public Button btnGo;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.dayplanner_listitem, null);
holder=new ViewHolder();
holder.txtScheduledTime=(TextView)vi.findViewById(R.id.txtScheduledTime);
holder.spnrChecks = (Spinner) vi.findViewById(R.id.spnrChecks);
holder.btnGo = (Button) vi.findViewById(R.id.btnGo);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.txtScheduledTime.setText(list.get(position).getScheduledTime());
ArrayAdapter<String> spnrAdapter=new ArrayAdapter<String>(mActivity,
android.R.layout.simple_spinner_item, list.get(position).getChecks());
spnrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spnrChecks.setAdapter(spnrAdapter);
holder.btnGo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(mActivity,DayPlannerFormActivity.class);
mActivity.startActivity(i);
}
});
return vi;
}
}