I want to fill a listView with filenames. All works fine with full path, but I’ld prefere just the name.
My extension function doesnt work as ment:
class SelectActivity : AppCompatActivity() {
//...
override fun onCreate(savedInstanceState: Bundle?) {
...
lv_Files = findViewById(R.id.lv_Files) as ListView
listDir()
...
}
val fileList = ArrayList<File>()
// I'ld like only name, not full path:
fun File.toString() = this.name // "is shadowed by member" <== ???? no effect!
fun listDir() {
fileList.clear()
val files = rootdir.listFiles()
files.mapTo(fileList) { it }
val viewedFiles=ArrayAdapter(this,android.R.layout.simple_list_item_1,fileList)
lv_Files.adapter = viewedFiles
}
//...
}
If a class has a member function, and an extension function is defined which has the same receiver type, the same name and is applicable to given arguments, the member always wins.
Though it doesn’t solve my problem, so let my ask the other way round:
How can I make _ArrayAdapter( _, , ArrayList<File>) use the desired toString function in Kotlin?
Help to ArrayAdapter: However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects.Override the toString() method of your objectsto determine what text will be displayed for the item in the list.
PS: " … the member always wins. …"
I don’t understand: … so when an extension function could work ever , as overrride is not accepted (here) by compiler.
(saluti a Milano)
Even if it worked, it would be a bad solution, because it could have changed a lot of other code, you do not want to be changed. I think that the most correct way to address the problem is to implement custom adapter (which is not very complicated), but wrapper object with custom toString() also suitable solution.
means that class DecoratedFile with a constructor that takes a parameter name of type string inherits from class File, passing along the string name to its constructor. Think extends if familiar with Java.
I’m quite familiar with Java. (At least I thought! )
I’ve made about 10 (real used) applications so far. and all are working fine.
Nevertheless I’m not that firm to know all constructions really good.
I knew “extends”, but did not happen to use it really. (Vintage programm style? )
When you add a new activity to your Android project you get a new Java class like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// code
}
}
This literally means that publicly accessible class MainActivity that inherits from the class AppCompatActivity (as in it contains the entirety of AppCompatActivity) contains a function onCreate but no constructors, so it inherits AppCompatActivity’s constructor. A constructor is, simply put, a special function in Java, C++ and some other languages, that gets executed when you create an instance of an object. Its purpose is to establish the initial state of the class.
Kotlin allows you to write things like this more concisely.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// code
}
}
Indeed, there is no need. A custom adapter would be much better (and why not go with recyclerview in the meantime) as it also allows for more complex views and was intended for this purpose. As an alternative, have the arraylist just contain the string values (but then you don’t have your file object internally).