我的项目包含两个类,一个在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;
}
}
答案 0 :(得分:0)
kotlin.collections.ArrayList
只是JVM上java.util.ArrayList
的一个类型,所以你可以在另一个预期的地方传递一个。
这里的问题可能在于您在Java中使用raw ArrayList
type。在Kotlin中,它将被视为ArrayList<*>
,即由未知类型进行参数化,因此它不能分配给ArrayList<HashMap<String, String>>
。
在这种情况下,您必须在Kotlin中使用未经检查的强制转换:
al = loadContacts() as ArrayList<HashMap<String, String>>
或者 - 更好 - 您应该在Java方法中指定类型参数:
public ArrayList<HashMap<String, String>> loadContacts() { ... }