Selected Android ListView checkbox

I have a question in 2 parts.

1) How can I populate my ListView so that the rows are displayed, but when the items are selected, the value of the invisible identifier (contact identifier from telephone contacts) is the value that is actually used?

2) I have a ListView that uses multipleChoice mode to select items. He replenished with the names from my contact list. When I select an item in a ListView, I want this selected item to call my SqLite routine to store the value in a database record. How to make this event fire when an item is marked in the list?

This is my XML layout;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ListView
    android:id="@+id/lvContacts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice"
/>

</LinearLayout>

And here is the code that I use to populate my ListView;

    private void fillData() {
    final ArrayList<String> contacts = new ArrayList<String>();

    // Let set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
    cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
    while(cursor.moveToNext()) {
        // Only add contacts that have mobile number entries
        if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
            String name = cursor.getString(1);
            contacts.add(name);
        }
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}

, - onClick , .

. .

+3
1

, - , , :

1) bean, 2 : contactID, contactName

public class contactItem{
    private long contactID;
    private String contactName;

    //...
}

CustomContactAdapter:

public class CustomContactAdapter extends ArrayAdapter<contactItem>{

    ArrayList<contactItem> itemList = null;

    //Constructor
    public CustomContactAdapter (Context context, int MessagewResourceId,
            ArrayList<contactItem> objects, Handler handler) {
        //Save objects and get LayoutInflater
        itemList = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ReceiveMailStruct contact= items.get(position);

        if (contact!= null) {
            view = inflater.inflate(R.layout.layout_display_contact_item, null);
                        //Set view for contact here
         }
    }

}

2) clicker ListView, listitem: 1: :

lvContacts.setOnItemClickListener(new HandlerListClickEvent());

2. (/ )

class HandlerListClickEvent implements OnItemClickListener {
    public void onItemClick( AdapterView<?> adapter, View view, int position, long id ) {
    //Get contact ID here base on item position
}

, , ,

+2

Source: https://habr.com/ru/post/1774005/


All Articles