5

My project contains two classes one in java another in kotlin. I am calling method in java class from kotlin but the method returns arraylist is in format of java.utils.arraylist but while excepting it need in format of kotlin.collections.arraylist. So is there any way if I could convert or other way to accept arraylist from java to kotlin

kotlin class

class contactAllFragment : Fragment() {


@BindView(R.id.contacts_lv) lateinit var contact_lv: ListView

var al = ArrayList<HashMap<String,String>>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    var view: View
    view = inflater.inflate(R.layout.fragment_contact_all,container,false)
    ButterKnife.bind(this,view)


    //load all contacts
    al = LoadAllContacts(activity.application.contentResolver,
            activity.applicationContext)
            .loadContacts()

    var adapter: SimpleAdapter = SimpleAdapter(context,al,R.layout.listview_style,LoadAllContacts.keys,LoadAllContacts.ids);
    if(contact_lv!=null)
        contact_lv.adapter(adapter)




    // Inflate the layout for this fragment
    return view
}

@OnItemClick(R.id.contacts_lv)
fun onItemClick(parent: AdapterView<?>,  position){
    var hm_element: HashMap<String,String> = al.get(position)
    var name: String = hm_element.get(LoadAllContacts.keys[0])
    var number: String = hm_element.get(LoadAllContacts.keys[1])
}
}

following is java code

public class LoadAllContacts {

//parameter to import
private ContentResolver contentResolver;
private Context context;

public static ArrayList al=null;

private Cursor cursor_Android_Contacts = null;
public static final String[] keys = {"name"};
public static final int[] ids = {R.id.contact_name};

public LoadAllContacts( ContentResolver contentResolver, Context context) {
    this.contentResolver = contentResolver;
    this.context = context;
}

public ArrayList loadContacts() {

    al = new ArrayList();

    //to get connection to database in android we use content resolver
    //get all contacts
    try {
        //sort the list while taking contact_id itself

        cursor_Android_Contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
                null,
                null,
                null,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    } catch (Exception e) {

        Log.e("error in contact", e.getMessage());
    }

    //check if it has contacts
    if (cursor_Android_Contacts.getCount() > 0) {


        if (cursor_Android_Contacts.moveToFirst()) {

            do {
            //get the object of class android contact to store values and string to get the data from android database
                HashMap hm = new HashMap();
                String contact_id = cursor_Android_Contacts.getString(
                        cursor_Android_Contacts.getColumnIndex(
                                ContactsContract.Contacts._ID));
                String contact_display_name = cursor_Android_Contacts.getString(cursor_Android_Contacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                hm.put(keys[0], contact_display_name);
                int hasPhoneNumber = Integer.parseInt(cursor_Android_Contacts.getString(cursor_Android_Contacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {

                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =? ",
                            new String[]{contact_id},
                            null
                    );

                    if (phoneCursor.moveToFirst()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //hm.put(keys[1], phoneNumber);
                    }

                    phoneCursor.close();
                }
                al.add(hm);
            } while (cursor_Android_Contacts.moveToNext());
        }
        return al;
    }
    return al;
}
}
Abhishek Borikar
  • 374
  • 1
  • 5
  • 15

1 Answers1

5

kotlin.collections.ArrayList is just a typealias for java.util.ArrayList on JVM, so you can pass one where the other is expected.

A problem here can be in the fact that you use raw ArrayList type in Java. In Kotlin it will be seen as ArrayList<*>, i.e. parametrized by an unknown type and therefore it won't be assignable to ArrayList<HashMap<String, String>>.

In this case you either have to use an unchecked cast in Kotlin:

al = loadContacts() as ArrayList<HashMap<String, String>>

Or - that's better - you should specify type parameters in your Java method:

public ArrayList<HashMap<String, String>> loadContacts() { ... }
Ilya
  • 21,871
  • 8
  • 73
  • 92
  • After specifying the params in the Java method the error still persists and on casting the `Unchecked cast` warning is thrown. Is casting the most accurate solution to this problem? – Neeraj Sewani Aug 26 '19 at 08:01