android listview 初始化时被选中的问题

本身是想做一个类似用户配置的界面,在listview里面有checkbox 当用户选中这个checkbox的时候 自动保存,第二次打开的时候也是保存的状态。

最主要的是自定义的 MyAdatper的代码

	class MyAdatper extends BaseAdapter{
		public HashMap<Integer, Boolean> selectedMap;
		String gamesStr = getContentByType("games");
		String alamString = getConfigByGames(gamesStr,"alarms");

		/*
		public MyAdatper(){
			init();
		}
		private void init(){
	        selectedMap = new HashMap<Integer, Boolean>();  
	        JSONArray alamsJsonArray;
			try {
				alamsJsonArray = new JSONArray(alamString);
				for(int i=0; i<alamsJsonArray.length(); i++){
					JSONObject alarmsobj=(JSONObject) alamsJsonArray.get(i);
					String alarmId = alarmsobj.getString("id");
					boolean isSelected = getUserIsChecked(alarmId);
					selectedMap.put(Integer.parseInt(alarmId), isSelected);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		@Override
		public int getCount() {
			try {
				JSONArray jsonCount = new JSONArray(alamString);
				return jsonCount.length();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return 10;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	    private class RecentViewHolder {  
	        TextView alarmTitle;  //提醒名称
	        TextView alarmTime;  //活动时间
	        CheckBox isOpenAlarm; //是否打开提醒
	    } 
		@Override
		public View getView(int position, View contentView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			RecentViewHolder holder;
			if(contentView==null)
			{
				holder = new RecentViewHolder();
				contentView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.alarm_item, null);
				holder.alarmTitle = (TextView) contentView.findViewById(R.id.alarmTitle);
				holder.alarmTime  = (TextView) contentView.findViewById(R.id.alarmTime);
				holder.isOpenAlarm = (CheckBox) contentView.findViewById(R.id.isOpenAlarm);
				contentView.setTag(holder);
			}else{
				holder = (RecentViewHolder) contentView.getTag();
			}
			JSONArray alamsJsonArray;
			try {
				alamsJsonArray = new JSONArray(alamString);
				JSONObject alarmsobj=(JSONObject) alamsJsonArray.get(position);
				String title = alarmsobj.getString("title");
				String target_time = alarmsobj.getString("target_time");
				String clock = alarmsobj.getString("clock");
				holder.alarmTitle.setText(title);
				holder.alarmTime.setText(target_time);
				
				final String alarmId = alarmsobj.getString("id");
				boolean isChecked = getUserIsChecked(alarmsobj.getString("id"));
				holder.isOpenAlarm.setOnCheckedChangeListener(null);
				holder.isOpenAlarm.setChecked(isChecked);
				buttonOnclick(holder.isOpenAlarm,alarmId);
				/*
				//判断用户的这个是否设置
				//boolean isChecked = getUserIsChecked(alarmsobj.getString("id"));
				//Log.e("is_selected",isChecked+"");
				//holder.isOpenAlarm.setSelected(isChecked);
				//holder.isOpenAlarm.setVisibility(View.VISIBLE);
				//boolean checked = selectedMap.get(Integer.parseInt(alarmId));
				//holder.isOpenAlarm.setChecked(checked);
				//final CheckBox checkbox = holder.isOpenAlarm;
				contentView.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						//checkbox.toggle();
						//selectedMap.put(Integer.parseInt(alarmId), checkbox.isChecked());
						
						//holder.isOpenAlarm.setSelected(checkbox.isChecked());
					}
				});*/
				//holder.isOpenAlarm.setId(Integer.parseInt(alarmId));
				

				//buttonOnclick(holder.isOpenAlarm,alarmId);

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return contentView;
		}
		private void buttonOnclick(CheckBox checkbox,final String alarmId){
			checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// 判断是否选中 更新用户的配置
					if(isChecked){
						updateConfigInfo(alarmId,1);
					}else{
						updateConfigInfo(alarmId,0);
					}
				}
			});
		}
	}

其中最主要的是 这块

				boolean isChecked = getUserIsChecked(alarmsobj.getString("id"));
				holder.isOpenAlarm.setOnCheckedChangeListener(null);
				holder.isOpenAlarm.setChecked(isChecked);
				buttonOnclick(holder.isOpenAlarm,alarmId);

我也不知道为什么,但是找了一个下午的解决办法,只有这个是我需要的
阅读更多
个人分类: android
上一篇关于facebook&nbsp;api&nbsp;的应用测试
下一篇关于AlarmManager的问题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭