ListActivity, custom row layout with Bitmap, NOT populating correctly

元气小坏坏 提交于 2019-12-13 12:44:32

问题


here is a code snippet of my ListActivity with an adapter that is attempting to

a) get a list of the file paths of all files in a particular SD card directory

b) factory.decode those files to bitmaps and add them to the custom row in listActivity

    File directory = new File("/sdcard/DirectoryOfStuff");
    String[] filePaths= directory.list;

    ArrayAdapter<Bitmap> pics = new ArrayAdapter<Bitmap>();
    for(int i = 0; i<filePaths.length; i++){
    Bitmap b = BitmapFactory.decodeFile(filePaths[i]);
    pics.add(b); }


    setListAdapter(new ArrayAdapter<Bitmap>(this, R.layout.custom_row, pics) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row;

        if (null == convertView) {
        row = mInflater.inflate(R.layout.custom_row, null);
        } else {
        row = convertView;
        }

        ImageView iv = (ImageView) row.findViewById(R.id.imageView_row);
        iv.setImageBitmap(getItem(position));

        return row;
        }
       });

i tried to do that from memory, i'll go home later and edit any mistakes- but that should be the jist of it.

when I run this, nothing happens, as if i didn't specify setContentView(R.layout.main), which i have. main.xml holds a ListView in it.

ONE OF THE ISSUES HAS BEEN SOLVED:

//this directory path needs to pre-fix the filepath when trying to 'decode' via BitmapFactory, oops
String DIRECTORY_PATH = "/sdcard/folder_name/"

for(int i = 0; i<filePaths.length; i++){
     Bitmap b = BitmapFactory.decodeFile(DIRECTORY_PATH + filePaths[i]);
     pics.add(b);
}

ALL PROBLEMS SOLVED:

layoutInflater wasn't instantiated...

   mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final working code snippet:

    ArrayList<Bitmap> pics = new ArrayList<Bitmap>();

    String DIRECTORY_PATH = "/sdcard/Directory/";
    File file = new File(DIRECTORY_PATH);
    String[] s = file.list();       

    for(int i = 0; i<s.length; i++){
        Bitmap b = BitmapFactory.decodeFile(DIRECTORY_PATH + s[i]);
        pics.add(b);
    }

   mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    setListAdapter(new ArrayAdapter<Bitmap>(this, R.layout.row, pics) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row;

        if (null == convertView) {
        row = mInflater.inflate(R.layout.row, null);
        } else {
        row = convertView;
        }

        ImageView iv = (ImageView) row.findViewById(R.id.imageView_row);
        iv.setImageBitmap(getItem(position));
        return row;
        }
       });

FOR HAVEXZ: here is the custom row_row layout i used, although I will add some more things to the row when I figure out the most accessible/easy way for the user to navigate and handle the pictures

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView 
        android:id="@+id/imageView_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />    

    </LinearLayout>

also for havexz, just incase your wondering, here is the main.xml layout - equally as simple as the row_layout, just one view in the middle of a layout :)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list"></ListView>

</LinearLayout>

来源:https://stackoverflow.com/questions/8548732/listactivity-custom-row-layout-with-bitmap-not-populating-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!