当屏幕方向改变时,ListView 在列表中添加了重复项目
我尝试创建列表视图,当方向改变时我遇到一些问题。 问题是:当我更改屏幕列表视图的方向时,在列表中添加重复的列表项。如何限制此数据更改
代码是:
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);
}
}
List 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;
}
}
I trying to create List View i face some problem when orientation changes.
Problem is: when i changes orientation of screen list-view add duplicate list item in list. how to restrict this data change
Code Is:
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);
}
}
List 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 Android 会在方向更改时自动保存
View
状态,因此您需要一种方法来知道这是否不是第一次调用onCreate
。幸运的是,这很简单:覆盖onSaveInstanceState
,并存储 1 个值,以使您在onCreate
中获得的包不为空。然后,当重新创建 Activity 时,
onCreate
中的参数savedInstanceState
将不为空。因此,只需进行测试:在将数据添加到视图之前。
Since Android automatically saves
View
s states when the orientation changes, you need a way to know if it's not the first call toonCreate
. Luckily, it's easy: OverrideonSaveInstanceState
, and store even 1 value to make the bundle your get inonCreate
not-null.Then, when the activity is recreated, the parameter
savedInstanceState
inonCreate
will not be null. So just do the test:Before you add data to your views.
这是因为您的 dayplanner 对象列表是静态的,因此当您更改视图的方向时,它会重新创建活动,但由于在 java 中不会重新创建静态对象,而是为该类型保存,因此它使列表有两个相同的对象。
its because your list of dayplanner objects is static, so when you change the orientation of the view it recreates the activity but since in java a static object is not recreated, but saved for that type, it makes the list have two of the same.
这里的聚会太晚了,仍然会回答,因为它可能对其他人有用。我也遇到了同样的问题,通过
正确实现视图持有者设计模式
解决了这个问题。Way too late the party here,will still answer as it might be useful to someone else. I was also having the same issue it was resolved by
PROPERLY implementing the view holder design pattern
.