将 Arraylist 从 java.utils 转换为 kotlin.collections.arraylist

Abh*_*kar 5 java android arraylist kotlin

我的项目包含两个类,一个在 java 中,另一个在 kotlin 中。我从 kotlin 调用 java 类中的方法,但该方法返回 arraylist 的格式为 java.utils.arraylist,但除此之外它需要 kotlin.collections.arraylist 的格式。那么有什么方法可以转换或其他方式接受 arraylist 从 java 到 kotlin

科特林类

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])
}
}
Run Code Online (Sandbox Code Playgroud)

以下是java代码

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;
}
}
Run Code Online (Sandbox Code Playgroud)

Ily*_*lya 5

kotlin.collections.ArrayList只是java.util.ArrayListJVM 上的类型别名,因此您可以在需要另一个的地方传递一个。

这里的一个问题可能是您在 Java 中使用原始ArrayList类型。在 Kotlin 中,它将被视为ArrayList<*>,即由未知类型参数化,因此它不能分配给ArrayList<HashMap<String, String>>

在这种情况下,您必须在 Kotlin 中使用未经检查的强制转换:

al = loadContacts() as ArrayList<HashMap<String, String>>
Run Code Online (Sandbox Code Playgroud)

或者 - 更好 - 您应该在 Java 方法中指定类型参数:

public ArrayList<HashMap<String, String>> loadContacts() { ... }
Run Code Online (Sandbox Code Playgroud)