android listview初始化设定选中项
3个回答
展开全部
本身是想做一个类似用户配置的界面,在listview里面有checkbox 当用户选中这个checkbox的时候 自动保存,第二次打开的时候也是保存的状态。
最主要的是自定义的 MyAdatper的代码
[java] view plaincopy
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);
}
}
});
}
}
最主要的是自定义的 MyAdatper的代码
[java] view plaincopy
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);
}
}
});
}
}
展开全部
onCreate的时候先findViewById获取到该ListView,然后ListView中有个setSelection()方法,传的参数为当前选定的Item的position。在onCreate的时候setSelection(position)就好了。
详见http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)
详见http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)
追问
额,麻烦你打出代码好么,我把那个setSelection(position)添上了 也不好使。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以通过绑定在adapter上的初始数据设定来实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询