【问题标题】:Converting Arraylist from java.utils to kotlin.collections.arraylist将 Arraylist 从 java.utils 转换为 kotlin.collections.arraylist
【发布时间】:2018-05-30 19:44:39
【问题描述】:

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

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])
}
}

下面是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;
}
}

【问题讨论】:

  • 这不太清楚 - 您能否添加一些示例代码来说明您的问题?
  • 投到MutableList
  • 你能给出示例代码吗?

标签: java android arraylist kotlin


【解决方案1】:

kotlin.collections.ArrayList 只是 JVM 上 java.util.ArrayList 的类型别名,因此您可以将一个传递到预期的另一个位置。

这里的问题可能在于您在 Java 中使用了raw ArrayList type。在 Kotlin 中,它将被视为 ArrayList&lt;*&gt;,即由未知类型参数化,因此不能分配给 ArrayList&lt;HashMap&lt;String, String&gt;&gt;

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

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

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

public ArrayList<HashMap<String, String>> loadContacts() { ... }

【讨论】:

  • 在 Java 方法中指定参数后,错误仍然存​​在,并且在强制转换时会抛出 Unchecked cast 警告。投射是解决这个问题的最准确的方法吗?
猜你喜欢
  • 2019-09-04
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2015-10-19
  • 1970-01-01
相关资源
最近更新 更多