如何从列表中删除选中的项目?

时间:2019-10-11 18:59:38

标签: android class android-listview android-adapter android-checkbox

一旦我按下删除按钮,我试图根据联系人姓名旁边的复选框从联系人列表中删除联系人。现在,当列表中有一个联系人时,我就可以使用该代码,但是,一旦添加超过1个,无论我如何调整循环,它似乎都无法工作。要么崩溃,我怀疑这与列表中的越界问题有关,或者与我当前的代码完全无关。我一直在寻找一些解决方案,但到目前为止,它们都没有起作用。如果有人可以帮助我,将不胜感激。我在当前的实现中包含以下相应的代码:

MainActivity.kt:

class MainActivity : AppCompatActivity() {

var CONTACT_DETAIL_ACTIVITY_REQUEST = 0

var listContacts = ArrayList<Contacts>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setTitle("Contacts")

    val contactAdapter = DSArrayAdapter(this, R.layout.activity_listview, listContacts)
    val listView1 = findViewById<ListView>(R.id.contact_list)
    listView1.setAdapter(contactAdapter)

    listContacts.add(Contacts("Faisal is spoiled", 732, false))
    listContacts.add(Contacts("Dawg", 666, false))
    contactAdapter.notifyDataSetChanged()

    val add_new_contact = findViewById<Button>(R.id.add_contacts)
    val delete_contacts = findViewById<Button>(R.id.delete_contacts)

    //*** Delete Function ***///
    delete_contacts.setOnClickListener({
        var newlistContacts = ArrayList<Contacts>()
        for (x in listContacts) {
            if (x.cchecked == false) {
                newlistContacts.add(x)
            }
        }
        listContacts = newlistContacts
        contactAdapter.notifyDataSetChanged()
    })
    //*** Delete Function ***///

    add_new_contact.setOnClickListener {
        val name = ""
        val phone_number = 1
        val relationship = ArrayList<String>()

        val intent1 = Intent(this,ContactDetails::class.java)
        startActivityForResult(intent1, CONTACT_DETAIL_ACTIVITY_REQUEST)
    }
    }
    }

DSArrayAdapter.kt:

    class DSArrayAdapter(context: Context, resource: Int, list: ArrayList<Contacts>) : ArrayAdapter<Contacts>(context, resource, list) {
    private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    override fun getView(position: Int, convertView: View?, parent: ViewGroup) : View {
    val rowView = inflater.inflate(R.layout.activity_listview, null)
    val item_name = rowView.findViewById<TextView>(R.id.contact_name)
    val item_checkbox = rowView.findViewById<CheckBox>(R.id.checked)

    item_name.setText(getItem(position)?.cname.toString())

    item_checkbox.setOnClickListener(View.OnClickListener{
            val contact = getItem(position) as Contacts
            contact.cchecked = !contact.cchecked
            item_checkbox.isChecked = contact.cchecked
    })

    return rowView
    }
    }

1 个答案:

答案 0 :(得分:1)

您正在更改列表联系人

public DataTable ReadCsv(string filename, bool hasHeaders = true, string delimiter = ",")
{
    var dt = new System.Data.DataTable();

    var fi = new System.IO.FileInfo(filename);

    var connStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties='text;HDR={1};FMT=Delimited({2})';",
                                fi.DirectoryName,
                                ((hasHeaders) ? "Yes" : "No"),
                                delimiter);

    var commStr = string.Format("SELECT * FROM [{0}]", fi.Name);

    using (var conn = new System.Data.OleDb.OleDbConnection(connStr))
    using (var cmd = new System.Data.OleDb.OleDbCommand(commStr, conn))
    using (var adapter = new System.Data.OleDb.OleDbAdapter(cmd))
    {
        adapter.Fill(dt);
    }

    return dt;
}

但是您的适配器不知道newListContacts。 您可以复制呼叫listContacts.clear()和listContacts.addAll(newListContacts) 但是,如果您复制列表,则将需要大量工作。

尝试将您的删除栏更改为:

listContacts = newListContacts