我正在做什么 ::我有一个horizontal listview
,如下所示我正在动态填充项目
正在发生的事情 ::由于动态创建的listview onorientation
更改了已选中项目未选中
问题:如何从适配器收集已检查的项目并重新检查orientation change
上选定的内容
AdpBufTypeSearch.java
public class AdpBufTypeSearch extends BaseAdapter{
private HashMap<String, String> objHashBufType;
SparseBooleanArray mBufTypeArr = new SparseBooleanArray();
private ArrayList<HashMap<String, String>> objListBufType;
Context mContext;
public AdpBufTypeSearch(Context _mContext,ArrayList<HashMap<String, String>> _objListBufType) {
mContext=_mContext;
objListBufType=_objListBufType;
}
@Override
public int getCount() {
return objListBufType.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//LOGIC:: result will be a set on which ones are selected Ex:: 0,1,2,4
public String getSelectedBuffetType() {
//This final value(strBufTypeId) is returned when we access from class
String strBufTypeId="";
for(int i=0;i<objListBufType.size();i++) {
HashMap<String, String> objHashBufType = objListBufType.get(i);
if(objHashBufType.get("selected")=="1") {
strBufTypeId=strBufTypeId+objHashBufType.get(buf_type_mas.COLUMN_BUF_TYPE_ID);
strBufTypeId=strBufTypeId+",";
}
}
//remove the last "," in the string
if(strBufTypeId.lastIndexOf(",")>0)
strBufTypeId=strBufTypeId.substring(0, strBufTypeId.lastIndexOf(","));
return strBufTypeId;
}
/*LOGIC:: <HashMapObject(objHashBufType)> ==> their value of key(selected) is updated to "1" else key(selected) is updated to 0 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_meal_type, null);
final TextView buf_type_name = (TextView) retval.findViewById(R.id.buf_type_name);
TextView buf_type_id=(TextView) retval.findViewById(R.id.buf_type_id);
ImageView buf_type_image=(ImageView) retval.findViewById(R.id.buf_type_image);
final LinearLayout imgBkgSelector=(LinearLayout) retval.findViewById(R.id.imgBkgSelector);
imgBkgSelector.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//LOGIC:: If Selected unselect it and if it is unselected select it
if(mBufTypeArr.get((Integer) imgBkgSelector.getTag())==true){
//INNER-LOGIC:: Background not selected
mBufTypeArr.put((Integer) imgBkgSelector.getTag(), false);
objHashBufType = objListBufType.get((Integer) imgBkgSelector.getTag());
objHashBufType.put("selected", "0");
imgBkgSelector.setBackgroundColor(Color.parseColor(mContext.getString(R.color.cBlack)));
buf_type_name.setTextColor(Color.parseColor(mContext.getString(R.color.cWhite)));
}
else{
//INNER-LOGIC:: Background selected
mBufTypeArr.put((Integer) imgBkgSelector.getTag(), true);
objHashBufType = objListBufType.get((Integer) imgBkgSelector.getTag());
objHashBufType.put("selected", "1");
imgBkgSelector.setBackgroundColor(Color.parseColor(mContext.getString(R.color.cBlue)));
buf_type_name.setTextColor(Color.parseColor(mContext.getString(R.color.cWhite)));
}
}
});
imgBkgSelector.setTag(position);
//Essential code for retain the Background check part on scroll of images
if(mBufTypeArr.get(position)==true){
imgBkgSelector.setBackgroundColor(Color.parseColor(mContext.getString(R.color.cBlue)));
buf_type_name.setTextColor(Color.parseColor(mContext.getString(R.color.cWhite)));
}else{
imgBkgSelector.setBackgroundColor(Color.parseColor(mContext.getString(R.color.cBlack)));
buf_type_name.setTextColor(Color.parseColor(mContext.getString(R.color.cWhite)));
}
// Get the position
objHashBufType = objListBufType.get(position);
// Capture position and set results to the TextViews
//Capitilize the names
String capitalizedBufTypeName = WordUtils.capitalizeFully(objHashBufType.get(buf_type_mas.COLUMN_BUF_TYPE_NAME), ' ');
buf_type_name.setText(capitalizedBufTypeName);
buf_type_id.setText(objHashBufType.get(buf_type_mas.COLUMN_BUF_TYPE_ID));
Picasso.with(mContext)
.load(mContext.getString(R.string.URL_BUFFET_TYPE_IMAGE).trim()+objHashBufType.get(buf_type_mas.COLUMN_BUF_TYPE_IMAGE).trim()).resizeDimen(R.dimen.filter_image_width,R.dimen.filter_image_height).centerCrop().into(buf_type_image);
return retval;
}
}
FrgMdSearch .java
public class FrgMdSearch extends Fragment {
private HashMap<String, String> objHashBufType;
private ArrayList<HashMap<String, String>> objListBufType=null;
private AdpBufTypeSearch bufTypeAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Setting the adapter for buf images<---DYNAMIC VIEWS--->
setAdapterBufImages();
}
private void setAdapterBufImages() {
bufTypeAdapter=new AdpBufTypeSearch(getActivity(),objListBufType);
hListView.setAdapter(bufTypeAdapter);
}
}
答案 0 :(得分:0)
在onSaveInstanceState
方法中发生方向更改时,您需要保存已检查项目的位置还要在AdpBufTypeSearch
适配器中创建一个getter方法,该方法返回已检查项目的当前位置和setter方法设置选中的项目。
<强>样品:强>
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", bufTypeAdapter.getCheckedPosition()); //getCheckedPosition must return the checked item position
}
在片段的oncreateView中
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Setting the adapter for buf images<---DYNAMIC VIEWS--->
setAdapterBufImages(savedInstanceState);
}
private void setAdapterBufImages(Bundle savedInstanceState) {
bufTypeAdapter=new AdpBufTypeSearch(getActivity(),objListBufType);
if(savedInstanceState != null)
{
bufTypeAdapter.setCheckedItem(savedInstanceState.getInt("position")); //will set the checked item
}
hListView.setAdapter(bufTypeAdapter);
}