Membuat Listview dengan menampilkan files di folder Android Studio Java
Fungsi, Array Adapter, Bebas Taro Dimanapun
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
Taro Kode ini Di Bawah
super.onCreate(savedInstanceState);
setContentView(R.layout.favorited);
final ListView listview = (ListView) findViewById(R.id.listView1);
//pencarian files di folder
final ListView listview = (ListView) findViewById(R.id.listView1);String path = Environment.getExternalStorageDirectory().toString() + "/RanairuCreation/Favorite";
File directory = new File(path);
File[] files = directory.listFiles();
//fungsi listview
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < files.length; ++i) {
list.add(files[i].getName());
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//fungsi fileslist ketika di klik
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
// final String item = (String) parent.getItemAtPosition(position);
//klik yg akan terjadi, contoh seperti dibawah
AlertDialog.Builder a = new AlertDialog.Builder(Favorited.this);
a.setView(v);
a.show();
a.setCancelable(true); }
activity xml nya
<ListView
android:layout_width="match_parent"
android:id="@+id/listView1"
android:layout_height="wrap_content"/>