带标题的CheckedTextView ListView - 排序

时间:2014-06-14 21:53:28

标签: android listview sorting android-listview checkedtextview

我知道这不是关于CheckBoxes和ListViews这个主题的第一个问题,但我不知道该怎么办..

我有一个带CheckedTextView和TextView的ListView,如果我滚动一些其他的将被检查... 视图也被使用,启用/禁用部分(使用SharedPref)也不起作用...

用于:
lalit3686 Blog
Android ListView Headers

所以有我的代码:

ActivtyMain.java

public class ActivityMain extends NavigationDrawerActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    context=this;

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    character_header_id.add(0);
    character_header_id.add(character_original.size()+1);
    character_header_id.add(character_original.size()+character_extension1.size()+2);
    character_header_id.add(character_original.size()+character_extension1.size()+character_extension2.size()+3);

    items.add(new CharacterHeader(getString(R.string.character_original)));
    for(character key:character_original) {
        String value=character_translation.get(key);
        //System.out.println(value);
        items.add(new CharacterListItem(value));
    }
    items.add(new CharacterHeader(getString(R.string.character_extension1)));
    for(character key:character_extension1) {
        String value=character_translation.get(key);
        items.add(new CharacterListItem(value));
    }
    items.add(new CharacterHeader(getString(R.string.character_extension2)));
    for(character key:character_extension2) {
        String value=character_translation.get(key);
        items.add(new CharacterListItem(value));
    }
    items.add(new CharacterHeader(getString(R.string.character_extension3)));
    for(character key:character_extension3) {
        String value=character_translation.get(key);
        items.add(new CharacterListItem(value));
    }

    lv_charactere=((ListView) findViewById(R.id.character_list));
    lv_charactere.setAdapter(new CharacterArrayAdapter(this, items));

    //TODO check/unceck scroll problem
    lv_charactere.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(!character_header_id.contains(position)) {
                CheckedTextView ctv = (CheckedTextView) view.getTag(R.id.listitem_character);
                //CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.list_character);

                System.out.println("character:position:"+position);
                NavigationDrawerActivity.setCharacterCheckedTextViewChecked(ctv);
            }
        }
    });
}

CharacterArrayAdapter.java

public class CharacterArrayAdapter extends ArrayAdapter<Item> {
private LayoutInflater mInflater;
private List<Item> items;

public enum RowTypeText {
    CHECKED_ITEM, TITLE_ITEM
}

public CharacterArrayAdapter(Context context, List<Item> items) {
    super(context, 0, items);
    mInflater = LayoutInflater.from(context);
    this.items=items;
}

@Override
public int getViewTypeCount() {
    return RowTypeText.values().length;
}

@Override
public int getItemViewType(int position) {
    return getItem(position).getViewType();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = new ViewHolder();
    if (convertView == null) {
        if(items.get(position).getClass().equals(CharacterListItem.class)) {
            convertView = mInflater.inflate(R.layout.character_list_item, null);

            viewHolder.checkedtextview = (CheckedTextView) convertView.findViewById(R.id.listitem_character);
            viewHolder.checkedtextview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    CheckedTextView ctv=((CheckedTextView)((CharacterListItem)items.get(getPosition)).getView(mInflater, v));
                    //ctv.setChecked(!((CheckedTextView) v).isChecked()); // Set the value of checkbox to maintain its state.
                    NavigationDrawerActivity.setCharacterCheckedTextViewChecked(ctv);
                    System.out.println("character:position:"+getPosition);
                }
            });
            viewHolder.checkedtextview.setTag(position);

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.listitem_character, viewHolder.checkedtextview);
        }else{
            convertView = mInflater.inflate(R.layout.character_header, null);

            viewHolder.textview=(TextView) convertView.findViewById(R.id.listheader_character);
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.listheader_menu, viewHolder.textview);
        }
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if(viewHolder.checkedtextview!=null) {
        int pos=(int)viewHolder.checkedtextview.getTag(); // This line is important.
        viewHolder.checkedtextview.setChecked(((CheckedTextView)((CharacterListItem)items.get(pos)).getView(mInflater, viewHolder.checkedtextview)).isChecked());

        loadSettings(viewHolder, position);

        return convertView;
    }else{
        return getItem(position).getView(mInflater, convertView);
    }
    //return getItem(position).getView(mInflater, convertView);
}

private void loadSettings(ViewHolder viewHolder, int position) {
    //TODO select if selected before
    for(Map.Entry<character, String> e:NavigationDrawerActivity.character_translation.entrySet()) {
        if(e.getValue().equals(viewHolder.checkedtextview.getText())) {
            if(NavigationDrawerActivity.character_playing.get(e.getKey())==0) {
                viewHolder.checkedtextview.setChecked(false);
                System.out.println("character:loadSaved: "+position+" checked:false");
            }else{
                if(viewHolder.checkedtextview.getText().equals(NavigationDrawerActivity.character_translation.get(character.DERNORMALEDORFBEWOHNER)) ||
                        viewHolder.checkedtextview.getText().equals(NavigationDrawerActivity.character_translation.get(character.WERWOLF))){

                    System.out.println("character:loadSaved: "+position+" disabled");
                    NavigationDrawerActivity.setCharacterCheckedTextViewEnabled(viewHolder.checkedtextview, false, true);
                }else{
                    System.out.println("character:loadSaved: "+position+" checked:true");
                    viewHolder.checkedtextview.setChecked(true);
                }
            }
        }
    }

    //TODO settings -> enable/disable
    boolean ex1=NavigationDrawerActivity.spref.getBoolean(prefs.BOOLEAN_EXTENSION1.toString(), false);
    boolean ex2=NavigationDrawerActivity.spref.getBoolean(prefs.BOOLEAN_EXTENSION2.toString(), false);
    boolean ex3=NavigationDrawerActivity.spref.getBoolean(prefs.BOOLEAN_EXTENSION3.toString(), false);

    for(Entry<character, String> e:NavigationDrawerActivity.character_translation.entrySet()) {
        if(e.getValue().equals(viewHolder.checkedtextview.getText())) {
            if(NavigationDrawerActivity.character_extension1.contains(e.getKey())) {
                System.out.println("character:loadSaved: "+position+" enabled:"+ex1);
                NavigationDrawerActivity.setCharacterCheckedTextViewEnabled(viewHolder.checkedtextview, ex1, false);
            }
        }
        if(e.getValue().equals(viewHolder.checkedtextview.getText())) {
            if(NavigationDrawerActivity.character_extension2.contains(e.getKey())) {
                System.out.println("character:loadSaved: "+position+" enabled:"+ex2);
                NavigationDrawerActivity.setCharacterCheckedTextViewEnabled(viewHolder.checkedtextview, ex2, false);
            }
        }
        if(e.getValue().equals(viewHolder.checkedtextview.getText())) {
            if(NavigationDrawerActivity.character_extension3.contains(e.getKey())) {
                System.out.println("character:loadSaved: "+position+" enabled:"+ex3);
                NavigationDrawerActivity.setCharacterCheckedTextViewEnabled(viewHolder.checkedtextview, ex3, false);
            }
        }
    }
}

private static class ViewHolder{
    protected CheckedTextView checkedtextview;
    protected TextView textview;
}
}

NavigationDrawerActivity.java

public static void setCharacterCheckedTextViewChecked(CheckedTextView ctv) {
    for(Entry<character, String> e:character_translation.entrySet()) {
        if(e.getValue().equals(ctv.getText())) {
            if(ctv.isChecked()) {
                ctv.setChecked(false);
                character_playing.put(e.getKey(),0);
            }else{
                ctv.setChecked(true);
                if(e.getKey().equals(character.DERNORMALEDORFBEWOHNER) || e.getKey().equals(character.WERWOLF)) {
                    character_playing.put(e.getKey(),-1);
                }else {
                    character_playing.put(e.getKey(),1);
                }
            }
            break;
        }
    }
}

public static void setCharacterCheckedTextViewEnabled(CheckedTextView ctv, boolean enabled, boolean checked) {
    ctv.setEnabled(enabled);
    ctv.setClickable(enabled);
    ctv.setLongClickable(enabled);
    ctv.setChecked(checked);
}

ListViewCharacter.java

public class CharacterListItem implements Item {
private final String str;

public CharacterListItem(String text) {
    this(text, false);
}
public CharacterListItem(String text, boolean selected) {
    if(text==null) {
        text="";
    }
    str=text;
}

@Override
public int getViewType() {
    return RowTypeText.CHECKED_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
    View view;
    if (convertView == null) {
        view = (View) inflater.inflate(R.layout.character_list_item, null);
    } else {
        view = convertView;
    }

    CheckedTextView tv=(CheckedTextView) view.findViewById(R.id.listitem_character);
    tv.setText(str);
    return view;
}
}

我想我发布了所有重要的代码部分,如果缺少某些内容,请告诉我 我不知道该怎么做..

1 个答案:

答案 0 :(得分:0)

<强> getView

ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
    if(items.get(position).getClass().equals(CharacterListItem.class)) {
        convertView = mInflater.inflate(R.layout.character_list_item, null);

        viewHolder.checkedtextview = (CheckedTextView) convertView.findViewById(R.id.listitem_character);
        viewHolder.checkedtextview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener(v);
            }
        });

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.listitem_character, viewHolder.checkedtextview);
    }else{
        convertView = mInflater.inflate(R.layout.character_header, null);

        viewHolder.textview=(TextView) convertView.findViewById(R.id.listheader_character);

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.listheader_menu, viewHolder.textview);
    }
}else{
    viewHolder = (ViewHolder) convertView.getTag();
}

if(viewHolder.checkedtextview!=null) {
    viewHolder.checkedtextview.setTag(position); // This line is important.
    viewHolder.checkedtextview.setText(items.get(position).getText());;

    loadSettings(viewHolder);

    return convertView;
}else{
    return getItem(position).getView(mInflater, convertView);
}

重要的变化(我想是这样): 仅在第二部分设置位置