I want to write an application that will a: Fill a list view with picture files from sdcard and b: display an image when choosing an option from the list.
However, it hangs with aforementioned exception. I can't see why...? Please, help!
Main activity:
public class ProjektJimmuActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigateTo("/sdcard/");
}
public void navigateTo(String dir){
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,getListOfFiles(dir)));
getListView().setTextFilterEnabled(true);
}
public String[] getListOfFiles(String dir){
File file = new File(dir);
String[] files = file.list(new Filter());
return files;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object item = l.getItemAtPosition(position);
Toast.makeText(this,"Selection: "+ item.toString(), Toast.LENGTH_SHORT).show();
setContentView(new PictureView(this,item.toString()));
}
private class Filter implements FilenameFilter{
public boolean accept(File dir, String filename) {
return (( filename.endsWith("jpg") || filename.endsWith("png") || filename.endsWith("gif") ) && !filename.startsWith("._") );
}
}
}
Picture view:
public class PictureView extends View{
private Bitmap bitmap;
int width, height;
private Canvas canvas;
public PictureView(Context context, String pictureFile) {
super(context);
setFocusable(true);
bitmap = BitmapFactory.decodeFile("/sdcard/"+pictureFile);
height = bitmap.getHeight();
width = bitmap.getWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(0xFFCCCCCC);
canvas.drawBitmap(bitmap, null, null);
}
}
Because you're extending ListActivity
, you must have a ListView
in your layout xml file, with the id, @android:id/list
. extending ListActivity
means that this activity, mainly is a list, but may have some components beside it. so, when you assume an activity is a list originally, you should have specify a list inside layout xml file of that acitivty, that is the main list. that list, should have the id, @android:id/list
. so, simply set id field of the ListView
of your activity, to @android:id/list
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With