I am displaying a list of files names in a ListView so i want to get the path of the clicked item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//...
}
any suggestion?
Just get value by position:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String value = (String) lv.getItemAtPosition(position);
//cast it to something else if your object is not String
//for example
File file = (File) lv.getItemAtPosition(position);
String path = file.getAbsolutePath();
}
You can try this in your code .
private List<File> files = new ArrayList<>();
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
File file = files.get(position);
String fileName = file.getName();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.