gpt4 book ai didi

android - ListView 在屏幕方向更改时在列表中添加了重复项

转载 作者:行者123 更新时间:2023-11-29 15:28:55 25 4
gpt4 key购买 nike

我尝试创建 ListView 时,当方向发生变化时我遇到了一些问题。问题是:当我更改屏幕 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);
}

}

列表适配器:

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

}

最佳答案

由于 Android 会在方向更改时自动保存 View 的状态,因此您需要一种方法来了解它是否不是第一次调用 onCreate。幸运的是,这很简单:重写 onSaveInstanceState,甚至存储 1 个值以使您在 onCreate 中获得的 bundle 不为空。

@Override
public void onSaveInstanceState(Bundle outInstanceState) {
outInstanceState.putInt("value", 1);
}

然后,当重新创建 Activity 时,onCreate 中的参数 savedInstanceState不会为 null。所以只做测试:

if(savedInstanceState != null)

在向 View 添加数据之前。

关于android - ListView 在屏幕方向更改时在列表中添加了重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8942135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com