简体   繁体   English

单击后将Listview设置为删除线项目

[英]Setup Listview to strikethrough item after click

good day, i have a problem here with my listview 美好的一天,我的列表视图有问题

  1. i have a listview with custom arrayadapter which is have 12 item 我有一个带有12个项目的自定义arrayadapter的列表视图
  2. i have already set the listview when click it's get strikethrough on the selected item 我已经设置了单击时的listview删除项

UPDATE 更新

    public class MainActivity extends AppCompatActivity {
    private ListView mainList;
    private final String[] listContent = {
            "1. Get Ready",
            "2. Second ",
            "3. Third",
            "4. Fourth",
            "5. Fifth",
            "6. Sixth",
            "7. Seventh",
            "8. Eight",
            "9. Nineth",
            "10. Ten",
            "11. Eleven",
            "12. Twelve",
            "13. Look up number twelve",
            "14. Its become strikethrough too",
            "15. how to fix it?",
            "16. Please help",
            "17. Thanks",
            "18. End" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainList = (ListView) findViewById(R.id.listview2);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listContent);
        mainList.setAdapter(adapter);

        mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                TextView text = (TextView) view;
                text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }

        });
    }
}

my problem is, when i click the first item (number one) and then it become strikethrough but the 12 item (number twelve) it's also strikethrough to. 我的问题是,当我单击第一个项目(第一个),然后它成为删除线时,但是它也删除了十二个项目(第十二个)。 see my picture 看我的照片

clicking first item: https://i.stack.imgur.com/M0nvm.jpg 单击第一项: https : //i.stack.imgur.com/M0nvm.jpg

number twelve also get strikethrough https://i.stack.imgur.com/1hTPM.jpg 十二号也被删除线https://i.stack.imgur.com/1hTPM.jpg

This is because it is the same View instance that is being used now that the first item is out of view. 这是因为第一个项目不在视图中,因此正在使用的是同一View实例。

For this purpose you will need to implement your own (Base)Adapter and store the information there as to which items should be strike through. 为此,您将需要实现自己的(Base)Adapter并在其中存储有关应删除哪些项目的信息。
Then set the correct paint flags in the getView method as the views move in and out of view. 然后在视图移入和移出视图时,在getView方法中设置正确的绘制标志。

This is happening because of a view is reused. 这是由于视图被重用而发生的。 You need to store the position OR id of that object and provide stroke through effect accordingly. 您需要存储该对象的位置或ID,并相应地通过效果提供笔触。 Hope it will help you out. 希望它能对您有所帮助。 For code reference or help please refer below link.It will definitely help you out 有关代码参考或帮助,请参考下面的链接,它一定会对您有所帮助

http://lalit3686.blogspot.com/2012/06/today-i-am-going-to-show-how-to-deal.html http://lalit3686.blogspot.com/2012/06/today-i-am-going-to-show-how-to-deal.html

https://www.codeproject.com/Questions/896462/Android-Need-to-save-checkbox-state-in-a-custom-ad https://www.codeproject.com/Questions/896462/Android-Need-to-save-checkbox-state-in-a-custom-ad

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

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