简体   繁体   English

删除列表视图中的项目

[英]Strikethrough an item on a Listview

I need to put a strikethrough on the text after the item has been checked. 检查项目后,我需要在文本上加上删除线。 I found solutions that use setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 我找到了使用setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);解决方案setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); . I don't use a textview but instead use simple_list_item_multiple_choice for my listview so how do I solve this? 我不使用textview,而是使用simple_list_item_multiple_choice作为listview,那么我该如何解决呢? Here is my entire code: 这是我的整个代码:

public class Surv_list extends Fragment {

    final String[] OPSys = new String[]{"item1","item2","item3","item4"
    };
    ListView myList;
    Button getChoice, clearAll;
    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyUserChoice" ;
    ArrayList<String> selectedItems = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.listlay, container, false);

        myList = (ListView)rootView.findViewById(R.id.list);
        ListView list = (ListView) rootView.findViewById(R.id.list);

        clearAll = (Button)rootView.findViewById(R.id.clearall);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, OPSys);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        myList.setAdapter(adapter);
        list.setAdapter(adapter);
        sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        if(sharedpreferences.contains(MyPREFERENCES)){
            LoadSelections();
        }
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                SaveSelections();

            }

        });


        clearAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
// TODO Auto-generated method stub
                ClearSelections();
            }
        });
return rootView;
    }

    private void SaveSelections() {
// save the selections in the shared preference in private mode for the user

        SharedPreferences.Editor prefEditor = sharedpreferences.edit();
        String savedItems = getSavedItems();
        prefEditor.putString(MyPREFERENCES.toString(), savedItems);
        prefEditor.commit();
    }

    private String getSavedItems() {
        String savedItems = "";
        int count = this.myList.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            if (this.myList.isItemChecked(i)) {
                if (savedItems.length() > 0) {
                    savedItems += "," + this.myList.getItemAtPosition(i);

                } else {
                    savedItems += this.myList.getItemAtPosition(i);
                }
            }
        }
        return savedItems;
    }

    private void LoadSelections() {
// if the selections were previously saved load them

        if (sharedpreferences.contains(MyPREFERENCES.toString())) {

            String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
            selectedItems.addAll(Arrays.asList(savedItems.split(",")));

            int count = this.myList.getAdapter().getCount();

            for (int i = 0; i < count; i++) {
                String currentItem = (String) myList.getAdapter()
                        .getItem(i);
                if (selectedItems.contains(currentItem)) {
                    myList.setItemChecked(i, true);

                } else {
                    myList.setItemChecked(i, false);
                }

            }
        }
    }

    private void ClearSelections() {
// user has clicked clear button so uncheck all the items
        int count = this.myList.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            this.myList.setItemChecked(i, false);
        }
// also clear the saved selections
        SaveSelections();
    }

}

any help would be very much appreciated. 任何帮助将不胜感激。

You can create a custom adapter for your list view and in the getview method of the adpater take the handle of textview . 您可以为列表视图创建自定义适配器,并在adpater的getview方法中使用textview的句柄。 Based on your condition you can combine the already available code to strikeout the text view. 根据您的条件,您可以组合已有的代码以删除文本视图。

for making your own view attributes in list view, you have to make a custom Adapter other than using default adapter. 为了在列表视图中创建自己的视图属性,除了使用默认适配器之外,还必须制作一个自定义适配器。 there is no way you can do this using default adapter. 您无法使用默认适配器执行此操作。 here you can learn how to make custom adapter. 在这里您可以学习如何制作自定义适配器。 also I would suggest you to use RecyclerView instead of ListView 我也建议您使用RecyclerView而不是ListView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM